| 1 | package WebService::Audioscrobbler::Artist; | ||||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use CLASS; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 1 | ▶ | use base 'WebService::Audioscrobbler::Base'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | =head1 NAME | ||||||||||||
| 9 | |||||||||||||
| 10 | WebService::Audioscrobbler::Artist - An object-oriented interface to the Audioscrobbler WebService API | ||||||||||||
| 11 | |||||||||||||
| 12 | =cut | ||||||||||||
| 13 | |||||||||||||
| 14 | 1 | our $VERSION = '0.08'; | |||||||||||
| 15 | |||||||||||||
| 16 | # postfix related accessors | ||||||||||||
| 17 | 1 | CLASS->mk_classaccessor("base_resource_path" => "artist"); | |||||||||||
| 18 | |||||||||||||
| 19 | # similar artists related accessors | ||||||||||||
| 20 | 1 | CLASS->mk_classaccessor("similar_artists_postfix" => "similar.xml"); | |||||||||||
| 21 | 1 | CLASS->mk_classaccessor("similar_artists_class" => "WebService::Audioscrobbler::SimilarArtist"); | |||||||||||
| 22 | |||||||||||||
| 23 | # change the field used to sort stuff | ||||||||||||
| 24 | 1 | CLASS->tracks_sort_field('reach'); | |||||||||||
| 25 | |||||||||||||
| 26 | # requiring stuff | ||||||||||||
| 27 | 1 | ▶ | CLASS->similar_artists_class->require or die($@); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 28 | 1 | ▶ | CLASS->tracks_class->require or die($@); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 29 | 1 | ▶ | CLASS->tags_class->require or die($@); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 30 | |||||||||||||
| 31 | # object accessors | ||||||||||||
| 32 | 1 | CLASS->mk_accessors(qw/name mbid streamable picture_url/); | |||||||||||
| 33 | |||||||||||||
| 34 | =head1 SYNOPSIS | ||||||||||||
| 35 | |||||||||||||
| 36 | This module implements an object oriented abstraction of an artist within the | ||||||||||||
| 37 | Audioscrobbler database. | ||||||||||||
| 38 | |||||||||||||
| 39 | use WebService::Audioscrobbler; | ||||||||||||
| 40 | |||||||||||||
| 41 | my $ws = WebService::Audioscrobbler->new; | ||||||||||||
| 42 | |||||||||||||
| 43 | # get an object for artist named 'foo' | ||||||||||||
| 44 | my $artist = $ws->artist('foo'); | ||||||||||||
| 45 | |||||||||||||
| 46 | # fetch artists similar to 'foo' | ||||||||||||
| 47 | my @similar = $artist->similar_artists; | ||||||||||||
| 48 | |||||||||||||
| 49 | print "Artists similar to: " . $artist->name . "\n"; | ||||||||||||
| 50 | |||||||||||||
| 51 | # prints each one of their names | ||||||||||||
| 52 | for my $similar (@similar) { | ||||||||||||
| 53 | print $similar->name . "\n"; | ||||||||||||
| 54 | } | ||||||||||||
| 55 | |||||||||||||
| 56 | # retrieves tracks from 'foo' | ||||||||||||
| 57 | my @tracks = $artist->tracks; | ||||||||||||
| 58 | |||||||||||||
| 59 | # retrieves tags associated with 'foo' | ||||||||||||
| 60 | my @tags = $artist->tags; | ||||||||||||
| 61 | |||||||||||||
| 62 | This module inherits from L<WebService::Audioscrobbler::Base>. | ||||||||||||
| 63 | |||||||||||||
| 64 | =head1 FIELDS | ||||||||||||
| 65 | |||||||||||||
| 66 | =head2 C<name> | ||||||||||||
| 67 | |||||||||||||
| 68 | The name of a given artist as provided when constructing the object. | ||||||||||||
| 69 | |||||||||||||
| 70 | =head2 C<mbid> | ||||||||||||
| 71 | |||||||||||||
| 72 | MusicBrainz ID as provided by the Audioscrobbler database. | ||||||||||||
| 73 | |||||||||||||
| 74 | =head2 C<picture_url> | ||||||||||||
| 75 | |||||||||||||
| 76 | URI object pointing to the location of the artist's picture, if available. | ||||||||||||
| 77 | |||||||||||||
| 78 | =head2 C<streamable> | ||||||||||||
| 79 | |||||||||||||
| 80 | Flag indicating whether the artist has streamable content available. | ||||||||||||
| 81 | |||||||||||||
| 82 | =cut | ||||||||||||
| 83 | |||||||||||||
| 84 | =head1 METHODS | ||||||||||||
| 85 | |||||||||||||
| 86 | =cut | ||||||||||||
| 87 | |||||||||||||
| 88 | =head2 C<new($artist_name, $data_fetcher)> | ||||||||||||
| 89 | |||||||||||||
| 90 | =head2 C<new(\%fields)> | ||||||||||||
| 91 | |||||||||||||
| 92 | Creates a new object using either the given C<$artist_name> or the C<\%fields> | ||||||||||||
| 93 | hashref. The data fetcher object is a mandatory parameter and must | ||||||||||||
| 94 | be provided either as the second parameter or inside the C<\%fields> hashref. | ||||||||||||
| 95 | |||||||||||||
| 96 | =cut | ||||||||||||
| 97 | |||||||||||||
| 98 | 0 | ▶ | sub new { | ||||||||||
|
Subroutine
new: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||
| 99 | 0 | my $class = shift; | |||||||||||
| 100 | 0 | my ($name_or_fields, $data_fetcher) = @_; | |||||||||||
| 101 | |||||||||||||
| 102 | 0 | ▶ | my $self = $class->SUPER::new( | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 103 | ref $name_or_fields eq 'HASH' ? | ||||||||||||
| 104 | $name_or_fields : { name => $name_or_fields, data_fetcher => $data_fetcher } | ||||||||||||
| 105 | ); | ||||||||||||
| 106 | |||||||||||||
| 107 | 0 | ▶ | $self->croak("No data fetcher provided") | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 108 | unless $self->data_fetcher; | ||||||||||||
| 109 | |||||||||||||
| 110 | 0 | ▶ | unless (defined $self->name) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 111 | 0 | ▶ | if (defined $self->{content}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 112 | 0 | $self->name($self->{content}); | |||||||||||
| 113 | } | ||||||||||||
| 114 | else { | ||||||||||||
| 115 | 0 | $class->croak("Can't create artist without a name"); | |||||||||||
| 116 | } | ||||||||||||
| 117 | } | ||||||||||||
| 118 | |||||||||||||
| 119 | 0 | return $self; | |||||||||||
| 120 | } | ||||||||||||
| 121 | |||||||||||||
| 122 | =head2 C<similar_artists([$filter])> | ||||||||||||
| 123 | |||||||||||||
| 124 | =head2 C<related_artists([$filter])> | ||||||||||||
| 125 | |||||||||||||
| 126 | =head2 C<artists([$filter])> | ||||||||||||
| 127 | |||||||||||||
| 128 | Retrieves similar artists from the Audioscrobbler database. $filter can be used | ||||||||||||
| 129 | to limit artist with a low similarity index (ie. artists which have a similarity | ||||||||||||
| 130 | index lower than $filter won't be returned). | ||||||||||||
| 131 | |||||||||||||
| 132 | Returns either a list of artists or a reference to an array of artists when called | ||||||||||||
| 133 | in list context or scalar context, respectively. The artists are returned as | ||||||||||||
| 134 | L<WebService::Audioscrobbler::SimilarArtist> objects by default. | ||||||||||||
| 135 | |||||||||||||
| 136 | =cut | ||||||||||||
| 137 | |||||||||||||
| 138 | 1 | *related_artists = *artists = \&similar_artists; | |||||||||||
| 139 | |||||||||||||
| 140 | 0 | ▶ | sub similar_artists { | ||||||||||
|
Subroutine
similar_artists: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||
| 141 | 0 | my $self = shift; | |||||||||||
| 142 | 0 | ▶ | my $filter = shift || 1; | ||||||||||
|
Condition0%0/2
shift() || 1
MC/DC0%0/1
shift() || 1
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||
| 143 | |||||||||||||
| 144 | 0 | my $data = $self->fetch_data($self->similar_artists_postfix); | |||||||||||
| 145 | |||||||||||||
| 146 | 0 | $self->load_fields($data); | |||||||||||
| 147 | |||||||||||||
| 148 | 0 | my @artists; | |||||||||||
| 149 | |||||||||||||
| 150 | # check if we've got any similar artists | ||||||||||||
| 151 | 0 | ▶ | if (ref $data->{artist} eq 'ARRAY') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 152 | |||||||||||||
| 153 | 0 | shift @{$data->{artist}}; | |||||||||||
| 154 | |||||||||||||
| 155 | @artists = map { | ||||||||||||
| 156 | 0 | my $artist = $_; | |||||||||||
| 157 | $self->similar_artists_class->new({ | ||||||||||||
| 158 | picture_url => URI->new($_->{image}), | ||||||||||||
| 159 | related_to => $self, | ||||||||||||
| 160 | data_fetcher => $self->data_fetcher, | ||||||||||||
| 161 | 0 | map {$_ => $artist->{$_}} qw/name streamable mbid match/ | |||||||||||
| 162 | }) | ||||||||||||
| 163 | 0 | } grep { $_->{match} >= $filter } @{$data->{artist}}; | |||||||||||
| 164 | |||||||||||||
| 165 | } | ||||||||||||
| 166 | |||||||||||||
| 167 | 0 | ▶ | return wantarray ? @artists : \@artists; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 168 | } | ||||||||||||
| 169 | |||||||||||||
| 170 | =head2 C<tracks> | ||||||||||||
| 171 | |||||||||||||
| 172 | Retrieves the artist's top tracks as available on Audioscrobbler's database. | ||||||||||||
| 173 | |||||||||||||
| 174 | Returns either a list of tracks or a reference to an array of tracks when called | ||||||||||||
| 175 | in list context or scalar context, respectively. The tracks are returned as | ||||||||||||
| 176 | L<WebService::Audioscrobbler::Track> objects by default. | ||||||||||||
| 177 | |||||||||||||
| 178 | =cut | ||||||||||||
| 179 | |||||||||||||
| 180 | =head2 C<tags> | ||||||||||||
| 181 | |||||||||||||
| 182 | Retrieves the artist's top tags as available on Audioscrobbler's database. | ||||||||||||
| 183 | |||||||||||||
| 184 | Returns either a list of tags or a reference to an array of tags when called | ||||||||||||
| 185 | in list context or scalar context, respectively. The tags are returned as | ||||||||||||
| 186 | L<WebService::Audioscrobbler::Tag> objects by default. | ||||||||||||
| 187 | |||||||||||||
| 188 | =cut | ||||||||||||
| 189 | |||||||||||||
| 190 | =head2 C<load_fields(\%data)> | ||||||||||||
| 191 | |||||||||||||
| 192 | Loads artist fields from the hashref C<\%data>. | ||||||||||||
| 193 | |||||||||||||
| 194 | =cut | ||||||||||||
| 195 | |||||||||||||
| 196 | 0 | ▶ | sub load_fields { | ||||||||||
|
Subroutine
load_fields: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 197 | 0 | my $self = shift; | |||||||||||
| 198 | 0 | my $data = shift; | |||||||||||
| 199 | |||||||||||||
| 200 | 0 | $self->streamable($data->{streamable}); | |||||||||||
| 201 | |||||||||||||
| 202 | 0 | ▶ | $self->picture_url(URI->new($data->{picture})) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 203 | unless $self->picture_url; | ||||||||||||
| 204 | |||||||||||||
| 205 | $self->mbid($data->{mbid}) | ||||||||||||
| 206 | 0 | ▶ | unless $self->mbid; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 207 | } | ||||||||||||
| 208 | |||||||||||||
| 209 | =head2 C<resource_path> | ||||||||||||
| 210 | |||||||||||||
| 211 | Returns the URL from which other URLs used for fetching artist info will be | ||||||||||||
| 212 | derived from. | ||||||||||||
| 213 | |||||||||||||
| 214 | =cut | ||||||||||||
| 215 | |||||||||||||
| 216 | 0 | ▶ | sub resource_path { | ||||||||||
|
Subroutine
resource_path: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 217 | 0 | my $self = shift; | |||||||||||
| 218 | 0 | $self->uri_builder( $self->name ); | |||||||||||
| 219 | } | ||||||||||||
| 220 | |||||||||||||
| 221 | =head1 AUTHOR | ||||||||||||
| 222 | |||||||||||||
| 223 | Nilson Santos Figueiredo Junior, C<< <nilsonsfj at cpan.org> >> | ||||||||||||
| 224 | |||||||||||||
| 225 | =head1 COPYRIGHT & LICENSE | ||||||||||||
| 226 | |||||||||||||
| 227 | Copyright 2006-2007 Nilson Santos Figueiredo Junior, all rights reserved. | ||||||||||||
| 228 | |||||||||||||
| 229 | This program is free software; you can redistribute it and/or modify it | ||||||||||||
| 230 | under the same terms as Perl itself. | ||||||||||||
| 231 | |||||||||||||
| 232 | =cut | ||||||||||||
| 233 | |||||||||||||
| 234 | 1 | 1; # End of WebService::Audioscrobbler::Artist | |||||||||||