http://www.perlmonks.org?node_id=591733

Hi Everyone,

I really don't know how cool this is for you, but it is useful for me, so here it is. I'm an Google Adsense publisher (have sites with Adsense Banners), and was checking my adsense stats too often, so I needed a script that checked my adsense stats and presented the current values to me on a regular basis, so I stayed briefed, and didn't need to stop what I was doing to check the stats.

There is a plugin to firefox that already do this, but I use Galeon (and will not change for the moment), so that was not a choice.

For sometime I looked at the docs for perlgtk and verified that what I wanted was not (too much) hard to implement using it.

So, I created PerlGtk Adsense Checker, a small script that check keep running in the background, with a icon in the systray, and every 30 minutes checks my adsense stats and show a small popup with actual values.

I was putting where the code, but it is 220 lines long, and you can get it here, so I will not.

#!/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: $ye +stday\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 Check +er\n\n". "Month: $month\nYester +day: $yestday\n". "Today: $today (+$chan +ge)" ), 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/aggr +egate', { 'product' => 'afc', 'dateRange.dateRangeType' => 'si +mple', 'dateRange.simpleDate' => 'th +ismonth', '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,und +ef,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 yourpa +ssword\n"; exit(1); }

The script in not much complex, and uses Gtk2::TrayIcon, LWP::UserAgent, Crypt::SSLeay (for https) and Getopt::Long.

So... How often do you check your adsense account?

I hope you comment my use of Gtk2 (as this is my first try to it), and sugest improvement in the script, if you use Adsense. Thanks.

Update: Added the code as sugested by diotalevi. I still need an icon in the same directory, named adcheck.ico, that is bundled in the tgz with the script, but any icon will work.

Replies are listed 'Best First'.
Re: AdSense Checker
by diotalevi (Canon) on Dec 26, 2006 at 17:34 UTC

    That link is only good as long your web site is around. Post the code here in a <code></code> block. Just because your code is 200 lines long doesn't mean it's too long for here!

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊