Skip to content

Instantly share code, notes, and snippets.

@turugina
Forked from mattn/jisin-growler.pl
Created March 16, 2011 05:46
Show Gist options
  • Save turugina/872072 to your computer and use it in GitHub Desktop.
Save turugina/872072 to your computer and use it in GitHub Desktop.
#!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