line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WebService::LastFM::SimilarArtists; |
2
|
2
|
|
|
2
|
|
59569
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
70
|
|
3
|
2
|
|
|
2
|
|
2204
|
use Cache::File; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
use Carp; |
5
|
|
|
|
|
|
|
use File::Path qw/mkpath/; |
6
|
|
|
|
|
|
|
use LWP::Simple qw(get $ua); |
7
|
|
|
|
|
|
|
use URI::Escape; |
8
|
|
|
|
|
|
|
use XML::Simple; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
13
|
|
|
|
|
|
|
my ($class, %parameters) = @_; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
my $self = bless ({}, ref ($class) || $class); |
16
|
|
|
|
|
|
|
my %options = ( |
17
|
|
|
|
|
|
|
min_match => 0.75, |
18
|
|
|
|
|
|
|
cache_time => '1 week', |
19
|
|
|
|
|
|
|
cache_dir => '/tmp/lastfm.cache', |
20
|
|
|
|
|
|
|
%parameters, |
21
|
|
|
|
|
|
|
); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
$self->{'_options'} = \%options; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# Check if the cache_dir exists |
26
|
|
|
|
|
|
|
if(!-d $self->{'_options'}->{'cache_dir'}) { |
27
|
|
|
|
|
|
|
eval { mkpath($self->{'_options'}->{'cache_dir'}) }; |
28
|
|
|
|
|
|
|
if($@) { |
29
|
|
|
|
|
|
|
Carp::croak("Couldn't create $self->{'_options'}->{'cache_dir'}:$@"); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
return $self; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub lookup { |
37
|
|
|
|
|
|
|
my ($self, $name) = @_; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
unless($name) { |
40
|
|
|
|
|
|
|
Carp::croak('No name supplied!'); |
41
|
|
|
|
|
|
|
return; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
my $cache = new Cache::File ( |
45
|
|
|
|
|
|
|
cache_root => $self->{'_options'}->{'cache_dir'}, |
46
|
|
|
|
|
|
|
); |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
my $filename = $name; |
49
|
|
|
|
|
|
|
$filename =~ s/\W//g; |
50
|
|
|
|
|
|
|
my @info; |
51
|
|
|
|
|
|
|
@info = @{$cache->thaw($filename)} |
52
|
|
|
|
|
|
|
if(ref $cache->thaw($filename) eq 'ARRAY'); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
unless(@info) { |
55
|
|
|
|
|
|
|
my $data = get(sprintf("%s/%s/%s", |
56
|
|
|
|
|
|
|
'http://ws.audioscrobbler.com/1.0/artist', |
57
|
|
|
|
|
|
|
uri_escape($name), |
58
|
|
|
|
|
|
|
'similar.xml') |
59
|
|
|
|
|
|
|
); |
60
|
|
|
|
|
|
|
if($data) { |
61
|
|
|
|
|
|
|
my $xs = new XML::Simple(); |
62
|
|
|
|
|
|
|
my $x = $xs->XMLin($data); |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
if(ref $x->{'artist'} eq 'ARRAY') { |
65
|
|
|
|
|
|
|
foreach my $item (@{$x->{'artist'}}) { |
66
|
|
|
|
|
|
|
next unless ref $item eq 'HASH'; |
67
|
|
|
|
|
|
|
push @info, $item |
68
|
|
|
|
|
|
|
if($item->{'match'} >= $self->{'_options'}->{'min_match'}); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
$cache->freeze($filename, \@info, |
71
|
|
|
|
|
|
|
$self->{'_options'}->{'cache_time'}); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
} else { |
74
|
|
|
|
|
|
|
Carp::carp("Couldn't fetch XML for $name"); |
75
|
|
|
|
|
|
|
return (); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
return @info; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
#------------------------------------------------------------------------------# |
82
|
|
|
|
|
|
|
=head1 NAME |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
WebService::LastFM::SimilarArtists - Module to retrieve Related Artist |
85
|
|
|
|
|
|
|
information from audioscrobbler.net / last.fm |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head1 SYNOPSIS |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
use strict; |
90
|
|
|
|
|
|
|
use WebService::LastFM::SimilarArtists; |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
my $sa = WebService::LastFM::SimilarArtists->new( |
93
|
|
|
|
|
|
|
minmatch => 85, |
94
|
|
|
|
|
|
|
cache_time => '1 week', |
95
|
|
|
|
|
|
|
cache_dir => '/var/cache/lastfm' |
96
|
|
|
|
|
|
|
); |
97
|
|
|
|
|
|
|
my @artists = $sa->lookup('Hate Forest'); |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
if(@artists) { |
100
|
|
|
|
|
|
|
print "{'url'}\">$_->{'name'} ". |
101
|
|
|
|
|
|
|
"(match: $_->{'match'})\n" |
102
|
|
|
|
|
|
|
foreach(@artists); |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head1 DESCRIPTION |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
C retrieves Similiar Artists |
108
|
|
|
|
|
|
|
(Related Artists) information from L |
109
|
|
|
|
|
|
|
(L), based on your input. |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head2 METHODS |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head3 new |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
C creates a new WebService::LastFM::SimilarArtists object |
116
|
|
|
|
|
|
|
using the configuration passed to it. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head4 options |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=over 4 |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=item min_match |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
Specifies the minimal similarity match count that should be returned. |
125
|
|
|
|
|
|
|
Defaults to C<0.75>. |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=item cache_time |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
Specifies the time the query should be cached to prevent hammering |
130
|
|
|
|
|
|
|
the audioscrobbler website. Webmaster suggested one week, which is |
131
|
|
|
|
|
|
|
the default. |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
Time can be specified in the same manner as with L. It can |
134
|
|
|
|
|
|
|
be set using seconds since the epoch, or using a slightly more readable |
135
|
|
|
|
|
|
|
form of e.g. '1 week', '2 months', etc. Valid units are s, second, |
136
|
|
|
|
|
|
|
seconds, sec, m, minute, minutes, min, h, hour, hours, w, week, weeks, |
137
|
|
|
|
|
|
|
M, month, months, y, year and years. You can also specify an |
138
|
|
|
|
|
|
|
absolute time, such as '16 Nov 94 22:28:20' or any other time that |
139
|
|
|
|
|
|
|
Date::Parse can understand. |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=item cache_dir |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
Specifies the root directory for the caching. Defaults to |
144
|
|
|
|
|
|
|
C. Directories are created when |
145
|
|
|
|
|
|
|
needed. The module will C when it fails to create a |
146
|
|
|
|
|
|
|
directory. |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=back |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head3 lookup |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
Takes one argument, the band or artist to be queried. This |
153
|
|
|
|
|
|
|
method returns an array of anonymous hashes with the similar |
154
|
|
|
|
|
|
|
artists found (if any). |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=head2 RETURN VALUES |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
The array returned contains anonymous hashes with the following |
159
|
|
|
|
|
|
|
information: |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=over 4 |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=item name |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
The artist/band name |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=item mbid |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
The Musicbrainz ID (if available) |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=item match |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
The similarity match count (on a scale of 0 to 100) |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=item url |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
The URL to the artist's page at L |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=item streamable |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
Whether the artist is "streamable", meaning, whether the rightholders |
182
|
|
|
|
|
|
|
of artist's work have submitted tracks to L. |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
1 for true, 0 for false. |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=back |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head1 BUGS |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
Please report any found bugs to |
191
|
|
|
|
|
|
|
L or contact the author. |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=head1 AUTHOR |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
M. Blom, |
196
|
|
|
|
|
|
|
Eblom@cpan.orgE, |
197
|
|
|
|
|
|
|
L |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head1 COPYRIGHT |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
This program is free software; you can redistribute |
202
|
|
|
|
|
|
|
it and/or modify it under the same terms as Perl itself. |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
The full text of the license can be found in the |
205
|
|
|
|
|
|
|
LICENSE file included with this module. |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
B: The datafeed from audioscrobbler.net is for |
208
|
|
|
|
|
|
|
I. Please see |
209
|
|
|
|
|
|
|
L for |
210
|
|
|
|
|
|
|
more licensing information. |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=head1 SEE ALSO |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=over 4 |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=item * L |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=item * L |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=item * L |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
=item * L |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=item * L, L, L, L |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
=item * L |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
=back |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=cut |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
1; |