RSS

Monthly Archives: February 2010

Knit your own Tweets

The Perl scripts that I was fiddling about with seem to be working the way I wanted them to (even if what’s calling them isn’t always), so here they are for anybody else who wants to use them.

queue_tweet.pl


#!/usr/bin/perl

use DateTime;
use Sys::Hostname;

my $time = DateTime->now()->strftime("%F %T");
my $tweet = "$time\n";
my $hostname = hostname;

$tweet .= "$hostname\n";

while(defined($line=<STDIN>)){
    $tweet .= $line;
}

$tweet .= "%%\n";

#print $tweet

$tweetqueuefile="/var/local/tweetqueue";

open(QUEUE,">>$tweetqueuefile") || die("Cannot open tweet queue");
print QUEUE $tweet;
close(QUEUE);

send_tweet.pl


#!/usr/bin/perl

use File::Copy;
use Net::Netrc;
use LWP::UserAgent;

$tweetqueuefile="/var/local/tweetqueue";

open(QUEUE,"$tweetqueuefile") || die("Cannot open tweet queue");

# Read lines of first tweet

my $tweet;

while(defined($line=<QUEUE>)){
    if($line eq "%%\n"){
        # Exit loop
        last;
    }
    $tweet .= $line;
}

if($tweet != ""){
#	print "Sending tweet:\n$tweet";
    $machine="twitter.com";
    $netrc = Net::Netrc->lookup($machine);
    my $ua = LWP::UserAgent->new;
    my $req = HTTP::Request->new(POST => 'https://twitter.com/statuses/update.xml');
    $req->authorization_basic($netrc->login, $netrc->password);
    $req->content('status='.$tweet);
    my $result = $ua->request($req)->as_string;
#	print $result;
}

$tempfile="/tmp/tweettmp";

open(TEMP,">$tempfile") || die("Cannot open temporary file");

while(defined($line=<QUEUE>)){
    print TEMP $line;
}

close(TEMP);
close(QUEUE);

copy("$tempfile", "$tweetqueuefile") || die("Cannot overwrite tweet queue");

(As I’ve mentioned before, it’s a long time since I’ve written any Perl, so I’ve probably made any number of schoolboy errors. Still, they seem to do the trick.)

queue_tweet.pl reads from stdin to queue a tweet, and send_tweet.pl reads and sends the first tweet from the queue each time it’s called. You’ll need your credentials for twitter.com in your .netrc file.

For example, I’ve got the following entries in my crontab:

# Send any pending tweets
*/5 * * * * /usr/local/bin/send_tweet.pl

# Log disk temperatures
30 */4 * * * /usr/sbin/hddtemp /dev/sda /dev/sdb | /usr/local/bin/queue_tweet.pl

# Motherboard temperatures
35 */4 * * * /usr/bin/sensors | /bin/grep Temp | /usr/bin/cut -c 1-21 | /usr/local/bin/queue_tweet.pl

Let me know if you use them for anything more interesting than tweeting your system temperatures, or if you’ve got any (constructive) criticisms of my Perl code…

Advertisements
 
1 Comment

Posted by on Tuesday 16 February 2010 in Geeky stuff

 

Tags: , ,

Perl before swine

It seems that I’ve forgotten everything I ever knew about Perl.

All I wanted to do was write a little Perl script that could be called to queue up informational tweets, and another to send queued tweets which would be called from a crontab every five minutes or so, but I’m having to look up almost every statement in the trusty Camel book. Programming, the slow way.

Update: Finally got the scripts working the way I wanted. I was going to use the Net::Twitter module, but trying to install it just resulted in errors relating to Compress::Zlib::gzopen, so it seemed easier to do things the long way round using LWP::UserAgent and HTTP::Request. Once I’ve got the scripts plugged in to my crontab and rcX.d scripts, I’ll post them here in case anybody’s trying to work out how to do the same thing.

 
Leave a comment

Posted by on Wednesday 3 February 2010 in Geeky stuff

 

Tags: , ,