* UPdated with new code HERE http://www.lancewicks.com/blog/index.ph ... 903-202313
[Updated: June 11th 2008 to include proper use of Twitter API]
I love Twitter, I love my Mac, I love Growl, I love TwitterPod and how it pops my Twitter updates in Growl alerts.
I have a Windows machine, I don't love my Windows machine. :(
It does not have Growl, I can't put twitterpod on it. :(
It does have Perl!!! :)
And it recently got Snarl, a free piece of software that aims to be a Growl equivalent for Windows.
So I decided to put the two together and try and get Growl type alerts working.
Below is a small Perl script I wrote, yes ugly, yes needs some experts to revise it, yes I could use XML::RSS to parse it better.
I have put a bunch of comments in it to make it clearer what is happening... hopefully.
Before running the Perl, you'll need to install Perl,Growl Snarl AND Snarl_cmd all the links are below the script itself.
---
---------------
Links:
Perl is available all over the place, for Windows try Strawberry ( http://strawberryperl.com/ ) or Activestate ( http://www.activestate.com/Products/activeperl/ ).
Snarl is available for free from http://www.fullphat.net/
Snarl_cmd is available from http://tlhan-ghun.de/?q=node/59
[Updated: June 11th 2008 to include proper use of Twitter API]
I love Twitter, I love my Mac, I love Growl, I love TwitterPod and how it pops my Twitter updates in Growl alerts.
I have a Windows machine, I don't love my Windows machine. :(
It does not have Growl, I can't put twitterpod on it. :(
It does have Perl!!! :)
And it recently got Snarl, a free piece of software that aims to be a Growl equivalent for Windows.
So I decided to put the two together and try and get Growl type alerts working.
Below is a small Perl script I wrote, yes ugly, yes needs some experts to revise it, yes I could use XML::RSS to parse it better.
I have put a bunch of comments in it to make it clearer what is happening... hopefully.
Before running the Perl, you'll need to install Perl,
---
#First up load the relevant modules some are from CPAN
use XML::Simple;
use Array::Diff;
use Data::Dumper;
use IPC::Open3;
use LWP::UserAgent;
# These two are as per Damian Conways Perl Best Practices and replaces $| =1;
use English qw( -no_match_vars );
local $OUTPUT_AUTOFLUSH = 1;
# So a scaler to hold my Twitter feed Url (follow me @ www.twitter.com/lancew)
my $rss_address = 'http://twitter.com/statuses/friends_timeline/73963.rss';
# A little print statement to tell me what the code is up to.
print "Getting original data set...\n";
# Grab the RSS from the internet and store it in anotherr scaler
my $rss_old = $ua1->request($req1)->as_string;
# Start a never ending While loop.
while (1) {
# Pause for a while (about 200 seconds) between cycles so there is time for your feeds to update
print "Pausing";
for ($count = 200; $count >= 1; $count--) {
print ".";
sleep(1);
}
# Now grab a new version of the RSS feed and store it in a scaler
print "\nGetting new dataset...\n";
my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new(GET => $rss_address);
$req->authorization_basic('<user>', '<pass>');
my $rss_new = $ua->request($req)->as_string;
# I then split the scalers on the <ITEM> tag into arrays
@old = split /<item>/, $rss_old;
@new = split /<item>/, $rss_new;
# Then I run the two arrays through the ARRAY::DIFF modules
my $diff = Array::Diff->diff( \@old, \@new );
# Print out how many differences there are
print "\n".$diff->count."\n";
# Now set the old_rss scaler to equal the new one, so when we cycle next it has the RSS contents from now.
$rss_old = $rss_new;
# Look at the number of differences from ARRAY::DIFF and do some stuff if there is more than none
if ($diff->count > 0){
# Loop through an array of the differences addded (which I reverse to get the correct order)
foreach (reverse(@{$diff->added})) {
#Find the title of the twitter post, by regex'ing between the title tags
$_ =~ /<title>(.+)<\/title>/;
#then print it out to the console
print $1.".\n";
#Next we push it out to Snarl
# I use IPC::Open3 as per PBP, but you could use back ticks I guess
# So the cmd here creates a Snarl message that remains for 8 seconds with a title Twitter. $1 is what we found in the regex above
my $cmd = "\"C:\\Program Files\\Snarl_CMD_0.1\\Snarl_CMD.exe\" snShowMessage 8 \"Twitter\" \"$1\"";
my $pid = open3($wtr, $rdr, $err, $cmd);
my @response = <$rdr>;
#Then before we go onto the next new twitter post (tweet) wait for 1 second. This makes the growls appear/disapear one after another
sleep(1);
} # end of the foreach diff->added loop
} # end of the if $diff->count > 0 block
} # end of the while(1) loop
---------------
Links:
Perl is available all over the place, for Windows try Strawberry ( http://strawberryperl.com/ ) or Activestate ( http://www.activestate.com/Products/activeperl/ ).
Snarl is available for free from http://www.fullphat.net/
Snarl_cmd is available from http://tlhan-ghun.de/?q=node/59