blib/lib/Net/CDDBSearch.pm | |||
---|---|---|---|
Criterion | Covered | Total | % |
statement | 60 | 97 | 61.8 |
branch | 12 | 42 | 28.5 |
condition | 0 | 6 | 0.0 |
subroutine | 13 | 15 | 86.6 |
pod | 2 | 8 | 25.0 |
total | 87 | 168 | 51.7 |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | package Net::CDDBSearch; | ||||||
2 | |||||||
3 | require 5.005_62; | ||||||
4 | 1 | 1 | 827 | use strict; | |||
1 | 2 | ||||||
1 | 37 | ||||||
5 | |||||||
6 | 1 | 1 | 704 | use LWP::Simple; | |||
1 | 101932 | ||||||
1 | 9 | ||||||
7 | 1 | 1 | 419 | use URI::Escape; | |||
1 | 7 | ||||||
1 | 1275 | ||||||
8 | |||||||
9 | require Exporter; | ||||||
10 | |||||||
11 | our @ISA = qw/Exporter/; | ||||||
12 | |||||||
13 | our @EXPORT = qw//; | ||||||
14 | |||||||
15 | our @EXPORT_OK = qw/new get_albums_artist get_albums_album get_songs_album albums tracks info url/; | ||||||
16 | |||||||
17 | our $VERSION = '2.01'; | ||||||
18 | |||||||
19 | |||||||
20 | sub new { | ||||||
21 | 3 | 3 | 0 | 167 | my $class = shift; | ||
22 | 3 | 16 | my $self = { | ||||
23 | DEBUG => undef, | ||||||
24 | @_ | ||||||
25 | }; | ||||||
26 | 3 | 9 | bless($self, $class); | ||||
27 | 3 | 50 | 18 | $self ? return $self : return undef; | |||
28 | } | ||||||
29 | |||||||
30 | |||||||
31 | sub albums { | ||||||
32 | 2 | 2 | 1 | 9 | my $self = shift; | ||
33 | 2 | 9 | return $self->{ALBUMS}; | ||||
34 | } | ||||||
35 | |||||||
36 | |||||||
37 | sub tracks { | ||||||
38 | 1 | 1 | 0 | 7 | my $self = shift; | ||
39 | 1 | 4 | return $self->{TRACKS}; | ||||
40 | } | ||||||
41 | |||||||
42 | sub info { | ||||||
43 | 1 | 1 | 1 | 3 | my $self = shift; | ||
44 | 1 | 11 | return {'Title' => $self->{TITLE}, | ||||
45 | 'Artist' => $self->{ARTIST}, | ||||||
46 | 'Year' => $self->{YEAR}, | ||||||
47 | 'Genre' => $self->{GENRE}, | ||||||
48 | 'Tracks' => $self->{TRACKN}, | ||||||
49 | 'Time' => $self->{TIME}}; | ||||||
50 | } | ||||||
51 | |||||||
52 | |||||||
53 | sub url { | ||||||
54 | 0 | 0 | 0 | 0 | my $self = shift; | ||
55 | 0 | 0 | my $line = lc(shift); | ||||
56 | |||||||
57 | 0 | 0 | 0 | if($self->{TYPE} eq 'artist') { | |||
0 | |||||||
58 | 0 | 0 | foreach (keys %{$self->{ALBUMS}}) { | ||||
0 | 0 | ||||||
59 | 0 | 0 | 0 | 0 | return $_ if (lc($self->{ALBUMS}->{$_}[0]) eq lc($self->{QUERY}) && lc($self->{ALBUMS}->{$_}[1]) eq $line); | ||
60 | } | ||||||
61 | } elsif($self->{TYPE} eq 'disc') { | ||||||
62 | 0 | 0 | foreach (keys %{$self->{ALBUMS}}) { | ||||
0 | 0 | ||||||
63 | 0 | 0 | 0 | 0 | return $_ if (lc($self->{ALBUMS}->{$_}[1]) eq lc($self->{QUERY}) && lc($self->{ALBUMS}->{$_}[0]) eq $line); | ||
64 | } | ||||||
65 | } | ||||||
66 | 0 | 0 | return undef; | ||||
67 | } | ||||||
68 | |||||||
69 | |||||||
70 | sub get_albums_artist { | ||||||
71 | 1 | 1 | 0 | 6 | my $self = shift; | ||
72 | 1 | 2 | $self->{QUERY}= shift; | ||||
73 | |||||||
74 | 1 | 50 | 4 | if ($self->{QUERY}) { | |||
75 | 1 | 7 | $self->{URL} = uri_escape($self->{QUERY}); | ||||
76 | 1 | 37 | $self->{ERROR} = undef; | ||||
77 | 1 | 3 | $self->{TYPE} = 'artist'; | ||||
78 | 1 | 50 | 4 | print "$self >> Calling _get_albums();\n" if $self->{DEBUG}; | |||
79 | 1 | 5 | $self = _get_albums($self); | ||||
80 | 1 | 50 | 8 | defined $self->{ERROR} ? return undef : return $self; | |||
81 | } | ||||||
82 | 0 | 0 | $self->{ERROR} = 'No query provided'; | ||||
83 | 0 | 0 | return undef; | ||||
84 | } | ||||||
85 | |||||||
86 | sub get_albums_album { | ||||||
87 | 1 | 1 | 0 | 2 | my $self = shift; | ||
88 | 1 | 3 | $self->{QUERY}= shift; | ||||
89 | |||||||
90 | 1 | 50 | 6 | if ($self->{QUERY}) { | |||
91 | 1 | 7 | $self->{URL} = uri_escape($self->{QUERY}); | ||||
92 | 1 | 15 | $self->{ERROR} = undef; | ||||
93 | 1 | 3 | $self->{TYPE} = 'title'; | ||||
94 | 1 | 50 | 3 | print "$self >> Calling _get_albums();\n" if $self->{DEBUG}; | |||
95 | 1 | 5 | $self = _get_albums($self); | ||||
96 | 1 | 50 | 10 | defined $self->{ERROR} ? return undef : return $self; | |||
97 | } | ||||||
98 | 0 | 0 | $self->{ERROR} = 'No query provided'; | ||||
99 | 0 | 0 | return undef; | ||||
100 | } | ||||||
101 | |||||||
102 | |||||||
103 | sub get_songs_album { | ||||||
104 | 1 | 1 | 0 | 3 | my $self = shift; | ||
105 | 1 | 5 | $self->{URL}= shift; | ||||
106 | |||||||
107 | 1 | 50 | 6 | if ($self->{URL}) { | |||
108 | 0 | 0 | $self->{ERROR} = undef; | ||||
109 | 0 | 0 | $self->{TYPE} = 'track'; | ||||
110 | 0 | 0 | 0 | print "$self >> Calling _get_songs();\n" if defined $self->{DEBUG}; | |||
111 | 0 | 0 | $self = _get_songs($self); | ||||
112 | 0 | 0 | 0 | defined $self->{ERROR} ? return undef : return $self; | |||
113 | } | ||||||
114 | 1 | 4 | $self->{ERROR} = 'No url provided'; | ||||
115 | 1 | 3 | return undef; | ||||
116 | } | ||||||
117 | |||||||
118 | |||||||
119 | sub DESTROY { | ||||||
120 | 2 | 2 | 14 | my $self = shift; | |||
121 | 2 | 6 | $self = {}; | ||||
122 | 2 | 24 | return 1; | ||||
123 | } | ||||||
124 | |||||||
125 | sub _get_url { | ||||||
126 | 2 | 2 | 3 | my $self = shift; | |||
127 | 2 | 50 | 8 | unless ($self->{URL} =~ /^http/) { | |||
128 | 2 | 7 | $self->{URL} = 'http://www.freedb.org/freedb_search.php?allfields=NO&allcats=YES&grouping=none&words=' . $self->{URL}; | ||||
129 | 2 | 7 | $self->{URL} .= '&fields=' . $self->{TYPE}; | ||||
130 | } | ||||||
131 | |||||||
132 | 2 | 50 | 21 | print "$self >> URL: $self->{URL}\n" if $self->{DEBUG}; | |||
133 | 2 | 13 | my $data = get($self->{URL}); | ||||
134 | |||||||
135 | 2 | 1244131 | $self->{DATA} = $data; | ||||
136 | 2 | 8 | return $self; | ||||
137 | } | ||||||
138 | |||||||
139 | sub _get_albums { | ||||||
140 | 2 | 2 | 4 | my $self = shift; | |||
141 | |||||||
142 | 2 | 4 | my($line, $n1, $n2, $n3, $n4); | ||||
143 | |||||||
144 | 2 | 50 | 8 | print "$self >> Calling _get_url();\n" if $self->{DEBUG}; | |||
145 | 2 | 7 | $self = _get_url($self); | ||||
146 | |||||||
147 | 2 | 50 | 10 | return $self if (!$self->{DATA}); | |||
148 | |||||||
149 | 2 | 3562 | while ($self->{DATA} =~ m!([^<]+)\s+\/\s+([^<]+)!ig) { | ||||
150 | 0 | 0 | ($n1,$n2,$n3,$n4) = ($1,$2,$3,$4); | ||||
151 | 0 | 0 | $self->{ALBUMS}->{$n1.'/freedb_search_fmt.php?'.$n2} = [$n3,$n4]; | ||||
152 | } | ||||||
153 | 2 | 9 | return $self; | ||||
154 | } | ||||||
155 | |||||||
156 | |||||||
157 | sub _get_songs { | ||||||
158 | 0 | 0 | my $self = shift; | ||||
159 | |||||||
160 | 0 | my($line, $n1, $n2, $n3, $n4); | |||||
161 | |||||||
162 | 0 | 0 | print "$self >> Calling _get_url();\n" if defined $self->{DEBUG}; | ||||
163 | |||||||
164 | 0 | $self = _get_url($self); | |||||
165 | |||||||
166 | 0 | 0 | return $self if (!$self->{DATA}); | ||||
167 | |||||||
168 | 0 | 0 | if ($self->{DATA} =~ m!([^<]+)\s+\/\s+([^<]+)\s*tracks:\s+([^<]*)\s*total time:\s+([^<]*) \s*year:\s+([^<]*) \s*genre:\s+([^<]*)!is) { |
||||
169 | 0 | $self->{ARTIST} = $1; | |||||
170 | 0 | $self->{TITLE} = $2; | |||||
171 | 0 | $self->{GENRE} = $6; | |||||
172 | 0 | $self->{YEAR} = $5; | |||||
173 | 0 | $self->{TRACKN} = $3; | |||||
174 | 0 | $self->{TIME} = $4; | |||||
175 | } | ||||||
176 | |||||||
177 | 0 | while ($self->{DATA} =~ m! | |||||
\s*(\d+)\. | ([^<]*) | ([^>]+)!g) { | |||||
178 | 0 | $self->{TRACKS}->{$1} = $3; | |||||
179 | } | ||||||
180 | 0 | $self->{DATA} = undef; | |||||
181 | 0 | return $self; | |||||
182 | } | ||||||
183 | |||||||
184 | 1; | ||||||
185 | |||||||
186 | __END__ |