root/trunk/plagger/assets/plugins/CustomFeed-Script/wowow-hv-51ch.pl

Revision 1752 (checked in by miyagawa, 2 years ago)

add misc CustomFeed?::Script example scripts

  • Property svn:executable set to *
Line 
1 #!/usr/bin/perl -w
2 use strict;
3 use utf8;
4 use DateTime;
5 use DateTime::Format::W3CDTF;
6 use Encode;
7 use LWP::Simple ();
8 use YAML;
9
10 my @url = ("http://www.wowow.co.jp/hivision/list_hv.html",
11            "http://www.wowow.co.jp/hivision/list_51.html");
12
13 my %seen;
14 my @programs = grep { !$seen{"$_->{channel}|$_->{date}"}++ }
15     sort { $a->{date} cmp $b->{date} }
16     map fetch_program($_), @url;
17
18 binmode STDOUT, ":utf8";
19 print YAML::Dump +{
20     title => 'WOWOW HV / 5.1ch programs',
21     link  => "http://www.wowow.co.jp/hivision/indexh.html",
22     entry => [
23         map {
24             my @tags = ($_->{channel});
25             push @tags, 'HV'    if $_->{hivision};
26             push @tags, '5.1ch' if $_->{51};
27             +{ title => $_->{title},
28                date  => $_->{date},
29                tags  => \@tags,
30                link  => $_->{link} }
31         } @programs,
32     ],
33 };
34
35 sub fetch_program {
36     my $url = shift;
37     my $html = LWP::Simple::get($url);
38     $html = decode("shift_jis", $html);
39     $html =~ tr/\r//d;
40     my $re = <<'RE';
41 <tr bgcolor="#(?:CCCCCC|FFFFCC)">
42   <td width="385"><span class="t12"><a href="(http://www\.wowow\.co\.jp/schedule/ghtml/.*?\.html)" target="_blank">(.*?)</a></span></td>
43   <td width="45" nowrap><span class="t12">(\d+ch)</span></td>
44   <td width="65" nowrap>(<img src="http://www\.wowow\.co\.jp/hivision/img/n?mark_15\.gif">)?(<img src="http://www\.wowow\.co\.jp/hivision/img/mark_51\.gif">)?</td>
45   <td width="150" nowrap><span class="t12">(.*?)</span></td>
46 </tr>
47 RE
48     ;
49     my @program;
50     while ($html =~ /$re/g) {
51         my %data;
52         @data{qw(link title channel hivision 51 date)} = ($1, $2, $3, $4, $5, $6);
53         $data{hivision} = $data{hivision} !~ /nmark/;
54         $data{date} = munge_datetime($data{date});
55         push @program, \%data;
56     }
57     return @program;
58 }
59
60 sub munge_datetime {
61     my $date = shift;
62
63     # date: 2006年10月28日午後0:00~ JST
64     $date =~ /^(\d{4})年(\d{1,2})月(\d{1,2})日(午前|午後|深夜)(\d{1,2}):(\d{2})/
65         or die "No match: $date";
66     my($year, $month, $day, $am_pm_midnight, $hour, $minute) = ($1, $2, $3, $4, $5, $6);
67     $hour += 12 if $am_pm_midnight eq '午後';
68
69     my $dt = DateTime->new(
70         year  => $year,
71         month => $month,
72         day   => $day,
73         hour  => $hour,
74         minute => $minute,
75         time_zone => 'Asia/Tokyo',
76     );
77     $dt->add( days => 1 ) if $am_pm_midnight eq '深夜';
78
79     return DateTime::Format::W3CDTF->format_datetime($dt);
80 }
Note: See TracBrowser for help on using the browser.