This code snippet demonstrates how easy it is to collect your twitter @mentions in Perl, coupled with curl for simplicity.

In this script, we use the JSON Twitter API. Perl has a JSON module to make parsing or JSON streams a bliss. I also used formats for easy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#! /usr/bin/perl
 
use strict;
use warnings;
 
use JSON;
use Encode;
 
my $json = new JSON;
my $json_stream = join "", map {decode("UTF-8", $_)} <>;
 
my $mentions = $json->decode($json_stream);
 
foreach my $mention (@$mentions) {
	my $screen_name = encode("UTF-8", $mention->{user}->{screen_name});
	my $text = encode("UTF-8", $mention->{text});
	format =
@<<<<<<<<<<<<<<    ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$screen_name,      $text
                   ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ~~
				   $text
.
	write;
}

You’ll have to run it as demonstrated below :

1
2
3
4
5
6
7
8
9
kattoo@roadrunner /files/toSave/home/kattoo $ curl -sS -u YourLoginHere:YourPasswordHere http://twitter.com/statuses/mentions.json | twitter-mentions.pl 
divarvel           @killermouse0 mon /boot est pas dans une
                   partition à part, et pour une install en
                   chroot je ne sais pas quoi faire (un autre
                   /boot ?)
fenice             @killermouse0 merci, c'est sympa. Tu tourne
                   sous Gentoo ?
[...]
kattoo@roadrunner /files/toSave/home/kattoo $

That’s all ! Of course this piece of code could use a lot of improvements, but this is intended as a kickstart sample… The curl part could have been directly integrated into the Perl code, but I like to keep it simple.

Now off to integrate this in my Conky layout ;-)

And by the way, you’ll find my twitter account on the right, in the “Follow me” section. Feel free to add me for updates !

Worth reading

January 24, 2010 at 11:56 am by Stephane Kattoor
Category: Dev
Tags: , , ,