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!

Advertisements

Upgrade Perl modules using cpan autobundle


It’s become bi weekly task to keep updated your Perl distro.

To achieve it I use following commands

perl -MCPAN -e shell
cpan> autobundle

Above command will generate a snapshot pm file. This file will include list of modules require upgrade.

Example
Snapshot_2012_05_01_01.pm

You can recheck the file and list of module with respective version. You have a facility to remove any of unwanted packages to your distro.

Once done the modification of the file you save it.

Now run below command

cpan> install Bundle::Snapshot_2012_05_01_01

This command will take care of installation of all these module incorporated in Snapshot_2012_05_01_00 file.

Bingo thats all! WeW

Perl GMAIL Feed


#!/usr/bin/env perl
use warnings;
use strict;

=head1 INTRODUCTION

Checks if there are new unread messages in your GMail Inbox.

=head1 USAGE

$ perl check_gmail.pl
=cut

############## Configuration ##############

# Change this to your correct username.
use constant GMAIL_USERNAME => “username”;
# Change this to your correct password.
use constant GMAIL_PASSWORD => “password”;

########## Don’t change anything below this. ##########

use LWP::UserAgent;
use XML::Atom::Feed;

my $fetcher = LWP::UserAgent->new();
$fetcher->agent(“check_gmail.pl/0.01″);

my $request = HTTP::Request->new(
‘GET’   => “https://mail.google.com/gmail/feed/atom”,
);
$request->authorization_basic(GMAIL_USERNAME, GMAIL_PASSWORD);

my $response = $fetcher->request($request);

if (! $response->is_success())
{
die(“Unsuccessful in trying to talk to GMail”);
}

my $content = $response->content;
my $feed = XML::Atom::Feed->new(\$content);
my @new_messages = $feed->entries();

my $i = 1;
foreach my $message(@new_messages)
{
print join(“\t”, $i, $message->author->name,
$message->title), “\n”;
$i++;
}

# The End