もはやいみふ。
#!/usr/bin/perl package Hatena::Counter; use strict; use warnings; use Carp; use WWW::Mechanize; use WWW::Mechanize::DecodedContent; use URI; use Web::Scraper; use Encode; use YAML; use Data::Dumper; use Time::Local; use Time::localtime; use Smart::Comments; sub new() { my ($class) = @_; bless { year => $_[1], month => $_[2], is_recent => $_[3], }, $class; } sub get_limit_day() { my $self = shift; my $year = $self->{year}; my $month = $self->{month}; my $tm = timelocal( 0, 0, 0, 1, $month, $year ) - 60 * 60 * 24; return ( localtime($tm)->mday ); } sub get_count() { my $self = shift; my $year = $self->{year}; my $month = $self->{month}; my $mech = new WWW::Mechanize( autocheck => 1 ); $mech->get('https://www.hatena.ne.jp/login'); $mech->submit_form( fields => { name => 'めあど', password => 'ぱす', }, ); if ( length($month) == 1 ) { $month = "0$month"; } my $count; if ( $self->{is_recent} == 1 ) { $mech->get('http://counter.hatena.ne.jp/syou6162/?cid=1'); $count = scraper { process 'div#dailyreport>div.report_table>table.report>tr>td', 'count[]' => 'TEXT'; result qw/count/; } ->scrape( $mech->decoded_content ); } if ( $self->{is_recent} == 0 ) { my $url = 'http://counter.hatena.ne.jp/syou6162/report?cid=1&date=' . $year . '-' . $month . '-01&mode=access&type=monthly'; $mech->get($url); $count = scraper { process 'div.report>div.report_table>table.report>tr>td', 'count[]' => 'TEXT'; result qw/count/; } ->scrape( $mech->decoded_content ); } my %counter = (); my $newcount; if ( $self->{is_recent} == 0 ) { foreach ( my $i = 0 ; $i < get_limit_day() * 3 ; $i = $i + 3 ) { @$newcount[$i] = @$count[$i]; @$newcount[ $i + 1 ] = @$count[ $i + 1 ]; @$newcount[ $i + 2 ] = @$count[ $i + 2 ]; } } $count = $newcount; foreach ( my $i = 0 ; $i < scalar(@$count) ; $i = $i + 3 ) { my @days = split( /\//, @$count[$i] ); $counter{ timelocal( 0, 0, 0, $days[1], $days[0] - 1, $year ) } = @$count[ $i + 2 ]; } return (%counter); } sub get_date_and_count() { my $self = shift; my $year = $self->{year}; my $month = $self->{month}; my %counter = @_; my $mday; my $mon; my $wday; my $text; my $date; foreach my $day ( sort keys %counter ) { $date = scalar( localtime($day) ); $mday = $date->mday; $mon = $date->mon + 1; $wday = $date->wday; $text .= "$mday,$mon,$wday,$counter{$day}\n"; } return ($text); } package main; my $counter = Hatena::Counter->new( 2007, 10, 0 ); $counter->{month} = 8; my %counter = $counter->get_count(); print $counter->get_date_and_count(%counter);