[Plagger] 列車運行情報をPublishしてみる
package Plagger::Plugin::CustomFeed::LDTraffic;
use utf8;
use strict;
use base qw( Plagger::Plugin );
use Plagger::Date;
use Plagger::Util;
use Switch;
use URI;
sub register {
my ($self, $context) = @_;
$context->register_hook(
$self,
'subscription.load' => \&load,
);
}
sub load {
my($self, $context) = @_;
my $ym = $self->conf->{month} || Plagger::Date->now()->strftime("%y%m");
my $feed = Plagger::Feed->new;
$feed->aggregator( sub { $self->aggregate($context, $ym); });
$context->subscription->add($feed);
}
sub aggregate {
my ($self, $context, $ym) = @_;
my $urls = "http://transit.livedoor.com/traffic_info/kantou"; ←関東決め打ちなので適当に変えてください
my $feed = Plagger::Feed->new;
$feed->link($urls);
$feed->title("鉄道運行情報");
my $list = $self->parse(Plagger::Util::load_uri(URI->new($urls)));
for my $item (@$list) {
my $entry = Plagger::Entry->new;
$entry->title($item->{title}."[".$item->{hour}.":".$item->{min}."]"); ←titleに時刻を入れています
$entry->body($item->{body});
$entry->date(Plagger::Date->strptime("%y%m%d%H%M", $ym.$item->{day}.$item->{hour}.$item->{min}));
$feed->add_entry($entry);
}
$context->update->add($feed);
}
sub parse {
my ($self, $content) = @_;
my $list = [];
while ($content =~ m{
<tr>
.*?<th.*?>(\d{2})/(\d{2}).(\d{2})\:(\d{2})</th>
.*?<td.*?>(.*?)</td>
.*?<td.*?>(.*?)</td>
.*?</tr>
}gsx) {
push @$list, {
month => $1,
day => $2,
hour => $3,
min => $4,
title => $5,
body => $6,
};
}
$list;
}
1;
plugins:cronで5分間隔で動かしています。
- module: CustomFeed::LDTraffic
- module: Filter::Rule
rule:
expression: $args->{entry}->title =~ m/(京浜東北|東海道)/; ←自分に関係する路線に絞っています
module: Deduped
- module: Publish::Twitter
config:
username: Twitterのユーザ名
password: Twitterのパスワード
このエントリーのトラックバックURL:
http://www.simazaki.com/~izumi/cgi-bin/tt-cgi/tt_tb.cgi/489
野良プラグインということでしたが、便利だったので紹介させていただきました。私がエンコードで躓いたので、その内容と、他のplaggerに関するメモを記録してあります。
本日もJava日和 : 2007年9月4日 23:07
| 日 | 月 | 火 | 水 | 木 | 金 | 土 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
| 29 | 30 | 31 |