AJAX utilities
#!/usr/bin/perl use strict; use warnings; use LWP::Simple; use Mac::Growl; # # Configuration # my $remote_counter_file = "http://www.mahesha.com/Count.js"; my $local_count_file = $ENV{'HOME'} . "/.mahesha_com_count"; my $check_interval = 60; # every minute # # Register notifiacations # Mac::Growl::RegisterNotifications("MaheshaComHits", ["mahesha.com hit count"], ["mahesha.com hit count"]); while (1) { # # Check after regular interval # sleep($check_interval); # # Get the count in remote counter file # my $count_js = get ($remote_counter_file); if (!defined $count_js) { # # Notify error if remote file couldn't be fetched # Mac::Growl::PostNotification("MaheshaComHits", "mahesha.com hit count", "mahesha.com Error!", "Could not fetch Counter!"); next; # and bail } my ($remote_count) = $count_js =~ /Hits to date: (\d+)/; # # Get count from the local count file # my $local_count; if (!open (CNT, $local_count_file)) { Mac::Growl::PostNotification("MaheshaComHits", "mahesha.com hit count", "mahesha.com Error!", "No local counter file!"); next; } else { chomp ($local_count = <CNT>); $local_count = int $local_count; close (CNT); } # # Check if the local count is latest (after startup/wake) # my $now_time = time; my $mod_time = (stat($local_count_file))[9]; my $not_latest = ($now_time - $mod_time) > (2 * $check_interval); # # If the remote counter has advanced, notify. # if ((defined $remote_count) && (defined $local_count)) { if (($remote_count != $local_count) || ($not_latest)) { my $reason = ($remote_count != $local_count) ? "New hits!" : "Update"; my $sticky = ($remote_count != $local_count); Mac::Growl::PostNotification("MaheshaComHits", "mahesha.com hit count", "mahesha.com hits:", "$remote_count so far!\n$reason", $sticky); # # Update the local count file with advanced count # if (!open (CNT, ">" . $local_count_file)) { Mac::Growl::PostNotification("MaheshaComHits", "mahesha.com hit count", "mahesha.com Error!", "Can't write local counter file!"); next; } else { print CNT $remote_count . "\n"; close (CNT); } } } # # Update access/modification times of the file to current time # utime undef, undef, $local_count_file; }