Query Twitter data using Perl


Search Twitter using Perl module Net::Twitter

Pre-requisites

I strongly recommend to read below url before playing around this example

Streaming API Concepts

Net::Twitter – A perl interface to the Twitter API

Streaming API Response Codes

#!/usr/bin/perl -w

use Net::Twitter;
use Scalar::Util 'blessed';

my $nt = Net::Twitter->new(
      traits                 => [qw/OAuth API::REST API::Search/],
      consumer_key           => "PZBzeiUFkzZ0vF4bNY88rQ",
      consumer_secret        => "2VjWny5UTCk4eIde6GBUFdRsCwiOX6jIgQCPmT6VA",
      access_token           => "349126638-MZ0fCAYRQnyarDjsph5sXtOZBF0h6TjbXjxUXHGn",
      access_token_secret    => "r0SyKoqU91uMksfXD8mTPOI9k0h1UkOkb5rH1LVJh8",
      ssl                    => '1',
      decode_html_entities   => '1',
);

eval {
      my $response = $nt->search("India");

      print
      $response->{'results'}->[$_]->{'created_at'}," - ",
      $response->{'results'}->[$_]->{'from_user'}," - ",
      $response->{'results'}->[$_]->{'text'}, "\n\n" foreach (1..10);
};

if ( my $err = $@ ) {
      print "12345";
      die $@ unless blessed $err && $err->isa('Net::Twitter::Error');
}

It will search “India” in latest tweets

Output format

Date TimeUser – Tweet

Mon, 07 May 2012 07:27:37 +0000 - kpark_kunal - @iSaakshi I doubt!india has very concentrated oil importers! Saudi, Iran,kuwait, Nigeria contributing more than 80%(source:PPAC) @Equateall

Mon, 07 May 2012 07:27:37 +0000 - kpark_kunal - @iSaakshi I doubt!india has very concentrated oil importers! Saudi, Iran,kuwait, Nigeria contributing more than 80%(source:PPAC) @Equateall

Mon, 07 May 2012 07:27:36 +0000 - goonergurl20 - @ReiceCookieCook Pls sign this petition & help us #IndianGooners to get ESPN to telecast WBA vs. Arsenal LIVE in India! http://t.co/fq8qcKbQ

Mon, 07 May 2012 07:27:34 +0000 - WildNavigator - @sophontrack :) have it before going to sleep. In India we call this grandmothers medicinal therapies - it works wonders :) plants are gr8

Mon, 07 May 2012 07:27:33 +0000 - NattaliaLopeez - Perra, gata, zorra, wila, lobuki, puta, pendeja, india .i. Eso eres y mas >:O

Mon, 07 May 2012 07:27:33 +0000 - NandiniNitwh02 - Amarnath Yatra 2012 http://t.co/HFXZA28n via @AmarnathYatra1

Mon, 07 May 2012 07:27:32 +0000 - RealRediffCom - AI to debut Dreamliner on domestic routes: Air India says it would be operating its new Boeing-787 Dreamliner ai... http://t.co/FnibB4Kd

Mon, 07 May 2012 07:27:32 +0000 - RealRediffCom - Cut down Iranian oil purchases: Hillary to India: US Secretary of State Hillary Clinton on Monday urged India to... http://t.co/cX3hXbtl

Mon, 07 May 2012 07:27:31 +0000 - korean_fanatics - Yuri sebagai anak pemimpin desa India dalam kehidupan sebelumnya. Tidak heran ia memiliki kulit coklat susu #yulfact

Mon, 07 May 2012 07:27:30 +0000 - CGMANTRA - #CGMantra is the only academy in #India offering training in all domains of #Entertainment & #Media Sector.

Mon, 07 May 2012 07:27:29 +0000 - goonergurl20 - @KeroBlack Pls sign this petition & help us #IndianGooners to get ESPN to telecast WBA vs. Arsenal LIVE in India! http://t.co/fq8qcKbQ

 

Have fun!