blib/lib/Audio/Scrobbler2.pm

filter: Statementstmt 100% 109 / 109 Branchbran 95.8% 23 / 24 Conditioncond 67.8% 19 / 28 MC/DCmcdc 50.0% 8 / 16 Subroutinesub 100% 19 / 19 Podpod 100% 6 / 6 totaltotal 91.0% 184 / 202 CC 24 SCAR 19.9
CC
24
Cov
94%
_request
16.1
_signature
3.1
track_scrobble
3.0
CRAP 7.3
↑ Summary
1 package Audio::Scrobbler2;
2  
3 3 ▶use 5.026;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Encode qw(encode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use HTTP::Tiny 0.084;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use JSON::PP qw(decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 our $VERSION = '1.00';
14  
15 3 my $API_URL = 'https://ws.audioscrobbler.com/2.0/';
16  
17 6 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
18 6 my ($class, $api_key, $api_secret) = @_;
19  
20 6 _require_value($api_key, 'API key');
21 5 _require_value($api_secret, 'API secret');
22  
23 4 ▶ my $self = bless {
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
24 api_key => $api_key,
25 api_secret => $api_secret,
26 api_url => $API_URL,
27 }, ref($class) || $class;
28  
29 4 $self->{http} = $self->_build_http_client;
30  
31 4 return $self;
32 }
33  
34 1 ▶sub _build_http_client {
Subroutine _build_http_client: called CC=1 SCAR=0.0
35 1 return HTTP::Tiny->new(
36 agent => "Audio-Scrobbler2/$VERSION",
37 keep_alive => 1,
38 timeout => 30,
39 verify_SSL => 1,
40 );
41 }
42  
43 15 ▶sub _signature {
Subroutine _signature: called CC=3 SCAR=11.4
44 15 my ($self, $params) = @_;
45  
46 my $signature = join '',
47 42 map { $_ . $params->{$_} }
48 43 ▶ sort grep { $_ ne 'api_sig' && $_ ne 'callback' && $_ ne 'format' }
Condition50%3/6
$_ ne 'api_sig' && $_ ne 'callback' && $_ ne 'format'
!ll&&!rl&&r
0142
$_ ne "api_sig" and $_ ne "callback"
!ll&&!rl&&r
0043
MC/DC33%1/3
$_ ne 'api_sig' && $_ ne 'callback' && $_ ne 'format'
$_ ne "api_sig" $_ ne "callback" $_ ne 'format'
Truth table50%2/4
A and B && C
A= $_ ne "api_sig"B= $_ ne "callback"C= $_ ne 'format'
ABCresult
0XX0
10X0
1100
1111
49 15 keys %{$params};
50  
51 15 return md5_hex(encode_utf8($signature . $self->{api_secret}));
52 }
53  
54 13 ▶sub _signed_params {
Subroutine _signed_params: called CC=1 SCAR=0.0
55 13 my ($self, $params) = @_;
56 13 my %signed_params = %{$params};
57  
58 13 $signed_params{api_sig} = $self->_signature(\%signed_params);
59  
60 13 return \%signed_params;
61 }
62  
63 15 ▶sub _request {
Subroutine _request: called CC=16 SCAR=27.8
64 15 my ($self, $params, $method) = @_;
65 15 ▶ $method //= 'GET';
Condition100%2/2
$method //= "GET"
l!l
312
MC/DC100%1/1
$method //= "GET"
$method
Truth table100%2/2
A
A= $method
Aresult
11
00
66  
67 15 my %fields = (%{$params}, format => 'json');
68 15 my $response;
69  
70 15 ▶ if ($method eq 'POST') {
Branch: 2 branches
BranchTrueFalseTotal
if ($method eq 'POST') { } 2 13 15
elsif ($method eq 'GET') { } 12 1 13
71 2 $response = $self->{http}->post_form($self->{api_url}, \%fields);
72 }
73 elsif ($method eq 'GET') {
74 12 my $query = $self->{http}->www_form_urlencode(\%fields);
75 12 $response = $self->{http}->get("$self->{api_url}?$query");
76 }
77 else {
78 1 croak "Unsupported HTTP method: $method";
79 }
80  
81 14 ▶ my $content = $response->{content} // '';
Condition50%1/2
$response->{"content"} // ""
l!l
140
MC/DC0%0/1
$response->{"content"} // ""
$response->{"content"}
Truth table50%1/2
A
A= $response->{"content"}
Aresult
11
00
82 14 my ($data, $json_error);
83  
84 14 ▶ if (length $content) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $content) 13 1 14
85 13 local $@;
86 13 $data = eval { decode_json($content) };
87 13 $json_error = $@;
88 }
89  
90 14 ▶ if (ref($data) eq 'HASH' && exists $data->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "HASH" and exists $data->{"error"}) 1 13 14
Condition100%3/3
ref $data eq "HASH" and exists $data->{"error"}
!ll&&!rl&&r
581
MC/DC100%2/2
ref $data eq "HASH" and exists $data->{"error"}
ref $data eq "HASH" exists $data->{"error"}
Truth table100%3/3
A and B
A= ref $data eq "HASH"B= exists $data->{"error"}
ABresult
0X0
100
111
91 1 ▶ my $message = $data->{message} // 'Unknown error';
Condition50%1/2
$data->{"message"} // "Unknown error"
l!l
10
MC/DC0%0/1
$data->{"message"} // "Unknown error"
$data->{"message"}
Truth table50%1/2
A
A= $data->{"message"}
Aresult
11
00
92 1 croak "Last.fm API error $data->{error}: $message";
93 }
94  
95 13 ▶ if (!$response->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->{"success"}) 2 11 13
96 2 ▶ my $status = $response->{status} // 'unknown';
Condition50%1/2
$response->{"status"} // "unknown"
l!l
20
MC/DC0%0/1
$response->{"status"} // "unknown"
$response->{"status"}
Truth table50%1/2
A
A= $response->{"status"}
Aresult
11
00
97 2 ▶ my $reason = $response->{reason} // 'Unknown error';
Condition50%1/2
$response->{"reason"} // "Unknown error"
l!l
20
MC/DC0%0/1
$response->{"reason"} // "Unknown error"
$response->{"reason"}
Truth table50%1/2
A
A= $response->{"reason"}
Aresult
11
00
98 2 ▶ my $kind = $status eq '599' ? 'transport' : 'HTTP';
Branch: 1 branch
BranchTrueFalseTotal
$status eq '599' ? : 1 1 2
99 2 croak "Last.fm $kind error $status: $reason";
100 }
101  
102 11 ▶ croak 'Last.fm returned an empty response' unless length $content;
Branch: 1 branch
BranchTrueFalseTotal
unless length $content 1 10 11
103 10 ▶ croak "Last.fm returned invalid JSON: $json_error" if $json_error;
Branch: 1 branch
BranchTrueFalseTotal
if $json_error 1 9 10
104 9 ▶ croak 'Last.fm returned an unexpected JSON value' unless ref($data) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 1 8 9
105  
106 8 return $data;
107 }
108  
109 9 ▶sub auth_getToken {
Subroutine auth_getToken: called CC=1 SCAR=0.0
Pod documented
110 9 my ($self) = @_;
111  
112 my $params = $self->_signed_params({
113 api_key => $self->{api_key},
114 9 method => 'auth.getToken',
115 });
116 9 my $response = $self->_request($params);
117 3 my $token = $response->{token};
118  
119 3 _require_value($token, 'Token in Last.fm response');
120 2 $self->{api_token} = $token;
121  
122 2 return $token;
123 }
124  
125 3 ▶sub auth_getSession {
Subroutine auth_getSession: called CC=2 SCAR=6.9
Pod documented
126 3 my ($self) = @_;
127  
128 3 _require_value($self->{api_token}, 'API token');
129  
130 my $params = $self->_signed_params({
131 api_key => $self->{api_key},
132 method => 'auth.getSession',
133 token => $self->{api_token},
134 2 });
135 2 my $response = $self->_request($params);
136 2 my $session = $response->{session};
137 2 ▶ my $session_key = ref($session) eq 'HASH' ? $session->{key} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $session eq 'HASH' ? : 2 0 2
138  
139 2 _require_value($session_key, 'Session key in Last.fm response');
140 1 $self->{api_session} = $session_key;
141  
142 1 return $session_key;
143 }
144  
145 2 ▶sub set_session_key {
Subroutine set_session_key: called CC=1 SCAR=0.0
Pod documented
146 2 my ($self, $key) = @_;
147  
148 2 _require_value($key, 'Session key');
149 1 $self->{api_session} = $key;
150  
151 1 return $key;
152 }
153  
154 2 ▶sub track_updateNowPlaying {
Subroutine track_updateNowPlaying: called CC=1 SCAR=0.0
Pod documented
155 2 my ($self, $artist, $track) = @_;
156  
157 2 _require_value($artist, 'Artist');
158 2 _require_value($track, 'Track');
159 2 _require_value($self->{api_session}, 'Session key');
160  
161 my $params = $self->_signed_params({
162 api_key => $self->{api_key},
163 artist => $artist,
164 method => 'track.updateNowPlaying',
165 sk => $self->{api_session},
166 1 track => $track,
167 });
168  
169 1 return $self->_request($params, 'POST');
170 }
171  
172 6 ▶sub track_scrobble {
Subroutine track_scrobble: called CC=3 SCAR=11.0
Pod documented
173 6 my ($self, $artist, $track, $timestamp) = @_;
174  
175 6 _require_value($artist, 'Artist');
176 5 _require_value($track, 'Track');
177 4 _require_value($self->{api_session}, 'Session key');
178 4 ▶ croak 'Timestamp must be a positive integer'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timestamp and $timestamp =~ /\A[1-9][0-9]*\z/u 3 1 4
Condition100%3/3
defined $timestamp and $timestamp =~ /\A[1-9][0-9]*\z/u
!ll&&!rl&&r
121
MC/DC100%2/2
defined $timestamp and $timestamp =~ /\A[1-9][0-9]*\z/u
defined $timestamp $timestamp =~ /\A[1-9][0-9]*\z/u
Truth table100%3/3
A and B
A= defined $timestampB= $timestamp =~ /\A[1-9][0-9]*\z/u
ABresult
0X0
100
111
179 unless defined $timestamp && $timestamp =~ /\A[1-9][0-9]*\z/;
180  
181 my $params = $self->_signed_params({
182 api_key => $self->{api_key},
183 artist => $artist,
184 method => 'track.scrobble',
185 sk => $self->{api_session},
186 1 timestamp => $timestamp,
187 track => $track,
188 });
189  
190 1 return $self->_request($params, 'POST');
191 }
192  
193 42 ▶sub _require_value {
Subroutine _require_value: called CC=3 SCAR=11.0
194 42 my ($value, $name) = @_;
195  
196 42 ▶ croak "$name is required" unless defined $value && length $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and length $value 9 33 42
Condition100%3/3
defined $value and length $value
!ll&&!rl&&r
7233
MC/DC100%2/2
defined $value and length $value
defined $value length $value
Truth table100%3/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
197  
198 33 return $value;
199 }
200  
201 =head1 NAME
202  
203 Audio::Scrobbler2 - Interface to the Last.fm scrobbling API
204  
205 =head1 SYNOPSIS
206  
207 use Audio::Scrobbler2;
208  
209 my $scrobbler = Audio::Scrobbler2->new($api_key, $api_secret);
210 my $api_token = $scrobbler->auth_getToken;
211  
212 # Ask the user to authorize the token:
213 # https://www.last.fm/api/auth/?api_key=$api_key&token=$api_token
214 my $api_session = $scrobbler->auth_getSession;
215  
216 $scrobbler->track_updateNowPlaying('Artist Name', 'Track Name');
217 $scrobbler->track_scrobble('Artist Name', 'Track Name', $started_at);
218  
219 =head1 DESCRIPTION
220  
221 Audio::Scrobbler2 implements the desktop authentication flow and the two
222 Last.fm track submission methods needed by a basic scrobbler. Requests use
223 HTTPS, UTF-8 form encoding, and verified TLS certificates.
224  
225 All validation, transport, HTTP, JSON, and Last.fm API failures throw an
226 exception. The module does not retry requests because retrying a submission
227 could create a duplicate scrobble.
228  
229 =head1 METHODS
230  
231 =head2 new
232  
233 my $scrobbler = Audio::Scrobbler2->new($api_key, $api_secret);
234  
235 Creates a client. Both credentials are required.
236  
237 =head2 auth_getToken
238  
239 my $token = $scrobbler->auth_getToken;
240  
241 Requests and stores an unauthorized desktop application token.
242  
243 =head2 auth_getSession
244  
245 my $session_key = $scrobbler->auth_getSession;
246  
247 Exchanges the authorized token for a session key and stores it.
248  
249 =head2 set_session_key
250  
251 $scrobbler->set_session_key($session_key);
252  
253 Stores an existing non-empty session key.
254  
255 =head2 track_updateNowPlaying
256  
257 my $response = $scrobbler->track_updateNowPlaying($artist, $track);
258  
259 Updates the currently playing track and returns the decoded Last.fm response.
260  
261 =head2 track_scrobble
262  
263 my $response = $scrobbler->track_scrobble(
264 $artist,
265 $track,
266 $started_at,
267 );
268  
269 Scrobbles a track and returns the decoded Last.fm response. C<$started_at>
270 must be a positive integer Unix timestamp for when playback started.
271  
272 =head1 MIGRATING FROM 0.05
273  
274 Version 1.00 requires a playback start timestamp in C<track_scrobble>. All
275 failures now throw exceptions instead of returning C<0> or inconsistent error
276 structures.
277  
278 =head1 AUTHOR
279  
280 Pier Dolique (L<Perdolique|https://github.com/Perdolique>),
281 C<baget@cpan.org>
282  
283 CPAN ID: BAGET
284  
285 =head1 COPYRIGHT AND LICENSE
286  
287 Copyright 2012-2026 Pier Dolique.
288  
289 This software is free software; you can redistribute it and/or modify it under
290 the same terms as Perl itself.
291  
292 =cut
293  
294 3 1;
↑ Summary