root/plagger/trunk/my-plugins/CustomFeed-SOS/SOS.pm

Revision 1 (checked in by yohei, 2 years ago)

Add my Plagger plugins

Line 
1 package Plagger::Plugin::CustomFeed::SOS;
2 use strict;
3 use base qw( Plagger::Plugin );
4
5 use Encode;
6 use Plagger::UserAgent;
7 use Plagger::Util qw( decode_content );
8
9 sub register {
10     my($self, $context) = @_;
11     $context->register_hook(
12         $self,
13         'customfeed.handle' => \&handle,
14     );
15 }
16
17 sub handle {
18     my($self, $context, $args) = @_;
19
20     # for sos-dan radio sibu only
21     if ($args->{feed}->url =~ m!^http://lantis-net.com/haruhi/!) {
22         $self->aggregate($context, $args);
23         return 1;
24     }
25
26     return;
27 }
28
29 sub aggregate {
30     my($self, $context, $args) = @_;
31
32     my $url = $args->{feed}->url;
33     $context->log(info => "GET $url");
34
35     my $agent = Plagger::UserAgent->new;
36     my $res = $agent->fetch($url, $self);
37
38     if ($res->is_error) {
39         $context->log(error => "GET $url failed: " . $res->status_code);
40         return;
41     }
42
43     my $content = decode_content($res);
44
45     my $feed = Plagger::Feed->new;
46     $feed->title(decode_content("SOS団ラジオ支部"));
47     $feed->link($url);
48
49     my $re = <<'RE';
50 <!--00[1-5]-->.*?<span class="syokai2">\s*\[\s*(.*?)\s*\]\s*(.*?)\s*<span class="date">\s*(\d{4}年\s*\d+\s*月\s*\d+\s*日)\s*放送分\s*</span>\s*</span>.*?<div class="w95 ma mb10">(.*?)</div>.*?<a href="(\d+.asx)".*?>.*?</a>.*?<!--00[1-5]-->
51 RE
52     $re = decode_content($re);
53
54     $content =~ s/\r\n/\n/g;
55
56     my @matches;
57     my @keys = qw( title date body enclosure );
58     my $date_format = decode_content("%Y年%m月%d日");
59     $url =~ m!(.*/)!;
60     my $base = $1;
61
62     while ($content =~ /$re/gs) {
63         my $data;
64         @{$data}{@keys} = ($1.$2, $3, $4, $5);
65         $data->{date} = Plagger::Date->strptime($date_format, $data->{date});
66
67         my $entry = Plagger::Entry->new;
68         $entry->title($data->{title});
69         $entry->date($data->{date});
70         $entry->body($data->{body});
71
72         my $res = $agent->fetch(URI->new_abs($data->{enclosure}, $base), $self);
73         if ($res->is_error) {
74             $context->log(error => "GET $url failed: " . $res->status_code);
75             return;
76         }
77
78         my $meta = decode_content($res);
79
80         while ($meta =~ m!http://.*?\.wsx!g) {
81             $self->log(info => "Adding enclosure $&");
82             my $enclosure = Plagger::Enclosure->new;
83             $enclosure->url($&);
84             $enclosure->auto_set_type;
85             $entry->add_enclosure($enclosure);
86         }
87
88         $feed->add_entry($entry);
89     }
90
91     $context->update->add($feed);
92 }
93
94 1;
95
96 __END__
97
98 =head1 NAME
99
100 Plagger::Plugin::CustomFeed::SOS - Custom feed for SOS-dan radio shibu
101
102 =head1 SYNOPSIS
103
104   - module: Subscription::Config
105     config:
106       feed:
107         - http://lantis-net.com/haruhi/radio.html
108
109   - module: CustomFeed::SOS
110
111 =head1 DESCRIPTION
112
113 This plugin creates a custom feed off of net radio - SOS-dan radio shibu
114
115 =head1 AUTHOR
116
117 Yohei Fushii
118
119 =head1 SEE ALSO
120
121 L<Plagger>, L<http://lantis-net.com/haruhi/>
122
123 =cut
Note: See TracBrowser for help on using the browser.