-
-
Save turugina/872072 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!perl | |
use strict; | |
use warnings; | |
use utf8; | |
use XML::RSS; | |
use Growl::Any; | |
use LWP::UserAgent; | |
use File::Temp qw/:POSIX/; | |
use HTTP::Status qw/:constants/; | |
use Scope::Guard; | |
my $uri = 'http://tenki.jp/component/static_api/rss/earthquake/recent_entries_by_day.xml'; | |
my $growl = Growl::Any->new( appname => '地震速報', events => ['地震'] ); | |
my $ua = LWP::UserAgent->new; | |
$ua->env_proxy; | |
my $rss = XML::RSS->new; | |
my %links; | |
while (1) { | |
my $file = tmpnam; | |
my $g = Scope::Guard->new( sub { unlink $file if -e $file } ); | |
my $res = $ua->mirror($uri, $file); | |
if ( $res->code == HTTP_NOT_MODIFIED ) { | |
print "not modified: @{[scalar localtime]}\n"; | |
next; | |
} | |
if (!$res->is_success ) { | |
$growl->notify( 'Error', 'fetch feed', $res->status_line ); | |
next; | |
} | |
my $feed; | |
eval { $feed = $rss->parsefile($file); }; | |
if ( !$feed or $@ ) { | |
$growl->notify( 'Error', 'parse feed', $@||'' ); | |
next; | |
} | |
for my $entry ( @{$feed->{items}}[0..4] ) { | |
next if exists $links{$entry->{"link"}}; | |
my $title = $entry->{title}; | |
my $description = $entry->{description}; | |
$description =~ s/<[^>]*>//sg; | |
$growl->notify( '地震', $title, $description ); | |
$links{$entry->{"link"}} = undef; | |
} | |
} | |
continue { | |
sleep 30; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment