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