MusicBrainz.pm

filter: Statementstmt 98.7% 77 / 78 Branchbran 87.5% 21 / 24 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 8 / 8 Podpod 50.0% 1 / 2 totaltotal 95.8% 116 / 121 CC 17 SCAR 21.2
CC
17
Cov
96%
search
13.0
is_valid_subquery
5.0
BEGIN
1.0
CRAP 8.3
↑ Summary MusicBrainz/Request.pm »
1 package WebService::MusicBrainz;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use WebService::MusicBrainz::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 our $VERSION = '1.0.11';
8  
9 6 has 'request';
10 has valid_resources =>
11 6 ▶ sub { [ 'area', 'artist', 'discid', 'label', 'recording', 'release', 'release-group' ] };
Subroutine __ANON__: called CC=1 SCAR=0.0
12 4 ▶has relationships => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
13 4 my $rels = [
14 'area-rels', 'artist-rels', 'event-rels', 'instrument-rels',
15 'label-rels', 'place-rels', 'recording-rels', 'release-rels',
16 'release-group-rels', 'series-rels', 'url-rels', 'work-rels'
17 ];
18 4 return $rels;
19 6 };
20  
21 # inc subqueries
22 6 our %subquery_map = (
23 _modifiers => [qw(discids media isrcs artist-credits various-artists)],
24 _misc => [qw(aliases annotation tags ratings user-tags user-ratings genres user-genres)],
25 artist => [qw(recordings releases release-groups works)],
26 discid => [qw(artists artist-credits collections labels recordings release-groups)],
27 label => [qw(releases)],
28 recording => [qw(artists releases)],
29 release => [qw(artists collections labels recordings release-groups)],
30 'release-group' => [qw(artists releases)],
31 );
32  
33 5 ▶has search_fields_by_resource => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
34 5 my %search_fields;
35  
36 $search_fields{area} = [
37 5 'aid', 'alias', 'area', 'begin', 'comment', 'end', 'ended', 'sortname',
38 'iso', 'iso1', 'iso2', 'iso3', 'type'
39 ];
40 $search_fields{artist} = [
41 5 'area', 'beginarea', 'endarea', 'arid', 'artist', 'artistaccent',
42 'alias', 'begin', 'comment', 'country', 'end', 'ended',
43 'gender', 'ipi', 'sortname', 'tag', 'type'
44 ];
45 5 $search_fields{discid} = ['discid'];
46 $search_fields{label} = [
47 5 'alias', 'area', 'begin', 'code', 'comment', 'country',
48 'end', 'ended', 'ipi', 'label', 'labelaccent', 'laid',
49 'sortname', 'type', 'tag'
50 ];
51 $search_fields{recording} = [
52 5 'arid', 'artist', 'artistname', 'creditname',
53 'comment', 'country', 'date', 'dur',
54 'format', 'isrc', 'number', 'position',
55 'primarytype', 'puid', 'qdur', 'recording',
56 'recordingaccent', 'reid', 'release', 'rgid',
57 'rid', 'secondarytype', 'status', 'tid',
58 'trnum', 'tracks', 'tracksrelease', 'tag',
59 'type', 'video'
60 ];
61 5 $search_fields{'release-group'} = [
62 'arid', 'artist', 'comment', 'creditname',
63 'primarytype', 'rgid', 'releasegroup', 'releasegroupaccent',
64 'releases', 'release', 'reid', 'secondarytype',
65 'status', 'tag', 'type'
66 ];
67 $search_fields{release} = [
68 5 'arid', 'artist', 'artistname', 'asin',
69 'barcode', 'catno', 'comment', 'country',
70 'creditname', 'date', 'discids', 'discidsmedium',
71 'format', 'laid', 'label', 'lang',
72 'mediums', 'primarytype', 'puid', 'quality',
73 'reid', 'release', 'releaseaccent', 'rgid',
74 'script', 'secondarytype', 'status', 'tag',
75 'tracks', 'tracksmedium', 'type'
76 ];
77 $search_fields{work}
78 5 = [ 'alias', 'arid', 'artist', 'comment', 'iswc', 'lang', 'tag', 'type', 'wid', 'work',
79 'workaccent' ];
80  
81 5 return \%search_fields;
82 6 };
83  
84 8 ▶sub is_valid_subquery {
Subroutine is_valid_subquery: called CC=5 SCAR=16.1
Pod undocumented
85 8 my $self = shift;
86 8 my $resource = shift;
87 8 my $subqueries = shift;
88  
89 8 ▶ return unless ($resource);
Branch: 1 branch
BranchTrueFalseTotal
unless $resource 0 8 8
90  
91 8 my $resource_map = $subquery_map{$resource};
92 8 ▶ return if ( !$resource_map );
Branch: 1 branch
BranchTrueFalseTotal
unless $resource_map 0 8 8
93  
94 236 my %valid_fields = map { $_ => 1 } (
95 @$resource_map,
96 8 @{ $subquery_map{_modifiers} },
97 8 @{ $subquery_map{_misc} },
98 8 @{ $self->relationships }
99 );
100  
101 8 foreach my $subquery (@$subqueries) {
102 10 ▶ return if ( !$valid_fields{$subquery} );
Branch: 1 branch
BranchTrueFalseTotal
unless $valid_fields{$subquery} 1 9 10
103 }
104  
105 7 return 1;
106 }
107  
108 27 ▶sub search {
Subroutine search: called CC=13 SCAR=25.7
Pod documented
109 27 my $self = shift;
110 27 my $search_resource = shift;
111 27 my $search_query = shift;
112  
113 27 $self->request( WebService::MusicBrainz::Request->new() );
114  
115 27 ▶ if ( !grep /^$search_resource$/, @{ $self->valid_resources() } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep /^$search_resource$/u, @{$self->valid_resources;}) 0 27 27
116 0 die "Not a valid resource for search ($search_resource)";
117 }
118  
119 27 $self->request()->search_resource($search_resource);
120  
121 27 ▶ if ( exists $search_query->{mbid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $search_query->{"mbid"}) 11 16 27
122 11 $self->request()->mbid( $search_query->{mbid} );
123 11 delete $search_query->{mbid};
124 }
125  
126 27 ▶ if ( exists $search_query->{discid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $search_query->{"discid"}) 3 24 27
127 3 $self->request()->discid( $search_query->{discid} );
128 3 delete $search_query->{discid};
129 }
130  
131 27 my $inc_subqueries = delete $search_query->{inc};
132 # only use "inc" parameters when a specific MBID or DISCID is given
133 27 ▶ if ( ( $self->request()->mbid() or $self->request()->discid() ) and $inc_subqueries ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->request->mbid || $self->request->discid and $inc_subqueries) 8 19 27
Condition100%6/6
$self->request->mbid || $self->request->discid and $inc_subqueries
!ll&&!rl&&r
1368
$self->request->mbid or $self->request->discid
l!l&&r!l&&!r
11313
MC/DC100%3/3
$self->request->mbid || $self->request->discid and $inc_subqueries
$self->request->mbid $self->request->discid $inc_subqueries
Truth table100%5/5
A or B and C
A= $self->request->mbidB= $self->request->discidC= $inc_subqueries
ABCresult
00X0
1X00
0100
1X11
0111
134 8 ▶ $inc_subqueries = [$inc_subqueries] if ( !ref $inc_subqueries );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $inc_subqueries 6 2 8
135  
136 8 ▶ if ( $self->is_valid_subquery( $search_resource, $inc_subqueries ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_valid_subquery($search_resource, $inc_subqueries)) { } 7 1 8
137 7 $self->request()->inc($inc_subqueries);
138 } else {
139 1 my $subquery_str = join ", ", @$inc_subqueries;
140 1 die "Not a valid \"inc\" subquery ($subquery_str) for resource: $search_resource";
141 }
142 }
143  
144 26 ▶ if ( exists $search_query->{fmt} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $search_query->{"fmt"}) 3 23 26
145 3 $self->request()->format( $search_query->{fmt} );
146 3 delete $search_query->{fmt};
147 }
148  
149 26 ▶ if ( exists $search_query->{offset} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $search_query->{"offset"}) 1 25 26
150 1 $self->request()->offset( $search_query->{offset} );
151 1 delete $search_query->{offset};
152 }
153  
154 26 foreach my $search_field ( keys %{$search_query} ) {
155 20 ▶ if ( !grep /^$search_field$/, @{ $self->search_fields_by_resource->{$search_resource} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep /^$search_field$/u, @{$self->search_fields_by_resource->{$search_resource};}) 1 19 20
156 1 die "Not a valid search field ($search_field) for resource \"$search_resource\"";
157 }
158 }
159  
160 25 $self->request->query_params($search_query);
161  
162 25 my $request_result = $self->request()->result();
163  
164 25 return $request_result;
165 }
166  
167 =head1 NAME
168  
169 WebService::MusicBrainz
170  
171 =head1 SYNOPSIS
172  
173 use WebService::MusicBrainz;
174  
175 my $mb = WebService::MusicBrainz->new();
176  
177 my $result = $mb->search($resource => { $search_key => 'search value' });
178 my $result = $mb->search($resource => { $search_key => 'search value', fmt => 'json' }); # fmt => 'json' is default
179  
180 my $result_dom = $mb->search($resource => { $search_key => 'search value', fmt => 'xml' });
181  
182 =head1 DESCRIPTION
183  
184 API to search the musicbrainz.org database
185  
186 =head1 VERSION
187  
188 Version 1.0 and future releases are not backward compatible with pre-1.0 releases. This is a complete re-write using version 2.0 of the MusicBrainz API and Mojolicious.
189  
190 =head1 METHODS
191  
192 =head2 new
193  
194 my $mb = WebService::MusicBrainz->new();
195  
196 =head2 search
197  
198 my $result_list = $mb->search($resource => { param1 => 'value1' });
199  
200 my $result = $mb->search($resource => { mbid => 'mbid' });
201  
202 my $result_more = $mb->search($resource => { mbid => 'mbid', inc => 'extra stuff' });
203  
204 Valid values for $resource are: area, artist, event, instrument, label, recording, release, release-group, series, work, url
205 The default is to return decoded JSON as a perl data structure. Specify format => 'xml' to return the results as an instance of Mojo::DOM.
206  
207 =head3 Search by MBID
208  
209 my $result = $mb->search($resource => { mbid => 'xxxxxx' });
210  
211 The "inc" search parameter is only allowed when searching for any particular "mbid".
212  
213 =head3 Search area
214  
215 my $area_list_results = $mb_ws->search(area => { iso => 'US-OH' });
216 my $area_list_results = $mb_ws->search(area => { area => 'cincinnati' });
217 my $area_list_results = $mb_ws->search(area => { alias => 'new york' });
218 my $area_list_results = $mb_ws->search(area => { sortname => 'new york' });
219 my $area_list_results = $mb_ws->search(area => { area => 'new york', type => 'city' });
220  
221 my $area_result = $mb_ws->search(area => { mbid => '0573177b-9ff9-4643-80bc-ed2513419267' });
222 my $area_result = $mb_ws->search(area => { mbid => '0573177b-9ff9-4643-80bc-ed2513419267', inc => 'area-rels' });
223  
224 =head3 Search artist
225  
226 # JSON example
227 my $artists = $mb->search(artist => { artist => 'Ryan Adams' });
228 my $artists = $mb->search(artist => { artist => 'Ryan Adams', type => 'person' });
229  
230 my $artist_country = $artists->{artists}->[0]->{country};
231  
232 # XML example
233 my $artists = $mb->search(artist => { artist => 'Ryan Adams', type => 'person', fmt => 'xml' });
234  
235 my $artist_country = $artists->at('country')->text;
236  
237 # find this particular artist
238 my $artist = $mb->search(artist => { mbid => '5c2d2520-950b-4c78-84fc-78a9328172a3' });
239  
240 # find this particular artist and include release and artist relations (members of the band)
241 my $artist = $mb->search(artist => { mbid => '5c2d2520-950b-4c78-84fc-78a9328172a3', inc => ['releases','artist-rels'] });
242  
243 # artists that started in Cincinnati
244 my $artists = $mb->search(artist => { beginarea => 'Cincinnati' });
245  
246 =head3 Search label
247  
248 my $labels = $mb->search(label => { label => 'Death' });
249  
250 =head3 Search recording
251  
252 my $recordings = $mb->search(recording => { artist => 'Taylor Swift' });
253  
254 =head3 Search release
255  
256 my $releases = $mb->search(release => { release => 'Love Is Hell', status => 'official' });
257 print "RELEASE COUNT: ", $releases->{count}, "\n";
258  
259 =head1 DEBUG
260  
261 Set environment variable MUSICBRAINZ_DEBUG=1
262  
263 =over 1
264  
265 =item
266  
267 The URL that is generated for the search will output to STDOUT.
268  
269 =item
270  
271 The formatted output (JSON or XML) will be output to STDOUT
272  
273 =back
274  
275 =head1 AUTHOR
276  
277 =over 4
278  
279 =item Bob Faist <bob.faist@gmail.com>
280  
281 =back
282  
283 =head1 COPYRIGHT AND LICENSE
284  
285 Copyright 2006-2017 by Bob Faist
286  
287 This library is free software; you can redistribute it and/or modify
288 it under the same terms as Perl itself.
289  
290 =head1 SEE ALSO
291  
292 https://musicbrainz.org/doc/MusicBrainz_API
293  
294 =cut
295  
296 6 1;
↑ Summary MusicBrainz/Request.pm »