gmailの受信量の可視化

gmailにメールが来る量が前より増えた気がして、そういうが増えるとちょっとやなのでこの一ヶ月が一ヶ月前とどうだったかグラフに出してみた。赤が今月、青が先月。

f:id:soh335:20110731165629p:image

そうでもなかった。

use strict;
use warnings;

use WWW::Mechanize;
use Config::Pit;
use DateTime;
use Getopt::Long;
use URI::GoogleChart;

my $config = pit_get('google', require => {
  "username" => "your username on google.com",
  "password" => "your password on google.com"
});

my $query = "";
GetOptions("query=s" => \$query);

my $mech = WWW::Mechanize->new;
$mech->get('https://mail.google.com/mail/');
$mech->submit_form(
    form_id => "gaia_loginform",
    fields => {
        Email =>  $config->{username},
        Passwd => $config->{password},
    }
);
$mech->get('https://mail.google.com/mail/?ui=html&zy=a');

my @y_axis = ();
my @p_y_axis = ();

my $date = DateTime->now( time_zone => 'Asia/Tokyo' );
my $p_date = $date->clone->subtract( months => 1 );

for (1 .. 30) {

    my $before_date = $date->ymd('/');
    my $after_date = $date->subtract( days => 1 )->ymd('/');

    my $p_before_date = $p_date->ymd('/');
    my $p_after_date = $p_date->subtract( days => 1 )->ymd('/');

    my $count = get_count($after_date, $before_date);
    my $p_count = get_count($p_after_date, $p_before_date);

    warn "after:$after_date before:$before_date count:$count";
    warn "p_after:$p_after_date p_before:$p_before_date p_count:$p_count";

    unshift @y_axis, $count;
    unshift @p_y_axis, $p_count;
}

my $u = URI::GoogleChart->new("lines", 700, 350,
    data => [ \@y_axis, \@p_y_axis, ],
    range_show => "left",
    range_round => 1,
    color => ["red", "blue"],
);

warn $u;

sub get_count {

    my ($after_date, $before_date) = @_;

    $mech->submit_form(
        form_name => "sf",
        fields => {
            q => sprintf 'after:%s before:%s'.$query, $after_date, $before_date
        }
    );

    detect_counts();
}

sub detect_counts {
    if ( $mech->content =~ /<b>\d+<\/b>&nbsp;-&nbsp;<b>\d+<\/b> of (about )?<b>(\d+)<\/b>/ ) {
        if ( defined $1 and $1 eq 'about ' ) {
            $mech->follow_link( text_regex => qr/Older/ );
            return detect_counts();
        }
        else {
            return $2;
        }
    }

    0;
}