blib/lib/WWW/BurrpTV.pm

filter: Statementstmt 51.2% 84 / 164 Branchbran 42.5% 23 / 54 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 82.3% 14 / 17 Podpod 80.0% 4 / 5 totaltotal 50.8% 127 / 250 CC 36 SCAR 41.2
CC
36
Cov
49%
get_shows
161.6
new
23.5
timezone
6.0
CRAP 61.4
↑ Summary
1 =head1 NAME
2  
3 WWW::BurrpTV - Parse tv.burrp.com for TV listings.
4  
5 =head1 SYNOPSIS
6  
7 use WWW::BurrpTV;
8  
9 my $tv = WWW::BurrpTV->new (
10 cache => '/tmp/module',
11 );
12  
13 $tv->timezone('Asia/Bahrain');
14  
15 # Get current playing show on Discovery Channel
16  
17 my $shows = $tv->get_shows(channel => 'Discovery Channel');
18  
19 my $current_playing_show = $$shows[0]; # First item in the array is the current playing show.
20  
21 print $current_playing_show->{_show};
22 print $current_playing_show->{_time12};
23 print $current_playing_show->{_link};
24  
25 =head1 DESCRIPTION
26  
27 =head2 Overview
28  
29 WWW::BurrpTV is an object oriented interface to parse TV listings from tv.burrp.com.
30  
31 =cut
32  
33 package WWW::BurrpTV;
34  
35 3 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
36 3 ▶use Path::Abstract qw(--no_0_093_warning);
Subroutine BEGIN: called CC=1 SCAR=0.0
37 3 ▶use HTML::TreeBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 3 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 3 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 3 our @ISA = qw();
47  
48 3 our $VERSION = '0.03';
49  
50 3 ▶use vars qw (
Subroutine BEGIN: called CC=1 SCAR=0.0
51 %CHANNELS
52 $UA
53 $TIMEZONE
54 3 );
55  
56 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
57 3 our %CHANNELS;
58 3 our $UA = LWP::UserAgent->new();
59 3 $UA->env_proxy;
60 3 our $TIMEZONE = 'Asia/Kolkata'; # Default timezone
61 }
62  
63 =head1 CONSTRUCTOR
64  
65 =over 4
66  
67 =item new()
68  
69 Object's constructor. Takes an optional hash argument which can be used to cache the list of channels. This does not cache the TV listings.
70  
71 my $tv = WWW::BurrpTV->new (
72 cache => '/tmp/module', # Path to use as cache (optional). Make sure the path exists.
73 );
74 =cut
75  
76 4 ▶sub new {
Subroutine new: called CC=17 SCAR=31.6
Pod documented
77 4 my ($class,%args) = @_;
78 4 my $cache = 0;
79 4 my $self = bless({}, $class);
80 4 my $write = 0;
81 # if ((exists $args{cache}) && (!-d $args{cache})) { carp 'Directory does not exist. Channel list will not be cached.'; }
82 0 ▶ if ((exists $args{cache}) && (!-d $args{cache})) { make_path($args{cache}); $write = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'cache'} and not -d $args{'cache'}) { } 0 4 4
Condition66%2/3
exists $args{"cache"} and not -d $args{"cache"}
!ll&&!rl&&r
130
MC/DC0%0/2
exists $args{"cache"} and not -d $args{"cache"}
exists $args{"cache"} not -d $args{"cache"}
Truth table66%2/3
A and not(B)
A= exists $args{"cache"}B= not -d $args{"cache"}
ABresult
0X0
100
111
83 4 else { $write = 1; }
84  
85 #$cache = $args{cache}.'/cache' if exists $args{cache};
86 4 ▶ $cache = Path::Abstract->new($args{cache})->child('cache')->stringify if exists $args{cache};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"cache"} 3 1 4
87 4 delete $args{cache}; # No longed needed.
88  
89 0 for my $key (keys %args) { carp "Ignored invalid argument ($key)"; }
90  
91 4 my $html;
92  
93 4 ▶ if ($cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache) 3 1 4
94 3 ▶ open my $cachefile, '<', $cache or goto skip_read_cache;
Branch: 1 branch
BranchTrueFalseTotal
open my $cachefile, "<", $cache or goto skip_read_cache 3 0 3
95 3 ▶ while (<$cachefile>) { $html .= $_; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $cachefile)) 4170 3 4173
96 3 close $cachefile;
97 }
98  
99 skip_read_cache:
100  
101 4 ▶ if (!$html) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($html) 1 3 4
102 1 my $channel_list_url = 'http://tv.burrp.com/channels.html';
103 1 my $http = $UA->get($channel_list_url);
104 1 $html = $http->decoded_content;
105 1 ▶ croak $http->status_line unless $http->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $http->is_success 1 0 1
106  
107 0 ▶ if (($cache) && ($write)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache and $write) 0 0 0
Condition0%0/3
$cache and $write
!ll&&!rl&&r
000
MC/DC0%0/2
$cache and $write
$cache $write
Truth table0%0/3
A and B
A= $cacheB= $write
ABresult
0X0
100
111
108 0 my $write_success = 0;
109 0 ▶ open my $cachefile, '>', $cache or carp $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $cachefile, ">", $cache or carp($!) 0 0 0
110 0 print {$cachefile} $html;
111 0 close $cachefile;
112 }
113 }
114 ##
115
116
117 3 my $tree = HTML::TreeBuilder->new_from_content($html);
118 # my $parsed = $tree->parse($html);
119 # $tree->eof;
120  
121 # my $channel_array = $parsed->{'_content'}[1]{'_content'}[2]{'_content'}[0]{'_content'}[1]{'_content'}[1]{'_content'}[1]{'_content'}[1]{'_content'};
122  
123  
124 # for (@$channel_array) {
125 # my $channel_name = $_->{_content}[0]->{_content}[0];
126 # my $link = $_->{_content}[0]->{href};
127 # $channel_name =~ s/^\s*(.*?)\s*$/$1/;
128 # $CHANNELS{$channel_name} = 'http://tv.burrp.com'.$link;
129 # } print Dumper \%CHANNELS;
130  
131 # $tree = $tree->delete();
132 # $parsed = $parsed->delete();
133 3 for (@{$tree->extract_links('a', 'href')})
134 {
135 849 my($link, $el, $attr, $tag) = @$_;
136 849 ▶ next unless $link =~ /\/channel\/[a-z\-]+\/\d+\//;
Branch: 1 branch
BranchTrueFalseTotal
unless $link =~ m[/channel/[a-z\-]+/\d+/] 237 612 849
137 612 my $channel_name = $el->as_text;
138 612 $channel_name =~ s/^\s*(.*?)\s*$/$1/;
139 612 ▶ next unless $channel_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $channel_name 240 372 612
140 372 ▶ next if $channel_name eq 'New';
Branch: 1 branch
BranchTrueFalseTotal
if $channel_name eq "New" 45 327 372
141 327 $CHANNELS{$channel_name} = 'http://tv.burrp.com'.$link;
142 }
143 3 $tree->delete;
144 3 return $self;
145 }
146  
147 =back
148  
149 =head1 METHODS
150  
151 =over 4
152  
153 =item channel_list()
154  
155 This method does not take any arguments, and returns a hashref.
156  
157 $tv->channel_list();
158  
159 =item timezone()
160  
161 Change the default timezone (Asia/Kolkata).
162  
163 $tv->timezone('Europe/Monaco');
164  
165 =item get_shows()
166  
167 Takes the channel name and an optional timezone as arguments, and returns an arrayref.
168  
169 $tv->get_shows (
170 channel => 'Discovery Channel',
171 timezone => 'Asia/Bahrain', # Optional
172 );
173 =cut
174  
175 2 ▶sub channel_list {
Subroutine channel_list: called CC=1 SCAR=0.0
Pod documented
176 2 return \%CHANNELS;
177 }
178  
179  
180  
181  
182 0 ▶sub timezone {
Subroutine timezone: not called CC=2 SCAR=17.9
Pod documented
183 0 my ($class,$new_timezone) = @_;
184 0 my ($success,$error) = verify_timezone($new_timezone);
185  
186 0 ▶ if ($success) { $TIMEZONE = $new_timezone; }
Branch: 1 branch
BranchTrueFalseTotal
if ($success) { } 0 0 0
187 0 else { carp $error; }
188 0 return;
189 }
190  
191 0 ▶sub verify_timezone {
Subroutine verify_timezone: not called CC=2 SCAR=17.9
Pod undocumented
192 0 my ($timezone_to_verify) = @_;
193 0 my $dt_object = DateTime->now();
194 0 eval { $dt_object->set_time_zone($timezone_to_verify); };
195 0 my $error = $@;
196 0 chomp $error;
197  
198 0 ▶ return (0,$error) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
199 0 return 1;
200 }
201  
202 1 ▶sub get_shows {
Subroutine get_shows: called CC=17 SCAR=50.8
Pod documented
203  
204 1 my ($class,%args) = @_;
205 #my $self = bless({}, $class); #testing.
206 0 ▶ if (!exists $args{'channel'}) { croak 'Channel not specified.'; }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"channel"}) 0 1 1
207 1 my $input_channel = $args{'channel'};
208 1 delete $args{'channel'}; # Don't need it anymore.
209 1 my $timezone_override = $TIMEZONE;
210  
211 1 ▶ if (exists $args{'timezone'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"timezone"}) 0 1 1
212 0 my ($success,$error) = verify_timezone($args{'timezone'});
213 0 ▶ if ($success) { $timezone_override = $args{'timezone'}; }
Branch: 1 branch
BranchTrueFalseTotal
if ($success) { } 0 0 0
214 0 else { carp $error; }
215 0 delete $args{'timezone'}; # Don't need it anymore.
216 }
217  
218 0 for my $key (keys %args) { carp "Ignored invalid argument ($key)"; }
219  
220 1 ▶ for (keys %CHANNELS) { if (lc($_) eq lc($input_channel)) { $input_channel = $_; } }
Branch: 1 branch
BranchTrueFalseTotal
if (lc $_ eq lc $input_channel) 1 94 95
221  
222 1 ▶ croak 'Invalid channel' if !exists $CHANNELS{$input_channel};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $WWW::BurrpTV::CHANNELS{$input_channel} 0 1 1
223  
224 1 my $url = $CHANNELS{$input_channel};
225  
226 1 my $http = $UA->get($url);
227 1 ▶ croak $http->status_line unless $http->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $http->is_success 1 0 1
228 0 my $html = $http->decoded_content;
229  
230 0 my $tree = HTML::TreeBuilder->new_from_content($html);
231 # my $parsed = $tree->parse($html);
232 # $tree->eof;
233  
234 0 my $today = 1;
235 0 my @listing_today = qw();
236 0 my @listing_tomorrow = qw();
237 0 my @listing = qw();
238
239 0 ▶ my @elements = $tree->look_down('_tag','td',sub { $_[0]->{class} =~ /resultTime/ if $_[0]->{class}});
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"class"} 0 0 0
240 0 for my $element (@elements) {
241 0 $element = $element->parent;
242 push @listing, {
243 _channel => $input_channel,
244 _original_time => eval {
245 0 my ($time) = $element->look_down('_tag','b','class','from')->as_text =~ /((?:\d{1,2}:?){2})/;
246 0 return $time;
247 },
248 _am_or_pm => $element->look_down('_tag','sup','class','ap')->as_text,
249 _show_link => 'http://tv.burrp.com'.$element->look_down('_tag','a')->{href},
250 0 _show => eval { my ($show) = $element->look_down('_tag','strong')->as_text =~ /(.*?) $/; return $show; },
251 _episode => eval {
252 0 my $full_title = $element->look_down('_tag','a','class','title')->as_text;
253 0 my $title = quotemeta($element->look_down('_tag','strong')->as_text);
254 0 $full_title =~ s/\s+$title\s+:\s+//;
255 0 $full_title =~ s/\s+$//;
256 0 return $full_title;
257 },
258 _image => 'http://tv.burrp.com'.$element->look_down('_tag','img')->{src},
259 0 };
260 0 $element->delete;
261 }
262
263 0 for (@listing) {
264 0 my $show = $_->{_show};
265 0 my $time = $_->{_original_time};
266 0 my $am_or_pm = $_->{_am_or_pm};
267 0 my $show_link = $_->{_show_link};
268 0 my $episode = $_->{_episode};
269 0 ▶ undef $episode if !$episode;
Branch: 1 branch
BranchTrueFalseTotal
unless $episode 0 0 0
270 0 my $season;
271 0 ▶ ($show,$season) = $show =~ /(.*?) \(Season (\d+)\)/ if $show =~ /Season \d+/;
Branch: 1 branch
BranchTrueFalseTotal
if $show =~ /Season \d+/ 0 0 0
272 ################## TIME CONVERSION ###############################
273 0 my ($hour,$minutes) = $time =~ /(\d+):(\d+)/;
274 #my $nt = normalize_hms($hour,$minutes,0,$am_or_pm);
275  
276 0 my $dt = DateTime->now(time_zone => 'Asia/Kolkata');
277  
278 $dt = DateTime->new (
279 time_zone => 'Asia/Kolkata', # Timezone used by tv.burrp.com
280 year => $dt->year,
281 month => $dt->month,
282 day => $dt->day,
283 #hour => $nt->{h24},
284 0 hour => eval {
285 0 ▶ if ($am_or_pm eq 'PM') {
Branch: 1 branch
BranchTrueFalseTotal
if ($am_or_pm eq 'PM') { } 0 0 0
286 0 ▶ if ($hour == 12) { return $hour; }
Branch: 1 branch
BranchTrueFalseTotal
if ($hour == 12) { } 0 0 0
287 0 else { $hour += 12; return $hour; }
288 }
289  
290 else {
291 0 ▶ if ($hour == 12) { return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($hour == 12) { } 0 0 0
292 0 else { return $hour; }
293 }
294 },
295  
296 minute => $minutes,
297 );
298  
299  
300 0 $dt->set_time_zone($timezone_override); # Convert to required timezone
301 #my $minute = $dt->minute;
302 #$minute =~ s/^(\d)$/0$1/ if $minute =~ /^\d$/; # Prefix 0 for single digit numbers
303 #my $human_time = $dt->hour_12.':'.$minute.' '.$dt->am_or_pm;
304 ##################################################################
305  
306 0 my $show_info = {
307 _channel => $input_channel,
308 _time24 => $dt->strftime('%H:%M'),
309 _time12 => $dt->strftime('%I:%M %p'),
310 _show => $show,
311 _link => $show_link,
312 _season => $season,
313 _episode => $episode,
314 };
315  
316 0 ▶ if ($today) { push @listing_today,$show_info; }
Branch: 1 branch
BranchTrueFalseTotal
if ($today) { } 0 0 0
317 0 else { push @listing_tomorrow,$show_info; }
318  
319 }
320  
321
322
323 0 $tree->delete;
324 0 @listing = (@listing_today,@listing_tomorrow);
325 0 return \@listing;
326 0 exit;
327  
328 }
329  
330 3 1;
331 __END__
↑ Summary