Skip to content

Instantly share code, notes, and snippets.

@kiyotune
Last active December 14, 2015 11:38
Show Gist options
  • Save kiyotune/5080304 to your computer and use it in GitHub Desktop.
Save kiyotune/5080304 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use Cache::Memcached::Fast;
use LWP::UserAgent;
use HTML::TreeBuilder;
use FindBin;
use File::Path 'mkpath';
use constant URL=>'http://www.jma.go.jp/jp/g3/'; #天気図
use constant UA=>'Mozilla/5.0 (Windows NT 6.1; rv:19.0) Gecko/20100101 Firefox/19.0';
use constant IMG=>$FindBin::Bin.'/data';
my $memd = Cache::Memcached::Fast->new({
servers => [ { address => 'localhost:11211' }],
namespace => 'weatherchart_jp:',
utf8 => 1,
});
#$memd->flush_all(); #キャッシュ削除 for Debug
my $ua = LWP::UserAgent->new('agent' => UA);
my $rq = new HTTP::Request(HEAD => URL);
my $rp = $ua->request($rq);
my $last_modified = $rp->header('Last-Modified');
if($last_modified ne $memd->get('Last-Modified')){
$memd->set('Last-Modified', $last_modified);
my $res = $ua->get(URL);
my $content = $res->content;
my $tree = HTML::TreeBuilder->new;
$tree->parse($content);
my $img_url = URL.$tree->look_down('id', 'info')->find('img')->attr('src');
$tree = $tree->delete;
my @path = split(/\//, $img_url);
my $img_name = $path[$#path]; # yymmddhh.png
my ($year, $mon, $mday, $hour) = ($img_name =~ /^(\d{2})(\d{2})(\d{2})(\d{2})/);
$year = "20".$year;
my $img_dir = sprintf(IMG."/%04d/%02d", $year, $mon);
unless(-d $img_dir){
umask(0);
mkpath($img_dir, 0, 0777);
}
$res = $ua->get($img_url);
$content = $res->content;
open(OUT, ">$img_dir/$img_name");
binmode OUT;
print OUT $content;
close(OUT);
print "($last_modified) [from] $img_url [to] $img_dir/$img_name\n"; # for Debug
}else{
print "($last_modified) skipped\n"; # for Debug
}
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment