理系学生日記

おまえはいつまで学生気分なのか

使ってみたかったモジュール使ってみた

明日からはもう定時で帰るんだ。

use strict;
use warnings;
use feature 'switch';

use URI;
use Try::Tiny;
use Log::Log4perl;
use Web::Scraper;
use YAML::Any;
use Config::Any;

my $VERSION = '0.01';

my $cfgfile = 'conf/fetch.yaml';
my $logger;

my $ranking = try {
    Log::Log4perl->init("conf/log4j.conf");
    $logger = Log::Log4perl->get_logger( 'My::Fetch' );
    $logger->info("start fetching ranking data");

    my $conf = Config::Any->load_files({
        files           => [$cfgfile], 
        use_ext         => 1,
        flatten_to_hash => 1,
    });

    $logger->info("retrieve from $conf->{$cfgfile}->{url}");

    my $scraper = scraper {
        process '//tr[@class="tbg1" or @class="tbg2"]',
            'records[]' => scraper {
                process '//th',        'rank'   => 'TEXT';
                process '//td[1]',     'status' => 'TEXT';
                process '//td[2]',     'title'  => 'TEXT';
                process '//td[3]',     'artist' => 'TEXT';
            };
        result 'records';
    };
    $scraper->user_agent->agent("My::Fetch/$VERSION");
    $scraper->scrape( URI->new( $conf->{$cfgfile}->{url} ) );
} catch {
    when (/Cannot open config file/) { print; exit 1; };               # handles Log4perl error 
    when (/GET .* failed: /)         { $logger->error($_); exit 2; };  # handles Web::Scraper error
    default { 
        $logger->error( $_ );
    };
} finally {
    $logger->info("complete") if $logger;
};