No Description

adchecker.pl 6.9KB

    #!/usr/bin/perl -w # PerlGtk Adsense Checker v.0.1 # Author: theMage # URL: http://magick-source.net # License: GPL v.2 use strict; use LWP::UserAgent; use HTTP::Cookies; use Gtk2::TrayIcon; use Gtk2 -init; use Glib qw/TRUE FALSE/; use Getopt::Long; my $username=undef; my $password=undef; my $debug=0; our $VERSION="0.1.3"; GetOptions("username=s" => \$username, "password=s" => \$password, "debug+" => \$debug); help() unless $username and $password; my $event_number; my $msgid; my $window=undef; my ($month,$yestday,$today,$change,$days,$lmonth,$lmdays)=(0,0,0,0,0,0,0); my ($avg,$lmavg)=(0,0); my $icon=Gtk2::TrayIcon->new('googleadsense'); # The eventbox, to get the events as mouse In/Out my $eventbox=Gtk2::EventBox->new(); # The icon that apears in the SysTray. my $imgfile=$0; $imgfile=~s{[^/]+$}{adcheck.ico}; my $img=Gtk2::Image->new_from_file($imgfile); $eventbox->add($img); $event_number = Glib::Timeout->add( # Call each 15 min=15*60 secs*1000 milisecs 20*60*1000 => \&update_adsense_data, ); my $tips=Gtk2::Tooltips->new(); # Add the popup menu, with Quit. my $menu=Gtk2::Menu->new(); my $menuquit=Gtk2::ImageMenuItem->new_from_stock('gtk-quit',undef); $menuquit->signal_connect('activate' => sub {Gtk2->main_quit();}); $menuquit->show(); $menu->append($menuquit); $eventbox->signal_connect('button-release-event'=>\&show_menu); $icon->add($eventbox); $icon->show_all; my $ua=LWP::UserAgent->new( cookie_jar=>HTTP::Cookies->new(), agent=>"GTK2 Adsense Checker $VERSION" ); my $adsensedata=""; my $lmadsensedata=""; update_adsense_data(); Gtk2->main(); sub update_adsense_data { unless (adsense_login()) { error_login_popup(); return; } ($lmadsensedata,$adsensedata)=adsense_info(); my $prevtoday=$today; my @lines=split /\n/, $adsensedata; $month=0;$yestday=0; $today=0; $days=0; $lmonth=0;$lmdays=0; for my $line (@lines) { $line=~s/\0//g; next if $line =~/(Page|Average)/; if ($line =~/Total/) { $month=(split /\s+/, $line)[-1]; next; } next if $line =~/\A\s*\Z/; $days++; my $money=(split /\s+/, $line)[-1]; $yestday=$today; $today=$money; } @lines=split /\n/, $lmadsensedata; for my $line (@lines) { $line=~s/\0//g; next if $line =~/(Page|Average)/; if ($line =~/Total/) { $lmonth=(split /\s+/, $line)[-1]; next; } next if $line =~/\A\s*\Z/; $lmdays++; } if ($today<$prevtoday) { # Day changed - value for change=change on yesterday + value for today $change=sprintf("%.2f",$yestday-$prevtoday+$today); } else { # Same day. $change=sprintf("%.2f",$today-$prevtoday); } $avg=sprintf("%.3f",$month/$days); $lmavg=sprintf("%.3f",$lmonth/$lmdays); $tips->set_tip($icon, "PerlGtk Adsense Checker $VERSION\n\n". "Last Month: $lmonth [$lmdays @ $lmavg ]\n". "Month: $month [$days @ $avg ]\n". "Yesterday: $yestday\n". "Today: $today(+$change)\n". "\nUpdated at:\n". scalar localtime()); $window=Gtk2::Window->new('popup'); $window->set_position('center'); my $frame = Gtk2::Frame->new('Adsense'); $frame->set_border_width(3); my $box=Gtk2::VBox->new(); $box->pack_end( Gtk2::Label->new( "PerlGtk Adsense Checker $VERSION\n\n". "Last Month: $lmonth [$lmdays @ $lmavg ]\n". "Month: $month [$days @ $avg ]\n". "Yesterday: $yestday\n". "Today: $today (+$change)" ), FALSE, FALSE, 5, ); my $hbox=Gtk2::HBox->new(); $hbox->pack_end($box, FALSE, FALSE, 5); $frame->add($hbox); $window->add($frame); $window->show_all(); $msgid = Glib::Timeout->add( # Call 1 secs from now. 1*1000 => \&hide_box, ); } sub hide_box { Glib::Source->remove($msgid); if ($window) { $window->hide(); $window->destroy(); $window=undef; } } sub adsense_login { my $loginform=$ua->get('https://www.google.com/accounts/ServiceLoginBoxAuth', { 'continue' => 'https://www.google.com/adsense/login-box-gaiaauth', foolowup => 'https://www.google.com/adsense/login-box-gaiaauth', service => 'adsense', ltmpl => 'login', rm => 'false', Email => $username, Passwd => $password, null => 'Login' } ); print STDERR "LoginForm:\n", $loginform->as_string(),"\n-----------\n" if $debug; my ($galx)=$loginform->as_string=~m{name="GALX"\s+value="([^"]+)">}msg; $galx='' unless $galx; my $login=$ua->post('https://www.google.com/accounts/ServiceLoginBoxAuth', { 'continue' => 'https://www.google.com/adsense/login-box-gaiaauth', foolowup => 'https://www.google.com/adsense/login-box-gaiaauth', service => 'adsense', ltmpl => 'login', rm => 'false', Email => $username, Passwd => $password, GALX => $galx, null => 'Login' } ); print STDERR "Login:\n", $login->as_string(),"\n-----------\n" if $debug; if ($login->as_string=~/CheckCookie/) { $login=$ua->get('https://www.google.com/accounts/CheckCookie?continue=https%3A%2F%2Fwww.google.com%2Fadsense%2Flogin-box-gaiaauth&service=adsense&ltmpl=login&chtml=LoginDoneHtml'); if ($login->as_string=~/SetSID/) { my ($uri)=$login->as_string=~/Refresh:\s+\d+;\s*url\='([^']+)'/; $login=$ua->get($uri); return 1 if $login->as_string=~/AdSense - Reports/; } } return 0; } sub adsense_info { my $data=$ua->post('https://www.google.com/adsense/report/aggregate', { 'product' => 'afc', 'dateRange.dateRangeType' => 'simple', 'dateRange.simpleDate' => 'thismonth', 'groupByPref' => 'date', 'reportType' => 'property', 'null' => 'Display+Report', 'outputFormat' => 'TSV_EXCEL', }, ); my $ldata=$ua->post('https://www.google.com/adsense/report/aggregate', { 'product' => 'afc', 'dateRange.dateRangeType' => 'simple', 'dateRange.simpleDate' => 'lastmonth', 'groupByPref' => 'date', 'reportType' => 'property', 'null' => 'Display+Report', 'outputFormat' => 'TSV_EXCEL', }, ); return ($ldata->content, $data->content); } sub show_menu { my ($widget,$event) = @_; my $button_nr = $event->button; #make sure it was the right mouse button ($button_nr == 3)&&($menu->popup(undef,undef,undef,undef,0,0)); } sub error_login_popup { $window=Gtk2::Window->new('popup'); $window->set_position('center'); my $frame=Gtk2::Frame->new('Login Error'); $frame->set_border_width(3); my $box=Gtk2::VBox->new(); $box->pack_end( Gtk2::Label->new("Email or password invalid.\n\n". "Usage:\n$0 --username adsemseaccount\@yourdomain.com". " --password yourpassword\n" ), FALSE, FALSE, 5, ); my $hbox=Gtk2::HBox->new(); $hbox->pack_end($box, FALSE, FALSE, 5); $frame->add($hbox); $window->add($frame); $window->show_all(); $msgid=Glib::Timeout->add( 10*1000 => \&hide_and_quit, ); } sub hide_and_quit { Glib::Source->remove($msgid); if ($window) { $window->hide(); $window->destroy(); $window=undef; } Gtk2->main_quit(); } sub help { print "Usage:\n", "\t$0 --username your\@adsenseacount --password yourpassword\n"; exit(1); }