読者です 読者をやめる 読者になる 読者になる

はてなカウンターから日付とユニークアクセス数を持ってくるperlスクリプト

はてな Perl 問題発見と解決

最新一ヶ月分を持ってくるスクリプト。

#!/usr/bin/perl

use strict;
use warnings;

use WWW::Mechanize;
use WWW::Mechanize::DecodedContent;
use URI;
use Web::Scraper;
use Encode;
use Time::Local;

my $mech = new WWW::Mechanize( autocheck => 1 );
$mech->get('https://www.hatena.ne.jp/login');

$mech->submit_form(
    fields => {
        name     => 'めるあど',
        password => 'ぱす',
    },
);
$mech->get('http://counter.hatena.ne.jp/syou6162/?cid=1');

my $links = scraper {
    process 'div#dailyreport>div.report_table>table.report>tr>td',
      'count[]' => 'TEXT';
    result qw/count/;
}
->scrape( $mech->decoded_content );

my %counter = ();

foreach ( my $i = 0 ; $i < scalar(@$links) ; $i = $i + 3 ) {
    my @days = split(/\//,@$links[$i]);
    $counter{ scalar(localtime(timelocal( 0, 0, 0, $days[1],$days[0]-1)))} = @$links[ $i + 2 ];
}

foreach my $day (sort keys %counter){
   print "\"$day\",$counter{$day}\n";
}

すると、こんな感じで出てきてハッピーである。

"Fri Nov  3 00:00:00 2000",55
"Fri Nov 10 00:00:00 2000",68
"Fri Nov 17 00:00:00 2000",127
"Fri Oct 27 00:00:00 2000",93
"Mon Nov  6 00:00:00 2000",94
"Mon Nov 13 00:00:00 2000",128
"Mon Nov 20 00:00:00 2000",160
"Mon Oct 30 00:00:00 2000",131
"Sat Nov  4 00:00:00 2000",108
"Sat Nov 11 00:00:00 2000",91
"Sat Nov 18 00:00:00 2000",136
"Sat Oct 28 00:00:00 2000",67
"Sun Nov  5 00:00:00 2000",119
"Sun Nov 12 00:00:00 2000",133
"Sun Nov 19 00:00:00 2000",228
"Sun Oct 29 00:00:00 2000",143
"Thu Nov  2 00:00:00 2000",90
"Thu Nov  9 00:00:00 2000",108
"Thu Nov 16 00:00:00 2000",212
"Thu Nov 23 00:00:00 2000",9
"Thu Oct 26 00:00:00 2000",90
"Tue Nov  7 00:00:00 2000",84
"Tue Nov 14 00:00:00 2000",105
"Tue Nov 21 00:00:00 2000",145
"Tue Oct 31 00:00:00 2000",119
"Wed Nov  1 00:00:00 2000",103
"Wed Nov  8 00:00:00 2000",99
"Wed Nov 15 00:00:00 2000",143
"Wed Nov 22 00:00:00 2000",143
"Wed Oct 25 00:00:00 2000",117

localtimeとか使いかた分からなくて激しくこまった。