Admin.pm

filter: Statementstmt 28.7% 29 / 101 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 56.2% 9 / 16 Podpod 100% 5 / 5 totaltotal 26.0% 43 / 165 CC 22 SCAR 33.9
CC
22
Cov
21%
new
72.0
_fetch_status_xml
42.0
listeners
30.0
CRAP 29.5
↑ Summary Admin/Song.pm »
1 package Net::Shoutcast::Admin;
2 # $Id: Admin.pm 229 2008-02-13 00:10:09Z davidp $
3  
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Net::Shoutcast::Admin::Song;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Net::Shoutcast::Admin::Listener;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 $VERSION = '0.02';
15  
16  
17 =head1 NAME
18  
19 Net::Shoutcast::Admin - administration of Shoutcast servers
20  
21  
22 =head1 VERSION
23  
24 This document describes Net::Shoutcast::Admin version 0.0.2
25  
26  
27 =head1 SYNOPSIS
28  
29 use Net::Shoutcast::Admin;
30  
31 my $shoutcast = Net::Shoutcast::Admin->new(
32 host => 'server hostname',
33 port => 8000,
34 admin_password => 'mypassword',
35 );
36
37 if ($shoutcast->source_connected) {
38 printf "%s is currently playing %s by %s",
39 $shoutcast->dj_name,
40 $shoutcast->currentsong->title,
41 $shoutcast->currentsong->artist
42 ;
43 } else {
44 print "No source is currently connected.";
45 }
46
47
48 =head1 DESCRIPTION
49  
50 A module to interact with Shoutcast servers to retrieve information about
51 their current status (and perhaps in later versions of the module, to also
52 control the server in various ways).
53  
54  
55 =head1 INTERFACE
56  
57 =over 4
58  
59 =item new
60  
61 $shoutcast = Net::Shoutcast::Admin->new( %params );
62  
63 Creates a new Net::Shoutcast::Admin object. Takes a hash of options
64 as follows:
65  
66 =over 4
67  
68 =item B<host>
69  
70 The hostname of the Shoutcast server you wish to query.
71  
72 =item port
73  
74 The port on which Shoutcast is running. Defaults to 8000 if not specified.
75  
76 =item B<admin_password>
77  
78 The admin password for the Shoutcast server.
79  
80 =item timeout
81  
82 The number of seconds to wait for a response. Defaults to 10 seconds if
83 not specified.
84  
85 =item agent
86  
87 The HTTP User-Agent header which will be sent in HTTP requests to the Shoutcast
88 server. If not supplied, a suitable default will be used.
89  
90 =back
91  
92 =cut
93  
94 0 ▶sub new {
Subroutine new: not called CC=8 SCAR=42.8
Pod documented
95  
96 0 my ($class, %params) = @_;
97
98 0 my $self = bless {}, $class;
99
100 0 $self->{last_update} = 0;
101
102 0 my %acceptable_params = map { $_ => 1 }
103 qw(host port admin_password timeout agent);
104
105 # make sure we haven't been given any bogus parameters:
106 0 ▶ if (my @bad_params = grep { ! $acceptable_params{$_} } keys %params) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@bad_params) = grep({not $acceptable_params{$_};} keys %params)) 0 0 0
107 0 carp "Net::Shoutcast::Admin does not recognise param(s) "
108 . join ',', @bad_params;
109 0 return;
110 }
111
112 #
113 0 $self->{$_} = $params{$_} for keys %acceptable_params;
114
115 # set decent defaults for optional params:
116 0 ▶ $self->{port} ||= 8000;
Condition0%0/2
$self->{"port"} ||= 8000
l!l
00
MC/DC0%0/1
$self->{"port"} ||= 8000
$self->{"port"}
Truth table0%0/2
A
A= $self->{"port"}
Aresult
11
00
117 0 ▶ $self->{timeout} ||= 10;
Condition0%0/2
$self->{"timeout"} ||= 10
l!l
00
MC/DC0%0/1
$self->{"timeout"} ||= 10
$self->{"timeout"}
Truth table0%0/2
A
A= $self->{"timeout"}
Aresult
11
00
118 # In my initial testing, it seems the Shoutcast server will not respond
119 # with the status XML if the User-Agent does not contain "Mozilla"
120 # (Why? That's just lame!)
121 0 ▶ $self->{agent} ||= "Mozilla";
Condition0%0/2
$self->{"agent"} ||= "Mozilla"
l!l
00
MC/DC0%0/1
$self->{"agent"} ||= "Mozilla"
$self->{"agent"}
Truth table0%0/2
A
A= $self->{"agent"}
Aresult
11
00
122
123 0 ▶ if (my @missing_params = grep { ! $self->{$_} } keys %acceptable_params) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@missing_params) = grep({not $self->{$_};} keys %acceptable_params)) 0 0 0
124 0 carp "Net::Shoutcast::Admin->new() must be supplied with params: "
125 . join ',', @missing_params;
126 0 return;
127 }
128
129 # get an LWP::UserAgent object to make our requests with:
130 0 my $ua = new LWP::UserAgent;
131 0 ▶ if ($ua) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ua) { } 0 0 0
132 0 $ua->agent( $self->{agent} );
133 0 $ua->timeout( $self->{timeout} );
134 0 $self->{ua} = $ua;
135 } else {
136 0 warn "Failed to create LWP::UserAgent object";
137 0 return;
138 }
139
140 0 return $self;
141  
142 }
143  
144  
145 # if we haven't fetched the status XML recently, fetch it. Returns true if
146 # we either fetched it successfully or it was fresh enough to not need
147 # re-fetching, or false if we couldn't get it.
148 0 ▶sub _update_if_necessary {
Subroutine _update_if_necessary: not called CC=4 SCAR=30.0
149 0 my $self = shift;
150 0 ▶ if ($self->{last_update} and (time - $self->{last_update}) < 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"last_update"} and time - $self->{"last_update"} < 5) 0 0 0
Condition0%0/3
$self->{"last_update"} and time - $self->{"last_update"} < 5
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"last_update"} and time - $self->{"last_update"} < 5
$self->{"last_update"} time - $self->{"last_update"} < 5
Truth table0%0/3
A and B
A= $self->{"last_update"}B= time - $self->{"last_update"} < 5
ABresult
0X0
100
111
151 # status was updated not long ago
152 0 return 1;
153 }
154
155 0 my ($fetched, $msg) = $self->_fetch_status_xml;
156 0 ▶ if (!$fetched) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fetched) 0 0 0
157 0 warn "Failed to fetch status from Shoutcast server: $msg";
158 0 return;
159 }
160
161 # all good.
162 0 return 1;
163 }
164  
165  
166 0 ▶sub _fetch_status_xml {
Subroutine _fetch_status_xml: not called CC=6 SCAR=37.4
167 0 my $self = shift;
168
169 0 my ($host, $port) = @$self{qw(host port)};
170 0 my $pass = URI::Escape::uri_escape( $self->{admin_password} );
171
172 0 my $url = "http://$host:$port/admin.cgi?pass=$pass&mode=viewxml";
173
174 0 my $response = $self->{ua}->get($url);
175
176 0 ▶ if (!$response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
177 0 my $err = "Failed to fetch status XML - " . $response->status_line;
178 0 carp $err;
179 0 ▶ return wantarray? (0, $err) : 0;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
180 }
181
182 0 my $data = XML::Simple::XMLin($response->content,
183 forceArray => [qw(LISTENER SONG)]);
184
185 0 ▶ if (!$data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 0 0 0
186 0 ▶ return wantarray? (0, 'Failed to parse XML') : 0;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
187 }
188
189 0 $self->{data} = $data;
190 0 $self->{last_update} = time;
191
192 0 ▶ return wantarray? (1, undef) : 1;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
193 }
194  
195  
196  
197 =item currentsong
198  
199 Returns a Net::Shoutcast::Admin::Song object representing the current
200 song.
201  
202 =cut
203  
204 0 ▶sub currentsong {
Subroutine currentsong: not called CC=2 SCAR=17.9
Pod documented
205 0 my $self = shift;
206 0 ▶ $self->_update_if_necessary or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_update_if_necessary or return 0 0 0
207
208 my $song = Net::Shoutcast::Admin::Song->new(
209 title => $self->{data}->{SONGTITLE}
210 0 );
211 0 return $song;
212 }
213  
214  
215 =item song_history
216  
217 Returns a list of Net::Shoutcast::Admin::Song objects representing
218 the the last few songs played
219  
220 =cut
221  
222 0 ▶sub song_history {
Subroutine song_history: not called CC=2 SCAR=17.9
Pod documented
223 0 my $self = shift;
224 0 my @song_objects;
225 0 $self->_fetch_status_xml;
226
227 0 for my $song (@{ $self->{data}->{SONGHISTORY}->{SONG} }) {
228 push @song_objects, Net::Shoutcast::Admin::Song->new(
229 title => $song->{TITLE},
230 played_at => $song->{PLAYEDAT},
231 0 );
232 }
233
234 0 return (@song_objects);
235 }
236  
237  
238 =item listeners
239  
240 In scalar context, returns the number of listeners currently connected.
241 In list context, returns a list of Net::Shoutcast::Admin::Listener
242 objects representing each listener.
243  
244 =cut
245  
246 0 ▶sub listeners {
Subroutine listeners: not called CC=5 SCAR=34.0
Pod documented
247 0 my $self = shift;
248 0 $self->_fetch_status_xml;
249
250 0 ▶ if (!wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (not wantarray) { } 0 0 0
251 # okay, it's nice and simple:
252 0 return $self->{data}->{CURRENTLISTENERS};
253 } else {
254 # okay, we need to return a list of N:S:A::Listener objects:
255 0 ▶ return if !$self->{data}->{CURRENTLISTENERS};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"data"}{"CURRENTLISTENERS"} 0 0 0
256
257 0 my @listener_objects;
258 0 for my $listener (@{ $self->{data}->{LISTENERS}->{LISTENER} }) {
259 push @listener_objects, Net::Shoutcast::Admin::Listener->new(
260 host => $listener->{HOSTNAME},
261 connect_time => $listener->{CONNECTTIME},
262 underruns => $listener->{UNDERRUNS} || 5,
263 agent => $listener->{USERAGENT},
264 0 ▶ );
Condition0%0/2
$listener->{'UNDERRUNS'} || 5
l!l
00
MC/DC0%0/1
$listener->{'UNDERRUNS'} || 5
$listener->{'UNDERRUNS'}
Truth table0%0/2
A
A= $listener->{'UNDERRUNS'}
Aresult
11
00
265 }
266
267 0 return (@listener_objects);
268 }
269 }
270  
271  
272 =item source_connected
273  
274 Returns true if the stream is currently up (a source is connected and streaming
275 audio to the server)
276  
277 =cut
278  
279 0 ▶sub source_connected {
Subroutine source_connected: not called CC=1 SCAR=6.9
Pod documented
280 0 my $self = shift;
281 0 $self->_fetch_status_xml;
282 0 return ($self->{data}->{STREAMSTATUS});
283 }
284  
285  
286 3 1; # Magic true value required at end of module
287 __END__
↑ Summary Admin/Song.pm »