| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
=head1 NAME |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
WebService::BBC::MusicCharts - Retrieve and return UK music chart listings |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
use WebService::BBC::MusicCharts; |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
my $chart = WebService::BBC::MusicCharts->new( chart => 'album' ); |
|
10
|
|
|
|
|
|
|
my $count = 1; |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
foreach my $title ($chart->titles) { |
|
13
|
|
|
|
|
|
|
print "At $count we have $title\n"; |
|
14
|
|
|
|
|
|
|
$count++; |
|
15
|
|
|
|
|
|
|
} |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
The WebService::BBC::MusicCharts module provides access to some of the BBCs |
|
20
|
|
|
|
|
|
|
online music charts via a simple object-oriented interface. |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
It currently supports the singles chart, album chart and downloaded music |
|
23
|
|
|
|
|
|
|
charts. |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head1 EXAMPLES |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
use WebService::BBC::MusicCharts; |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
my $albums = WebService::BBC::MusicCharts->new( chart => 'album' ); |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
foreach my $chart_position (1..40) { |
|
32
|
|
|
|
|
|
|
my $details = $albums->number($chart_position); |
|
33
|
|
|
|
|
|
|
print "$chart_position: ", $details->{title}; |
|
34
|
|
|
|
|
|
|
print " by ", $details->{artist},"\n"; |
|
35
|
|
|
|
|
|
|
} |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=cut |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
####################################################################### |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
package WebService::BBC::MusicCharts; |
|
42
|
1
|
|
|
1
|
|
23812
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
35
|
|
|
43
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
32
|
|
|
44
|
1
|
|
|
1
|
|
937
|
use LWP::Simple qw(get); |
|
|
1
|
|
|
|
|
82696
|
|
|
|
1
|
|
|
|
|
8
|
|
|
45
|
1
|
|
|
1
|
|
1039
|
use Template::Extract; |
|
|
1
|
|
|
|
|
760
|
|
|
|
1
|
|
|
|
|
31
|
|
|
46
|
1
|
|
|
1
|
|
7
|
use vars qw($VERSION); |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
779
|
|
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
$VERSION = "0.01"; |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
my %charts = ( |
|
51
|
|
|
|
|
|
|
album => 'http://www.bbc.co.uk/radio1/chart/albums.shtml', |
|
52
|
|
|
|
|
|
|
download => 'http://www.bbc.co.uk/radio1/chart/downloads.shtml', |
|
53
|
|
|
|
|
|
|
singles => 'http://www.bbc.co.uk/radio1/chart/singles.shtml', |
|
54
|
|
|
|
|
|
|
); |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head1 METHODS |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=over 4 |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=item new ( chart => 'chart type' ) |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
This is the constructor for a new WebService::BBC::MusicCharts object. |
|
64
|
|
|
|
|
|
|
The C argument is required (and can currently be 'album', |
|
65
|
|
|
|
|
|
|
'download' or 'singles') and the module will C if it is either |
|
66
|
|
|
|
|
|
|
missing or passed an invalid value. |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
The constructor also does the actual page fetch, which may also C |
|
69
|
|
|
|
|
|
|
if the C fails. Wrapping the C invocation in an C isn't |
|
70
|
|
|
|
|
|
|
a bad idea. |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=back |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=cut |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
#----------------------------------------# |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub new { |
|
79
|
1
|
|
|
1
|
1
|
13
|
my $class = shift; |
|
80
|
1
|
|
|
|
|
4
|
my $self = {@_}; |
|
81
|
|
|
|
|
|
|
|
|
82
|
1
|
50
|
|
|
|
7
|
die "Unknown chart '$self->{chart}'" unless $charts{$self->{chart}}; |
|
83
|
|
|
|
|
|
|
|
|
84
|
1
|
|
|
|
|
7
|
my @chart_entries = _get_entries($charts{$self->{chart}}); |
|
85
|
|
|
|
|
|
|
|
|
86
|
0
|
|
|
|
|
0
|
push(@{$self->{entries}}, @chart_entries); |
|
|
0
|
|
|
|
|
0
|
|
|
87
|
|
|
|
|
|
|
|
|
88
|
0
|
|
|
|
|
0
|
bless ($self, $class); |
|
89
|
0
|
|
|
|
|
0
|
return $self; |
|
90
|
|
|
|
|
|
|
} |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
#----------------------------------------# |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=over 4 |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=item chart_type |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
Returns the type of chart that this instance represents. Mostly used |
|
99
|
|
|
|
|
|
|
when I was debugging the module. |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=back |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=cut |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
sub chart_type { |
|
106
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
|
107
|
0
|
|
|
|
|
0
|
return $self->{chart}; |
|
108
|
|
|
|
|
|
|
} |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
#----------------------------------------# |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=over 4 |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=item titles |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
Returns an array containing all the titles from the chart this instance |
|
117
|
|
|
|
|
|
|
represents. |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=back |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=cut |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub titles { |
|
124
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
|
125
|
0
|
|
|
|
|
0
|
my @titles; |
|
126
|
|
|
|
|
|
|
|
|
127
|
0
|
|
|
|
|
0
|
foreach my $title (@{$self->{entries}}) { |
|
|
0
|
|
|
|
|
0
|
|
|
128
|
0
|
|
|
|
|
0
|
push(@titles, $title->{title}); |
|
129
|
|
|
|
|
|
|
} |
|
130
|
|
|
|
|
|
|
|
|
131
|
0
|
|
|
|
|
0
|
return @titles; |
|
132
|
|
|
|
|
|
|
} |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
#----------------------------------------# |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=over 4 |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=item artists |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Returns an array containing all the artists from the chart this instance |
|
141
|
|
|
|
|
|
|
represents. |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=back |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=cut |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
# TODO merge the iterator methods in to one. |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
sub artists { |
|
150
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
|
151
|
0
|
|
|
|
|
0
|
my @artists; |
|
152
|
|
|
|
|
|
|
|
|
153
|
0
|
|
|
|
|
0
|
foreach my $title (@{$self->{entries}}) { |
|
|
0
|
|
|
|
|
0
|
|
|
154
|
0
|
|
|
|
|
0
|
push(@artists, $title->{artist}); |
|
155
|
|
|
|
|
|
|
} |
|
156
|
|
|
|
|
|
|
|
|
157
|
0
|
|
|
|
|
0
|
return @artists; |
|
158
|
|
|
|
|
|
|
} |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
#----------------------------------------# |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=over 4 |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=item number($num) |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
The number method must be called with a valid integer (it accepts any |
|
167
|
|
|
|
|
|
|
between 1 and 40), it then returns a hash ref containing the details for |
|
168
|
|
|
|
|
|
|
the song/album at that chart position. |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
The fields returned are: |
|
171
|
|
|
|
|
|
|
C - the name of the artist |
|
172
|
|
|
|
|
|
|
C - the title of the single or album |
|
173
|
|
|
|
|
|
|
C |
|
174
|
|
|
|
|
|
|
C - the current chart position of the song/album |
|
175
|
|
|
|
|
|
|
C - The position this song/album was at last week. This |
|
176
|
|
|
|
|
|
|
will either be a number, NEW or RE (re-entry) |
|
177
|
|
|
|
|
|
|
C - the number of weeks it's been in the chart |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
If called without an argument, or with one that's not between 1 and 40, it |
|
180
|
|
|
|
|
|
|
will return undef. |
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=back |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=cut |
|
185
|
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
sub number { |
|
187
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
|
188
|
0
|
|
|
|
|
0
|
my $number = shift; |
|
189
|
|
|
|
|
|
|
|
|
190
|
0
|
0
|
|
|
|
0
|
return unless $number; |
|
191
|
0
|
0
|
|
|
|
0
|
return unless $number =~ /^\d+$/; |
|
192
|
0
|
0
|
0
|
|
|
0
|
return if ($number < 1 || $number > 40); |
|
193
|
|
|
|
|
|
|
|
|
194
|
0
|
|
|
|
|
0
|
$number--; # we're using 0 offset arrays |
|
195
|
0
|
|
|
|
|
0
|
my $entry = $self->{entries}->[$number]; |
|
196
|
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
} |
|
198
|
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
#----------------------------------------# |
|
200
|
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
# internal function that parses the HTML, builds the objects internal |
|
202
|
|
|
|
|
|
|
# state and passes it back to the constructor. Dense but not complex |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
sub _get_entries { |
|
205
|
1
|
|
|
1
|
|
3
|
my $chart_url = shift; |
|
206
|
1
|
|
|
|
|
9
|
my $extract = Template::Extract->new(); |
|
207
|
1
|
|
|
|
|
52677
|
my @chart_entries; |
|
208
|
|
|
|
|
|
|
|
|
209
|
1
|
50
|
|
|
|
10
|
my $chart_page = get($chart_url) |
|
210
|
|
|
|
|
|
|
or die "Failed to get chart."; |
|
211
|
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
# define the extraction template |
|
213
|
0
|
|
|
|
|
|
my $template = << 'END_OF_TEMPLATE'; |
|
214
|
|
|
|
|
|
|
[% FOREACH entries %] |
|
215
|
|
|
|
|
|
|
[% ... %] |
|
216
|
|
|
|
|
|
|
| [% thisweek %] | [% ... %]
|
217
|
|
|
|
|
|
|
| [% lastweek %] | [% ... %]
|
218
|
|
|
|
|
|
|
| ([% weeksin %]) | [% ... %]
|
219
|
|
|
|
|
|
|
[% artist %][% ... %] |
|
220
|
|
|
|
|
|
|
[% title %][% ... %] |
|
221
|
|
|
|
|
|
|
([% label %])[% ... %] |
|
222
|
|
|
|
|
|
|
[% ... %] |
|
223
|
|
|
|
|
|
|
[% END %] |
|
224
|
|
|
|
|
|
|
END_OF_TEMPLATE |
|
225
|
|
|
|
|
|
|
|
|
226
|
0
|
|
|
|
|
|
for (@{$extract->extract($template, $chart_page)->{entries}}) { |
|
|
0
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
# tidy up "last week" |
|
228
|
0
|
|
|
|
|
|
$_->{lastweek} =~ m/^\s*(\w+)/; |
|
229
|
0
|
|
|
|
|
|
my $last_week = $1; |
|
230
|
|
|
|
|
|
|
|
|
231
|
0
|
|
|
|
|
|
my %entry_details = ( |
|
232
|
|
|
|
|
|
|
artist => $_->{artist}, |
|
233
|
|
|
|
|
|
|
title => $_->{title}, |
|
234
|
|
|
|
|
|
|
label => $_->{label}, |
|
235
|
|
|
|
|
|
|
this_week => $_->{thisweek}, |
|
236
|
|
|
|
|
|
|
last_week => $last_week, |
|
237
|
|
|
|
|
|
|
weeks_in_chart => $_->{weeksin} |
|
238
|
|
|
|
|
|
|
); |
|
239
|
|
|
|
|
|
|
|
|
240
|
0
|
|
|
|
|
|
push(@chart_entries, \%entry_details); |
|
241
|
|
|
|
|
|
|
} |
|
242
|
0
|
|
|
|
|
|
return @chart_entries; |
|
243
|
|
|
|
|
|
|
} |
|
244
|
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
#----------------------------------------# |
|
246
|
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
1; |
|
248
|
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
####################################################################### |
|
250
|
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
=head1 DEPENDENCIES |
|
252
|
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
WebService::BBC::MusicCharts requires the following modules: |
|
254
|
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
L |
|
256
|
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
L |
|
258
|
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
=head1 LICENCE AND COPYRIGHT |
|
260
|
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
Copyright (C) 2006 Dean Wilson. All Rights Reserved. |
|
262
|
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
This module is free software; you can redistribute it and/or modify it |
|
264
|
|
|
|
|
|
|
under the same terms as Perl itself. |
|
265
|
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
=head1 AUTHOR |
|
267
|
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
Dean Wilson |
|
269
|
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
=cut |