Spotify.pm

filter: Statementstmt 50.6% 155 / 306 Branchbran 16.6% 27 / 162 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 71.1% 42 / 59 Podpod n/a totaltotal 42.5% 224 / 527 CC 83 SCAR 25.5
CC
83
Cov
39%
user_playlist_add_tracks
46.5
search
45.9
user_playlist_create
33.7
CRAP 12.8
↑ Summary Spotify/Util.pm »
1 package WebService::Spotify;
2 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Method::Signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = '1.003';
10  
11 1 has 'prefix' => ( is => 'rw', default => 'https://api.spotify.com/v1/' );
12 1 has 'auth' => ( is => 'rw' );
13 1 has 'trace' => ( is => 'rw', default => 0 );
14  
15 has 'user_agent' => (
16 is => 'rw',
17 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
18 1 my $ua = LWP::UserAgent->new;
19 1 $ua->agent("WebService::Spotify/$VERSION");
20 1 return $ua;
21 }
22 1 );
23  
24 1 ▶method get ($method, %args) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine get: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ > 0 0 4 4
25 4 my $uri = $self->_uri( $method, %args );
26 4 my $headers = $self->_auth_headers;
27 4 my $response = $self->user_agent->get( $uri->as_string, %$headers );
28
29 4 $self->_log("GET", $uri->as_string);
30 4 $self->_log("RESP", $response->content);
31  
32 4 ▶ return $response->content ? from_json($response->content) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$response->content ? : 4 0 4
33 }
34  
35 0 ▶method post ($method, $payload, %args) {
Subroutine BEGIN: called CC=1 SCAR=4.6
Subroutine post: not called CC=4 SCAR=27.8
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
unless scalar @_ > 1 0 0 0
36 0 my $uri = $self->_uri( $method, %args );
37 0 my $headers = $self->_auth_headers;
38 0 $headers->{'Content-Type'} = 'application/json';
39 0 my $response = $self->user_agent->post( $uri->as_string, %$headers, Content => to_json($payload) );
40
41 0 $self->_log("POST", $uri->as_string);
42 0 $self->_log("HEAD", Dumper $headers);
43 0 $self->_log("DATA", Dumper $payload);
44 0 $self->_log("RESP", $response->content);
45  
46 0 ▶ return $response->content ? from_json($response->content) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$response->content ? : 0 0 0
47 }
48  
49 0 ▶method put ($method, $payload, %args) {
Subroutine BEGIN: called CC=1 SCAR=4.6
Subroutine put: not called CC=4 SCAR=27.8
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
unless scalar @_ > 1 0 0 0
50 0 my $uri = $self->_uri( $method, %args );
51 0 my $headers = $self->_auth_headers;
52 0 $headers->{'Content-Type'} = 'application/json';
53 0 my $response = $self->user_agent->put( $uri->as_string, %$headers, Content => to_json($payload) );
54  
55 0 $self->_log("PUT", $uri->as_string);
56 0 $self->_log("HEAD", Dumper $headers);
57 0 $self->_log("DATA", Dumper $payload);
58 0 $self->_log("RESP", $response->content);
59  
60 0 ▶ return $response->content ? from_json($response->content) : $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
$response->content ? : 0 0 0
61 }
62  
63 0 ▶method next ($result) {
Subroutine BEGIN: called CC=1 SCAR=4.1
Subroutine next: not called CC=4 SCAR=26.2
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
if scalar @_ > 1 0 0 0
64 0 ▶ return $self->get($result->{next}) if $result->{next};
Branch: 1 branch
BranchTrueFalseTotal
if $result->{"next"} 0 0 0
65 }
66  
67 0 ▶method previous ($result) {
Subroutine BEGIN: called CC=1 SCAR=4.1
Subroutine previous: not called CC=4 SCAR=26.2
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
if scalar @_ > 1 0 0 0
68 0 ▶ return $self->get($result->{previous}) if $result->{previous};
Branch: 1 branch
BranchTrueFalseTotal
if $result->{"previous"} 0 0 0
69 }
70  
71 0 ▶method track ($track) {
Subroutine BEGIN: called CC=1 SCAR=4.1
Subroutine track: not called CC=3 SCAR=21.1
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
if scalar @_ > 1 0 0 0
72 0 my $track_id = $self->_get_id('track', $track);
73 0 return $self->get("tracks/$track_id");
74 }
75  
76 0 ▶method tracks ($tracks) {
Subroutine BEGIN: called CC=1 SCAR=4.1
Subroutine tracks: not called CC=3 SCAR=21.3
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
if scalar @_ > 1 0 0 0
77 0 my @track_ids = map { $self->_get_id('track', $_) } @$tracks;
78 0 return $self->get('tracks/?ids=' . join(',', @track_ids));
79 }
80  
81 1 ▶method artist ($artist) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine artist: called CC=3 SCAR=11.1
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 1 1
if scalar @_ > 1 0 1 1
82 1 my $artist_id = $self->_get_id('artist', $artist);
83 1 return $self->get("artists/$artist_id");
84 }
85  
86 1 ▶method artists ($artists) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine artists: called CC=3 SCAR=11.1
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 1 1
if scalar @_ > 1 0 1 1
87 1 my @artist_ids = map { $self->_get_id('artist', $_) } @$artists;
88 1 return $self->get('artists/?ids=' . join(',', @artist_ids));
89 }
90  
91 1 ▶method artist_albums ($artist, :$album_type, :$country, :$limit = 20, :$offset = 0) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine artist_albums: called CC=8 SCAR=21.4
Branch: 5 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 1 1
exists $args{'limit'} ? : 0 1 1
exists $args{'offset'} ? : 0 1 1
if keys %args 0 1 1
if scalar @_ > 9 0 1 1
92 1 my $artist_id = $self->_get_id('artist', $artist);
93 1 my %options;
94 1 ▶ $options{album_type} = $album_type if $album_type;
Branch: 1 branch
BranchTrueFalseTotal
if $album_type 0 1 1
95 1 ▶ $options{country} = $country if $country;
Branch: 1 branch
BranchTrueFalseTotal
if $country 0 1 1
96 1 return $self->get("artists/$artist_id/albums", %options, limit => $limit, offset => $offset);
97 }
98  
99 1 ▶method artist_top_tracks ($artist, :$country = 'US') {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine artist_top_tracks: called CC=5 SCAR=16.5
Branch: 4 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 1 1
exists $args{'country'} ? : 0 1 1
if keys %args 0 1 1
if scalar @_ > 3 0 1 1
100 1 my $artist_id = $self->_get_id('artist', $artist);
101 1 return $self->get("artists/$artist_id/top-tracks", country => $country);
102 }
103  
104 0 ▶method album ($album) {
Subroutine BEGIN: called CC=1 SCAR=4.1
Subroutine album: not called CC=3 SCAR=21.1
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
if scalar @_ > 1 0 0 0
105 0 my $album_id = $self->_get_id('album', $album);
106 0 return $self->get("albums/$album_id");
107 }
108  
109 0 ▶method album_tracks ($album) {
Subroutine BEGIN: called CC=1 SCAR=4.1
Subroutine album_tracks: not called CC=3 SCAR=21.1
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
if scalar @_ > 1 0 0 0
110 0 my $album_id = $self->_get_id('album', $album);
111 0 return $self->get("albums/$album_id/tracks");
112 }
113  
114 0 ▶method albums ($albums) {
Subroutine BEGIN: called CC=1 SCAR=4.1
Subroutine albums: not called CC=3 SCAR=21.3
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
if scalar @_ > 1 0 0 0
115 0 my @album_ids = map { $self->_get_id('album', $_) } @$albums;
116 0 return $self->get('albums/?ids=' . join(',', @album_ids));
117 }
118  
119 0 ▶method search ($q, :$limit = 10, :$offset = 0, :$type = 'track') {
Subroutine BEGIN: called CC=1 SCAR=5.8
Subroutine search: not called CC=7 SCAR=38.3
Branch: 6 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
exists $args{'limit'} ? : 0 0 0
exists $args{'offset'} ? : 0 0 0
exists $args{'type'} ? : 0 0 0
if keys %args 0 0 0
if scalar @_ > 7 0 0 0
120 0 return $self->get('search', q => $q, limit => $limit, offset => $offset, type => $type);
121 }
122  
123 0 ▶method user ($user_id) {
Subroutine BEGIN: called CC=1 SCAR=4.1
Subroutine user: not called CC=3 SCAR=20.7
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
if scalar @_ > 1 0 0 0
124 0 return $self->get("users/$user_id");
125 }
126  
127 0 ▶method user_playlists ($user_id) {
Subroutine BEGIN: called CC=1 SCAR=4.1
Subroutine user_playlists: not called CC=3 SCAR=20.7
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
if scalar @_ > 1 0 0 0
128 0 return $self->get("users/$user_id/playlists");
129 }
130  
131 0 ▶method user_playlist ($user_id, :$playlist_id, :$fields) {
Subroutine BEGIN: called CC=1 SCAR=5.3
Subroutine user_playlist: not called CC=5 SCAR=31.7
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
if keys %args 0 0 0
if scalar @_ > 5 0 0 0
132 0 ▶ my $method = $playlist_id ? "playlists/$playlist_id" : "starred";
Branch: 1 branch
BranchTrueFalseTotal
$playlist_id ? : 0 0 0
133 0 return $self->get("users/$user_id/$method", fields => $fields);
134 }
135  
136 0 ▶method user_playlist_create ($user_id, $name, :$public = 1) {
Subroutine BEGIN: called CC=1 SCAR=5.6
Subroutine user_playlist_create: not called CC=6 SCAR=35.2
Branch: 5 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
unless scalar @_ > 1 0 0 0
exists $args{'public'} ? : 0 0 0
if keys %args 0 0 0
if scalar @_ > 4 0 0 0
137 0 my $data = { 'name' => $name, 'public' => $public };
138 0 return $self->post("users/$user_id/playlists", $data);
139 }
140  
141 0 ▶method user_playlist_add_tracks ($user_id, $playlist_id, $tracks, :$position) {
Subroutine BEGIN: called CC=1 SCAR=5.7
Subroutine user_playlist_add_tracks: not called CC=7 SCAR=38.4
Branch: 5 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
unless scalar @_ > 1 0 0 0
unless scalar @_ > 2 0 0 0
if keys %args 0 0 0
if scalar @_ > 5 0 0 0
142 0 my %options;
143 0 ▶ $options{position} = $position if $position;
Branch: 1 branch
BranchTrueFalseTotal
if $position 0 0 0
144 0 return $self->post("users/$user_id/playlists/$playlist_id/tracks", $tracks, %options);
145 }
146  
147 0 ▶method user_playlist_replace_tracks ($user_id, $playlist_id, $tracks) {
Subroutine BEGIN: called CC=1 SCAR=5.3
Subroutine user_playlist_replace_tracks: not called CC=5 SCAR=31.3
Branch: 4 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
unless scalar @_ > 1 0 0 0
unless scalar @_ > 2 0 0 0
if scalar @_ > 3 0 0 0
148 0 return $self->put("users/$user_id/playlists/$playlist_id/tracks", { 'uris' => $tracks });
149 }
150  
151 0 ▶method me {
Subroutine BEGIN: called CC=1 SCAR=2.6
Subroutine me: not called CC=2 SCAR=12.4
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 0 0
152 0 return $self->get('me/');
153 }
154  
155 1 ▶method _log (@strings) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _log: called CC=2 SCAR=7.0
156 8 ▶ print join(' ', @strings) . "\n" if $self->trace;
Branch: 1 branch
BranchTrueFalseTotal
if $self->trace 0 8 8
157 }
158  
159 1 ▶method _auth_headers {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _auth_headers: called CC=3 SCAR=11.3
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 4 4
160 4 ▶ return $self->auth ? { 'Authorization' => 'Bearer ' . $self->auth } : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->auth ? : 0 4 4
161 }
162  
163 1 ▶method _uri ($method, %args) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _uri: called CC=4 SCAR=14.0
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ > 0 0 4 4
164 4 ▶ my $base_uri = $method =~ /^http/ ? $method : $self->prefix . $method;
Branch: 1 branch
BranchTrueFalseTotal
$method =~ /^http/ ? : 0 4 4
165  
166 4 my $uri = URI->new( $base_uri );
167 4 $uri->query_param( $_, $args{$_} ) for keys %args;
168  
169 4 return $uri;
170 }
171  
172 1 ▶method _get_id ($type, $id) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine _get_id: called CC=8 SCAR=26.3
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 5 5
unless scalar @_ > 1 0 5 5
if scalar @_ > 2 0 5 5
173 5 my @fields = split /:/, $id;
174 5 ▶ if (@fields == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields == 3) 5 0 5
175 5 ▶ warn "expected id of type $type but found type $fields[2] id" if $type ne $fields[1];
Branch: 1 branch
BranchTrueFalseTotal
if $type ne $fields[1] 0 5 5
176 5 return $fields[2];
177 }
178  
179 0 @fields = split /\//, $id;
180 0 ▶ if (@fields >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields >= 3) 0 0 0
181 0 ▶ warn "expected id of type $type but found type $fields[-2] id" if $type ne $fields[-2];
Branch: 1 branch
BranchTrueFalseTotal
if $type ne $fields[-2] 0 0 0
182 0 return $fields[-1];
183 }
184  
185 0 return $id;
186 }
187  
188 1 1;
189  
190 =head1 NAME
191  
192 WebService::Spotify - A simple interface to the L<Spotify Web API|https://developer.spotify.com/web-api/>
193  
194 =head1 SYNOPSIS
195  
196 my $spotify = WebService::Spotify->new;
197 my $results = $spotify->search('weezer', limit => 20);
198 say $_->{name} for @{$results->{tracks}->{items}};
199
200  
201 More examples can be found in the /eg directory.
202  
203  
204 =head1 DESCRIPTION
205  
206 =head1 METHODS
207  
208 See L<Method::Signatures> for details of the parameter spec used below.
209  
210 Refer to the L<Spotify API documentation|https://developer.spotify.com/spotify-web-api/> for details on the methods and parameters.
211  
212 Methods that take item IDs (such as the track, album and artist methods) accept URN, URL or simple ID types. The following 3 ids are all acceptable IDs:
213  
214 http://open.spotify.com/track/3HfB5hBU0dmBt8T0iCmH42
215 spotify:track:3HfB5hBU0dmBt8T0iCmH42
216 3HfB5hBU0dmBt8T0iCmH42
217  
218 The following methods are supported:
219  
220 =head2 album ($album_id)
221  
222 returns a single album given the album's ID, URN or URL
223  
224 =head2 album_tracks ($album_id)
225  
226 Get Spotify catalog information about an album's tracks
227  
228 =head2 albums (\@albums)
229  
230 returns a list of albums given the album IDs, URNs, or URLs
231  
232 =head2 artist ($artist_id)
233  
234 returns a single artist given the artist's ID, URN or URL
235  
236 =head2 artist_albums ($artist, :$album_type, :$country, :$limit = 20, :$offset = 0)
237  
238 Get Spotify catalog information about an artist’s albums
239  
240 =head2 artist_top_tracks ($artist, :$country = 'US')
241  
242 Get Spotify catalog information about an artist’s top 10 tracks by country.
243  
244 =head2 artists (\@artists)
245  
246 returns a list of artists given the artist IDs, URNs, or URLs
247  
248 =head2 me()
249  
250 returns info about me
251  
252 =head2 next ($result)
253  
254 returns the next result given a result
255  
256 =head2 previous ($result)
257  
258 returns the previous result given a result
259  
260 =head2 search ($q, limit => 10, offset => 0, type => 'track')
261  
262 searches for an item
263  
264 =head2 track ($track_id)
265  
266 returns a single track given the track's ID, URN or URL
267  
268 =head2 tracks (\@track_ids)
269  
270 returns a list of tracks given the track IDs, URNs, or URLs
271  
272 =head2 user ($user_id)
273  
274 Gets basic profile information about a Spotify User
275  
276 =head2 user_playlist ($user_id, :$playlist_id, :$fields)
277  
278 Gets playlist of a user
279  
280 =head2 user_playlist_add_tracks ($user_id, $playlist_id, $tracks, :$position)
281  
282 Adds tracks to a playlist
283  
284 =head2 user_playlist_create ($user_id, $name, :$public = 1)
285  
286 Creates a playlist for a user
287  
288 =head2 user_playlists ($user_id)
289  
290 Gets playlists of a user
291  
292  
293 =head1 AUTHOR
294  
295 Nick Langridge <nickl@cpan.org>
296  
297 =head1 CREDITS
298  
299 This module was ported from L<Spotipy|https://github.com/plamere/spotipy>, a Python wrapper for the Spotify Web API
300  
301 =head1 LICENSE
302  
303 This module is free software; you can redistribute it or
304 modify it under the same terms as Perl itself.
305  
306 =head1 SEE ALSO
307  
308 L<WebService::EchoNest> - wrapper for the EchoNest API which has some integration with the Spotify Web API
309  
310 L<Net::Spotify> - wrapper for the old Spotify metadata API
311  
↑ Summary Spotify/Util.pm »