#!/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; GetOptions("username=s" => \$username, "password=s" => \$password); help() unless $username and $password; my $event_number; my $msgid; my $window=undef; my ($month,$yestday,$today,$change)=(0,0,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 30*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' ); my $adsensedata=""; update_adsense_data(); Gtk2->main(); sub update_adsense_data { unless (adsense_login()) { error_login_popup(); return; } $adsensedata=adsense_info(); my $prevtoday=$today; my @lines=split /\n/, $adsensedata; $month=0;$yestday=0; $today=0; for my $line (@lines) { $line=~s/\0//g; next if $line =~/(Totals|Page)/; next if $line =~/\A\s*\Z/; my $money=(split /\s+/, $line)[-1]; $month+=$money; $yestday=$today; $today=$money; } if ($today<$prevtoday) { # Day changed - value for change=change on yesterday + value for today $change=$yestday-$prevtoday+$today } else { # Same day. $change=$today-$prevtoday; } $tips->set_tip($icon, "PerlGtk Adsense Checker\n\n". "Month: $month\nYesterday: $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\n\n". "Month: $month\nYesterday: $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 2 secs from now. 5*1000 => \&hide_box, ); } sub hide_box { Glib::Source->remove($msgid); if ($window) { $window->hide(); $window->destroy(); $window=undef; } } sub adsense_login { my $login=$ua->post('https://www.google.com/adsense/login.do', { destination => '', username => $username, password => $password, null => 'Login' } ); return $login->as_string=~/Invalid email address or password/?0:1; } 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', }, ); return $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); }