あたらしものずきっ!

試してみたものとか、遊んでみたものを色々記してみます。

壁紙スクレイピングなどしてみた

Galge.comが更新停止、というつぶやきをTwitterでみかけて、そんなにすぐに配布停止になるとは思えないけど、忘れた頃に…、というのは避けたかったので、壁紙をスクレイピングでダウンロードしてた。

#!/usr/local/bin/perl
use warnings;
use strict;
use LWP::UserAgent;
use URI;
use Web::Scraper;
use Time::HiRes;
use Path::Class qw/file/;

my $TimeSpan = 10.0;
my $galge_web = "http://www.galge.com/galge/nomember/sbp/wallpaper/index.html";
my $wp_dir = $ENV{'HOME'}."/Pictures/galge/";

my $scrap_home = scraper {
    process '//div[@id="indexmonth"]//li[last()]/a', 'link[]' => '@href';
};
my $year_res = $scrap_home->scrape( URI->new($galge_web) );
for my $year_link (@{$year_res->{link}}) { 
    warn $year_link;
    my $year_home = scraper {
        process '//table[@class="txt12k"]//tr[1]//td[4]/a', 'link[]' => '@href';
    };
    my $month_res = $year_home->scrape( URI->new($year_link) );
    Time::HiRes::sleep($TimeSpan);
    
    for my $month_link (@{$month_res->{link}}) {
        warn $month_link;
        my $month_home = scraper {
            process '//table[@class="txt12bldw"]/tr[1]//a', 'link[]' => '@href';
        };
        my $wp_res = $month_home->scrape( URI->new($month_link) );
        Time::HiRes::sleep($TimeSpan);
        
        for my $wp (@{$wp_res->{link}}) { 
            warn $wp;
            my @path_split = split(/\//, $wp);
            my $filename = $path_split[@path_split - 1];
            warn $filename;
            my $ua = LWP::UserAgent->new;
            my $path = file($wp_dir, $filename)->stringify;
            warn $path;
            $ua->get($wp, ":content_file" => $path);
            Time::HiRes::sleep($TimeSpan);
        }
    }
}

サーバの負担にならないようにと、アクセス毎に10秒間だけ間を開けてます。しょうもないことに全力を奮ったなぁと久々に思った。