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

まだまだひどいスクリプト

Perl 問題発見と解決

こいつちょっと進化させて、今年のアクセスのユニークアクセス数を片っ端から持ってきてみた。コードがひどい。

#!/usr/bin/perl

use strict;
use warnings;

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 get_limit_day() {
    my $year  = shift;
    my $month = shift;
    my $tm    = timelocal( 0, 0, 0, 1, $month, $year ) - 60 * 60 * 24;
    return ( localtime($tm)->mday );
}

sub get_recent_count() {
    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 $count = 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(@$count) ; $i = $i + 3 ) {
        my @days = split( /\//, @$count[$i] );
        $counter{ timelocal( 0, 0, 0, $days[1], $days[0] - 1, 2007 ) } =
          @$count[ $i + 2 ];
    }
    return (%counter);
}

sub get_month_count() {
    my $year  = shift;
    my $month = shift;
    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 $url =
        'http://counter.hatena.ne.jp/syou6162/report?cid=1&date=' 
      . $year . '-'
      . $month
      . '-01&mode=access&type=monthly';
    $mech->get($url);
    my $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;
    foreach ( my $i = 0 ;
        $i < &get_limit_day( $year, $month ) * 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 $year    = shift;
    my $month   = shift;
    my %counter = &get_month_count( $year, $month );
    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);
}

for ( 1 .. 10 ) {
    print &get_date_and_count( 2007, $_ );
}