GNUDBSearch.pm

filter: Statementstmt 60.4% 58 / 96 Branchbran 20.0% 2 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 93.7% 15 / 16 Podpod 100% 2 / 2 totaltotal 62.1% 77 / 124 CC 7 SCAR 18.8
CC
7
Cov
57%
byArtist
20.9
__getSearchUrl
2.1
__htmlToXml
2.0
CRAP 6.6
↑ Summary GNUDB/Cd.pm »
1 package Net::GNUDBSearch;
2  
3 =pod
4  
5 =head1 NAME
6  
7 Net::GNUDBSearch - Search interface to GNUDB database
8  
9 =head1 SYNOPSIS
10  
11 use Net::GNUDBSearch;
12 my $search = Net::GNUDBSearch->new();
13 my $results = $search->byArtist("The Prodigy");
14  
15 =head1 DESCRIPTION
16  
17 Net::GNUDBSearch is an interface to the website www.gnudb.org online free CD information database. Based on Net::CDDBSearch by Vitaliy Babiy. This module
18 allows you to search by artist and retrive a list of albums found then get their tracks.
19  
20 =head1 METHODS
21  
22 =cut
23  
24 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use URI::Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use Net::GNUDBSearch::Cd;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 our $VERSION = "1.01";
33 #########################################################
34  
35 =head2 new()
36  
37 my $gnudb = Net::GNUDBSearch->new();
38  
39 Constructor, returns a new instance of the search object ready for use.
40  
41 =cut
42  
43 #########################################################
44 1 ▶sub new{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
45 1 my $class = shift;
46 1 my $self = {
47 '__baseUrl' => "http://www.gnudb.org",
48 '__action' => undef
49 };
50 1 bless $self, $class;
51 1 return $self;
52 }
53 ##########################################################
54  
55 =head2 byArtist($query)
56  
57 my $albums = $gnudb->byArtist("The Prodigy");
58  
59 Returns an array reference of L<Net::GNUDBSearch::Cd> objects containing CD's found from the artist name given.
60  
61 =cut
62  
63 ##########################################################
64 1 ▶sub byArtist{
Subroutine byArtist: called CC=6 SCAR=30.4
Pod documented
65 1 my($self, $query) = @_;
66 1 my @results = ();
67 1 my $class = ref($self);
68 1 ▶ if($query){
Branch: 1 branch
BranchTrueFalseTotal
if ($query) { } 1 0 1
69 1 $self->__setAction("artist");
70 1 my $url = $self->__getSearchUrl();
71 1 my $encoded = $class->__encode($query);
72 1 $url .= "/" . $encoded; #add search terms
73 1 my $mech = WWW::Mechanize->new();
74 1 print "Getting url: $url\n";
75 1 my $res = $mech->get($url);
76 0 ▶ if($mech->success()){
Branch: 1 branch
BranchTrueFalseTotal
if ($mech->success) { } 0 0 0
77 0 my $content = $mech->content();
78 0 ▶ if($content =~ m/<h2>Search Results, \d+ albums found:<\/h2>.+<br><br>(.+<hr size="1">)/s){
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ m[<h2>Search Results, \d+ albums found:</h2>.+<br><br>(.+<hr size="1">)]s) 0 0 0
79 0 my $results = $1;
80 0 my $xml = $class->__htmlToXml($results);
81 0 my $xs = XML::Simple->new();
82 0 my $ref = $xs->XMLin($xml);
83 0 foreach my $match (@{$ref->{'match'}}){
84 #print Dumper $match;
85 0 my $a = $match->{'a'}[0];
86 #get the album and artist
87 0 my $name = $a->{'b'};
88 0 my($cdArtist, $cdAlbum) = split(" / ", $name);
89 0 $cdArtist =~ s/^\s+//g; #remove leading whitespace
90 0 $cdArtist =~ s/\s+$//g; #remove trailing whitespace
91 0 $cdAlbum =~ s/^\s+//g; #remove leading whitespace
92 0 $cdAlbum =~ s/\s+$//g; #remove trailing whitespace
93 #get data for the track lookup
94 0 $a = $match->{'a'}[1];
95 0 ▶ if($a->{'content'} =~ m/^Discid: (\w+) \/ ([a-f0-9]+)$/){
Branch: 1 branch
BranchTrueFalseTotal
if ($a->{'content'} =~ m[^Discid: (\w+) / ([a-f0-9]+)$]) { } 0 0 0
96 0 my $cdGenre = $1;
97 0 my $cdId = $2;
98 0 my $objClass = $class . "::Cd"; #avoid hard coding classes
99 0 my $config = {
100 "id" => $cdId,
101 "artist" => $cdArtist,
102 "album" => $cdAlbum,
103 "genre" => $cdGenre
104 };
105 0 my $cdObj = $objClass->new($config);
106 0 push(@results, $cdObj); #save it in the results
107 }
108 else{
109 0 confess("Invalid GNUDB info");
110 }
111 }
112 }
113 }
114 else{
115 0 confess("Problem with search, code: " . $mech->status());
116 }
117 }
118 else{
119 0 confess("No artist given");
120 }
121 0 return \@results;
122 }
123 #########################################################
124 1 ▶sub __getSearchUrl{
Subroutine __getSearchUrl: called CC=2 SCAR=7.2
125 1 my$self = shift;
126 1 my $url = undef;
127 1 ▶ if($self->__getAction()){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->__getAction) { } 1 0 1
128 1 $url = $self->__getBaseUrl() . "/" . $self->__getAction();
129 }
130 else{
131 0 confess("No action set");
132 }
133 1 return $url;
134 }
135 #########################################################
136 1 ▶sub __getBaseUrl{
Subroutine __getBaseUrl: called CC=1 SCAR=0.0
137 1 my $self = shift;
138 1 return $self->{'__baseUrl'};
139 }
140 #########################################################
141 1 ▶sub __setAction{
Subroutine __setAction: called CC=1 SCAR=0.0
142 1 my($self, $action) = @_;
143 1 $self->{'__action'} = $action;
144 1 return 1;
145 }
146 #########################################################
147 2 ▶sub __getAction{
Subroutine __getAction: called CC=1 SCAR=0.0
148 2 my $self = shift;
149 2 return $self->{'__action'};
150 }
151 #########################################################
152 0 ▶sub __htmlToXml{
Subroutine __htmlToXml: not called CC=1 SCAR=6.9
153 0 my($class, $html) = @_;
154 0 my $xml = $html;
155 0 $xml =~ s/<br>/<br\/>/g;
156 0 $xml =~ s/(<a href="http:\/\/www.gnudb.org\/cd)/<match>$1/g;
157 0 $xml =~ s/<hr size="1">/<\/match>/g;
158 0 $xml =~ s/ target=_blank//g;
159 0 $xml =~ s/&/&amp;/g;
160 0 $xml = "<matches>" . $xml . "</matches>\n";
161 0 return $xml;
162 }
163 #########################################################
164 1 ▶sub __encode{
Subroutine __encode: called CC=1 SCAR=0.0
165 1 my($class, $query) = @_;
166 1 my $uri = URI::Encode->new();
167 1 my $encoded = $uri->encode($query);
168 1 $encoded =~ s/%20/\+/g; #the gnudb search does a redirect if + signs are not used
169 1 return $encoded;
170 }
171 #########################################################
172  
173 =pod
174  
175 =head1 Notes
176  
177 Only retrieves the first page of results as that should be good enough to find data.
178  
179 =head1 Author
180  
181 MacGyveR <dumb@cpan.org>
182  
183 Development questions, bug reports, and patches are welcome to the above address.
184  
185 =head1 Thanks
186  
187 Vitaliy Babiy <admin@mpscope.net> for module Net::CDDBSearch
188  
189 =head1 Copyright
190  
191 Copyright (c) 2012 MacGyveR. All rights reserved.
192  
193 This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
194  
195 =head1 See Also
196  
197 Net::CDDBSearch
198  
199 =cut
200  
201 #########################################################
202 2 return 1;
↑ Summary GNUDB/Cd.pm »