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

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

Add my Plagger plugins

Line 
1 package Plagger::Plugin::CustomFeed::MangaOhClub;
2 use strict;
3 use base qw( Plagger::Plugin );
4
5 use Encode;
6 use String::Format;
7 use Plagger::UserAgent;
8 use Plagger::Util qw( decode_content );
9 use Plagger::Date;
10
11 our $CATEGORY = {
12     comic =>  {
13         title   => 'コミック',
14         csv_re  => qr{^"([^"]*)","(\d+/\d+/\d+)","([^"]*)","([^"]*)","([^"]*)","([^"]*)"$},
15         capture => ['publisher', 'date', 'title', 'author', 'cost', 'info'],
16         template => << "TEMPLATE",
17 <dl>
18 <dt>作者</dt>
19 <dd>%a</dd>
20 <dt>出版社</dt>
21 <dd>%p</dd>
22 <dt>発売日</dt>
23 <dd>%d</dd>
24 <dt>価格</dt>
25 <dd>%c円</dd>
26 </dl>
27 TEMPLATE
28     },
29     av    => {
30         title    => '音楽&映像',
31         csv_re   => qr{^"([^"]*)","(\d+/\d+/\d+)","([^"]*)","([^"]*)","[^"]*","([^"]*)","([^"]*)"$},
32         capture  => ['kind', 'date', 'title', 'publisher', 'cost', 'info'],
33         template => << "TEMPLATE",
34 <dl>
35 <dt>メーカー</dt>
36 <dd>%p</dd>
37 <dt>発売日</dt>
38 <dd>%d</dt>
39 <dt>種別</dt>
40 <dd>%k</dd>
41 <dt>価格</dt>
42 <dd>%c円</dd>
43 <dt>補足</dt>
44 <dd>%i</dd>
45 </dl>
46 TEMPLATE
47     },
48     bunko => {
49         title   => '文庫&漫画文庫',
50         csv_re  => qr{^"([^"]*)","([^"]*)","([^"]*)","(\d+/\d+/\d+)","([^"]*)","([^"]*)","([^"]*)","([^"]*)","([^"]*)"$},
51         capture => ['type', 'publisher', 'bunko',  'date', 'title', 'author', 'eshi', 'cost', 'info'],
52         template => << "TEMPLATE",
53 <dl>
54 <dt>作者</dt>
55 <dd>%a %e</dd>
56 <dt>出版社</dt>
57 <dd>%p - %b</dd>
58 <dt>発売日</dt>
59 <dd>%d</dd>
60 <dt>価格</dt>
61 <dd>%c円</dd>
62 <dt>補足</dt>
63 <dd>%i</dd>
64 </dl>
65 TEMPLATE
66     },
67     june =>  {
68         title   => '耽美コミック・ノベル',
69         csv_re  => qr{^"([^"]*)","([^"]*)","(\d+/\d+/\d+)","([^"]*)","([^"]*)","([^"]*)","([^"]*)"$},
70         capture => ['publisher', 'type', 'date', 'title', 'author', 'eshi', 'cost'],
71         template => << "TEMPLATE",
72 <dl>
73 <dt>作者</dt>
74 <dd>%a %e</dd>
75 <dt>出版社</dt>
76 <dd>%p %t</dd>
77 <dt>発売日</dt>
78 <dd>%d</dd>
79 <dt>価格</dt>
80 <dd>%c円</dd>
81 </dl>
82 TEMPLATE
83     },
84 };
85
86 sub register {
87     my ($self, $context) = @_;
88     $context->register_hook(
89         $self,
90         'subscription.load' => \&load,
91     );
92 }
93
94 sub load {
95     my($self, $context) = @_;
96
97     my $feed = Plagger::Feed->new;
98        $feed->aggregator( sub { $self->aggregate(@_); });
99     $context->subscription->add($feed);
100 }
101
102 sub aggregate {
103     my($self, $context, $args) = @_;
104     for my $type (@{$self->conf->{category} || ['comic']}) {
105         $context->error("$type not found") unless $CATEGORY->{$type};
106         $self->aggregate_feed($context, $type, $args);
107     }
108 }
109
110 sub aggregate_feed {
111     my($self, $context, $type, $args) = @_;
112
113     my($day, $month, $year) = (localtime)[3, 4, 5];
114     $year  += 1900;
115     $month += 1;
116
117     my $uri = sprintf("http://www.mangaoh.co.jp/download/$type%d%02d.csv", $year, $month);
118     $context->log(info => "GET $uri");
119
120     my $agent = Plagger::UserAgent->new;
121     my $res = $agent->fetch($uri, $self);
122     if ($res->http_response->is_error) {
123         $context->log(error => "GET $uri failed: " . $res->http_response()->status_line);
124         return;
125     }
126     my $content = decode_content($res);
127
128     my $feed = Plagger::Feed->new;
129     $feed->title(decode_utf8("まんが王倶楽部 $CATEGORY->{$type}->{title}"));
130     $feed->link($uri);
131
132     my $future_for = $self->conf->{future_for} || 0;
133
134     my @lines = split /\r\n/, $content;
135     my $minute = 0;
136     for my $line (@lines) {
137         my $data = $self->hash_from_line($type, $line);
138         next unless $data;
139        
140         my $date = Plagger::Date->strptime('%y/%m/%d', $data->{date});
141            $date->set_time_zone($context->conf->{timezone});
142         next if $date->day > $day + $future_for;
143            
144         my $body = stringf (decode_utf8($CATEGORY->{$type}->{template}), $data);
145         my $entry = Plagger::Entry->new;
146
147         $entry->title($data->{title});
148         $entry->author($data->{author});
149         $entry->date($date);
150         $entry->body($body);
151         $entry->link("$uri?" . $entry->digest);
152         $entry->id($entry->digest);
153         push @{$entry->tags}, $data->{info};
154
155         $feed->add_entry($entry);
156         $context->log(debug => "$date $data->{title}");
157     }
158
159     $context->update->add($feed);
160
161     return 1;
162 }
163
164 sub hash_from_line {
165     my($self, $type, $line) = @_;
166
167     my @match = $line =~ $CATEGORY->{$type}->{csv_re};
168     return unless @match;
169
170     my $data;
171     @{$data}{@{$CATEGORY->{$type}->{capture}}} = @match;
172     for my $key (keys %$data) {
173         $data->{substr($key, 0, 1)} = $data->{$key};
174     }
175
176     return $data;
177 }
178
179 1;
180
Note: See TracBrowser for help on using the browser.