File Coverage

blib/lib/WebService/LastFM.pm
Criterion Covered Total %
statement 19 21 90.4
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 26 28 92.8


line stmt bran cond sub pod time code
1             package WebService::LastFM;
2              
3 2     2   55445 use strict;
  2         5  
  2         78  
4 2     2   10 use warnings;
  2         5  
  2         55  
5              
6 2     2   10 use Carp ();
  2         7  
  2         36  
7 2     2   10 use Digest::MD5 ();
  2         4  
  2         39  
8 2     2   2789 use LWP::UserAgent;
  2         589620  
  2         85  
9 2     2   3673 use HTTP::Request::Common qw(POST GET);
  2         6336  
  2         182  
10 2     2   2638 use XML::Simple;
  0            
  0            
11              
12             use WebService::LastFM::Session;
13             use WebService::LastFM::NowPlaying;
14             use WebService::LastFM::Track;
15             use WebService::LastFM::Playlist;
16              
17             our $VERSION = '0.07';
18              
19             sub new {
20             my ( $class, %args ) = @_;
21             my $self = bless {}, $class;
22              
23             $self->_die('Username and password are required')
24             unless $args{username} || $args{password};
25              
26             $self->{username} = $args{username};
27             $self->{password} = $args{password};
28             $self->{ua} = LWP::UserAgent->new( agent => "WebService::LastFM/$VERSION", );
29              
30             return $self;
31             }
32              
33             sub ua { $_[0]->{ua} }
34              
35             sub get_session {
36             my $self = shift;
37              
38             my $url
39             = 'http://ws.audioscrobbler.com/radio/handshake.php'
40             . '?username='
41             . $self->{username}
42             . '&version= ' . "1.1.1"
43             . '&platform= ' . "linux"
44             . '&passwordmd5='
45             . Digest::MD5::md5_hex( $self->{password} );
46              
47             my $response = $self->_get_response( GET $url);
48              
49             $self->_die('Wrong params passed')
50             if !( keys %$response ) || $response->{session} eq 'FAILED';
51              
52             %$self = ( %$self, %$response );
53             return WebService::LastFM::Session->new($response);
54             }
55              
56             sub get_new_playlist {
57             my $self = shift;
58             my $xspf_xml = $self->_get_new_xspf();
59             my $xml = XML::Simple->new();
60             my $xspf = $xml->XMLin($xspf_xml);
61              
62             my @playlist;
63             foreach my $track_id ( keys %{ $xspf->{trackList}{track} } ) {
64             push @playlist, WebService::LastFM::Track->new( $xspf->{trackList}{track}{$track_id} )
65             }
66             my $playlist = WebService::LastFM::Playlist->new({ tracks => \@playlist });
67             return $playlist;
68              
69             }
70              
71             sub _get_new_xspf {
72             my $self = shift;
73             my $session_key = $self->{session} or $self->_die('Must have a session to get xspf');
74             my $url = 'http://ws.audioscrobbler.com/radio/xspf.php' .
75             '?sk=' . $self->{session} . '&discovery=0&desktop=0';
76              
77             my $content = $self->_do_request( GET $url );
78             return $content;
79             }
80              
81              
82             sub get_nowplaying {
83             my $self = shift;
84             my $url = 'http://ws.audioscrobbler.com/radio/np.php' . '?session=' . $self->{session};
85              
86             my $response = $self->_get_response( GET $url);
87             return WebService::LastFM::NowPlaying->new($response);
88             }
89              
90             sub send_command {
91             my ( $self, $command ) = @_;
92             $self->_die('Command not passed') unless $command;
93              
94             my $url = 'http://ws.audioscrobbler.com/radio/control.php' . '?session=' . $self->{session} . '&command=' . $command;
95              
96             my $response = $self->_get_response( GET $url);
97             return $response->{response};
98             }
99              
100             sub change_station {
101             my ( $self, $user ) = @_;
102             $self->_die('URL not passed') unless $user;
103              
104             my $url = 'http://ws.audioscrobbler.com/radio/adjust.php' . '?session=' . $self->{session} . '&url=' . "user/$user/personal";
105              
106             my $response = $self->_get_response( GET $url);
107             return $response->{response};
108             }
109              
110             sub change_tag {
111             my ( $self, $tag ) = @_;
112             $self->_die('tag not passed') unless $tag;
113             $tag =~ s/ /\%20/;
114              
115             my $url = 'http://ws.audioscrobbler.com/radio/adjust.php' . '?session=' . $self->{session} . '&url=' . "globaltags/$tag";
116              
117             my $response = $self->_get_response( GET $url);
118             return $response->{response};
119             }
120              
121             sub _get_response {
122             my ( $self, $request ) = @_;
123             my $content = $self->_do_request($request);
124             my $response = $self->_parse_response($content);
125             return $response;
126             }
127              
128             sub _parse_response {
129             my ( $self, $content ) = @_;
130             my $response = {};
131              
132             $response->{$1} = $2 while ( $content =~ s/^(.+?) *= *(.*)$//m );
133              
134             return $response;
135             }
136              
137             sub _do_request {
138             my ( $self, $request ) = @_;
139             my $response = $self->ua->simple_request($request);
140              
141             $self->_die( 'Request failed: ' . $response->message )
142             unless $response->is_success;
143              
144             return $response->content;
145             }
146              
147             sub _die {
148             my ( $self, $message ) = @_;
149             Carp::croak($message);
150             }
151              
152             1;
153              
154             __END__