Perl+Twitter : Getting @mentions from command line
stephane
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
#! /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 :
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
- Twitter : Send free SMS messages to your friends : now you can do this from the command line
- Scripting Twitter with cURL
- Scripting Twitter with Perl + LWP : a script in Perl doing the same thing (much simpler).
- Perl+Twitter : Getting @mentions from command line