Perl+Twitter : Getting @mentions from command line

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

5 thoughts on “Perl+Twitter : Getting @mentions from command line”

  1. You could also dispense with curl entirely by using Net::Twitter (http://search.cpan.org/perldoc?Net::Twitter) to handle interaction with the Twitter API. Going that route would also handle the JSON decoding automatically for you.

    Something along the lines of this should do it (code untested, but very similar to code I’ve used previously):

    #! /usr/bin/perl

    use strict;
    use warnings;

    use Net::Twitter;

    my $twit = Net::Twitter->new( { username => ‘MyUserName’, password => ‘MyPassword’ } );

    my $mentions = $twit->mentions;

    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
    ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<mentions to continue running indefinitely while periodically checking for new mentions.

  2. @Dave : Thanks for your input ! I was aware of that Net::Twitter module, but I felt like re-inventing the wheel 🙂 Of course if I was to do anything serious with twitter in Perl, I’d use the module !

  3. Another pretty simple way to go is just use LWP::UserAgent to query the twitter REST api instead of using curl for the download. Of course as Dave points out then you might as well look at using Net::Twitter. The advantage of combining your approach with LWP::UserAgent (or LWP::UserAgent with XML:LibXML) is that it’s a great way to learn how the underlying protocols work.

  4. Hello Phil,

    It’s was more a question of balance between keeping the code small, and adding module dependencies. For what my code snippet does, I’d say I wouldn’t have gained much simplicity from using LWP::UserAgent (which I love, btw 🙂 ).

    Still, same as previous comment : if I’d wanted to make something more robust and / or efficient, I’d probably have been with Net::Twitter as it does everything.

    Thanks for your comment !

    Stephane

Leave a Reply

Your email address will not be published. Required fields are marked *