line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WebService::LastFM::TrackInfo; |
2
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:GENE'; |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
# ABSTRACT: Access to *.getInfo slices of the last.fm API |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.0206'; |
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
1957
|
use strictures 2; |
|
1
|
|
|
|
|
1343
|
|
|
1
|
|
|
|
|
36
|
|
9
|
1
|
|
|
1
|
|
187
|
use Carp qw(croak); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
40
|
|
10
|
1
|
|
|
1
|
|
5
|
use Mojo::UserAgent (); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
14
|
|
11
|
1
|
|
|
1
|
|
4
|
use Mojo::JSON qw(decode_json); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
35
|
|
12
|
1
|
|
|
1
|
|
4
|
use Mojo::URL (); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
11
|
|
13
|
1
|
|
|
1
|
|
441
|
use Moo; |
|
1
|
|
|
|
|
8719
|
|
|
1
|
|
|
|
|
4
|
|
14
|
1
|
|
|
1
|
|
1720
|
use Try::Tiny; |
|
1
|
|
|
|
|
1092
|
|
|
1
|
|
|
|
|
48
|
|
15
|
1
|
|
|
1
|
|
430
|
use namespace::clean; |
|
1
|
|
|
|
|
7900
|
|
|
1
|
|
|
|
|
5
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
has api_key => ( |
19
|
|
|
|
|
|
|
is => 'ro', |
20
|
|
|
|
|
|
|
required => 1, |
21
|
|
|
|
|
|
|
); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
has method => ( |
25
|
|
|
|
|
|
|
is => 'ro', |
26
|
|
|
|
|
|
|
default => sub { 'track' }, |
27
|
|
|
|
|
|
|
); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
has format => ( |
31
|
|
|
|
|
|
|
is => 'ro', |
32
|
|
|
|
|
|
|
default => sub { 'json' }, |
33
|
|
|
|
|
|
|
); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
has base => ( |
37
|
|
|
|
|
|
|
is => 'rw', |
38
|
|
|
|
|
|
|
default => sub { 'http://ws.audioscrobbler.com' }, |
39
|
|
|
|
|
|
|
); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
has version => ( |
43
|
|
|
|
|
|
|
is => 'ro', |
44
|
|
|
|
|
|
|
default => sub { '2.0' }, |
45
|
|
|
|
|
|
|
); |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
has ua => ( |
49
|
|
|
|
|
|
|
is => 'rw', |
50
|
|
|
|
|
|
|
default => sub { Mojo::UserAgent->new }, |
51
|
|
|
|
|
|
|
); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub fetch { |
55
|
4
|
|
|
4
|
1
|
26579
|
my ( $self, %args ) = @_; |
56
|
|
|
|
|
|
|
|
57
|
4
|
100
|
|
|
|
18
|
croak 'No artist provided' unless $args{artist}; |
58
|
3
|
100
|
100
|
|
|
28
|
croak 'No track provided' if $self->method eq 'track' && !$args{track}; |
59
|
2
|
100
|
66
|
|
|
18
|
croak 'No album provided' if $self->method eq 'album' && !$args{album}; |
60
|
|
|
|
|
|
|
|
61
|
1
|
|
|
|
|
9
|
my $url = Mojo::URL->new($self->base) |
62
|
|
|
|
|
|
|
->path($self->version) |
63
|
|
|
|
|
|
|
->query( |
64
|
|
|
|
|
|
|
%args, |
65
|
|
|
|
|
|
|
api_key => $self->api_key, |
66
|
|
|
|
|
|
|
method => $self->method . '.getInfo', |
67
|
|
|
|
|
|
|
format => $self->format, |
68
|
|
|
|
|
|
|
); |
69
|
|
|
|
|
|
|
|
70
|
1
|
|
|
|
|
479
|
my $tx = $self->ua->get($url); |
71
|
|
|
|
|
|
|
|
72
|
1
|
|
|
|
|
21553
|
my $data = $self->_handle_response($tx); |
73
|
|
|
|
|
|
|
|
74
|
1
|
|
|
|
|
8
|
return $data; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub _handle_response { |
78
|
1
|
|
|
1
|
|
4
|
my ($self, $tx) = @_; |
79
|
|
|
|
|
|
|
|
80
|
1
|
|
|
|
|
2
|
my $data; |
81
|
|
|
|
|
|
|
|
82
|
1
|
|
|
|
|
8
|
my $res = $tx->result; |
83
|
|
|
|
|
|
|
|
84
|
1
|
50
|
|
|
|
24
|
if ( $res->is_success ) { |
85
|
1
|
|
|
|
|
16
|
my $body = $res->body; |
86
|
|
|
|
|
|
|
|
87
|
1
|
50
|
|
|
|
23
|
if ($self->format eq 'json') { |
88
|
|
|
|
|
|
|
try { |
89
|
1
|
|
|
1
|
|
113
|
$data = decode_json($body); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
catch { |
92
|
0
|
|
|
0
|
|
0
|
croak $body, "\n"; |
93
|
1
|
|
|
|
|
10
|
}; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
else { |
96
|
0
|
|
|
|
|
0
|
$data = $body; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
else { |
100
|
0
|
|
|
|
|
0
|
croak 'Connection error: ', $res->message; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
1
|
|
|
|
|
128
|
return $data; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
1; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
__END__ |