Tuesday, May 31, 2011

Pull Twitter Trending Topics using Perl

This is useful for a wide variety of applications and it doesn't require you to sign up for an API key. The only limitation is Twitter only allows you to pull this information 150 times an hour. Other Twitter API calls may affect this limit as well.

#!/usr/bin/perl

use strict;
use warnings;
use JSON;

my $curl_string = `curl -s http://api.twitter.com/1/trends/1.json`;

my $json_result = from_json($curl_string);

foreach my $trend (@{$json_result->[0]->{'trends'}}) {
    print $trend->{'name'} . "\n";
}

This script requires that you install the Perl JSON module by Makamaka Hannyaharamitu. This can easily be done either by downloading and compiling it from cpan.org or by running the cpan command line interface and typing "install JSON".

I'm also using the curl binary to execute the request by executing a system call from inside Perl. You can also use the perl bindings for libcurl to execute the request.

1 comment:

  1. The Twitter REST API v1 is no longer active. Please migrate to API v1.1.

    ReplyDelete