RSS Feed to Update Twitter Status using Perl Script with OAuth Part-II (Installation of Perl Modules, writing perl script and Integrating it with OAuth)

_
In previous article “RSS Feed to Update Twitter Status using Perl Script with OAuth Part-I (Registration of Twitter Application)” we have learn to register a twitter application and today I am going to tell how to use that application to update your twitter status with the help of a Perl Script which will fetch data from a RSS Feed.

This tutorial is divided in following sub parts or sections
1) Installation of Perl Modules.
2) Installation and understanding of per script.
3) Enabling Keys and setting up cronjob.

1) Installation of Perl Modules

To execute the perl script we need some Perl Modules to be installed on the server. Following is the list of modules which should be available on the server

  • Net::Twitter::Lite : Perl module for Twitter API modules.
  • XML::RSS::Parser::Lite : Perl RSS parser which will be used for RSS Feeds.
  • XML::Parser::Lite : Lightweight regexp-based XML parser and dependancy for XML::RSS::Parser::Lite
  • LWP::Simple : dependancy for XML::RSS::Parser::Lite
  • WWW::Shorten::Bitly : Interface to shortening URLs using URL shortening site bit.ly

 

You can install these modules using command “cpan [module name]“. e.g. is you want to install Net::Twitter::Lite you have to execute the command “cpan Net::Twitter::Lite

2) Installation and understanding of per script

Copy and paste following script on your server. This script will work as
1) Fetch Details from RSS Feed

  1. Title of the story
  2. URL of the story

2) URL of the story will be sent to Bit.ly using Perl module for URL Shortening
3) Perl Module will read the Shorten URL
4) Script will connect to Twitter and update status of twitter account with Title and shorten URL.

Script:-

#!/usr/bin/perl -w
use Net::Twitter::Lite;
use XML::RSS::Parser::Lite;
use LWP::Simple;
use WWW::Shorten::Bitly ;
my $YOUR_CONSUMER_KEY = “[YOUR_CONSUMER_KEY]” ;
my $YOUR_CONSUMER_SECRET = “[YOUR_CONSUMER_SECRET]” ;
my $YOUR_ACCESS_TOKEN = “[YOUR_ACCESS_TOKEN]“;
my $YOUR_ACCESS_TOKEN_SECRET = “[YOUR_ACCESS_TOKEN_SECRET]” ; 

my $nt = Net::Twitter::Lite->new(
consumer_key => $YOUR_CONSUMER_KEY,
consumer_secret => $YOUR_CONSUMER_SECRET,
access_token => $YOUR_ACCESS_TOKEN,
access_token_secret => $YOUR_ACCESS_TOKEN_SECRET,
);

my $set_post = &get_rss_post() ;
exit;

sub get_rss_post{
my $xml = get(“[RSS FEED URL]“);
my $rp = new XML::RSS::Parser::Lite;
$rp->parse($xml);
for (my $i = 0; $i < $rp->count(); $i++) {
my $it = $rp->get($i);
my $url = $it->get(‘url’);
my $bitly = WWW::Shorten::Bitly->new(URL => $url, USER => “[USERNAME]“, APIKEY => “[API KEY]“);
my $temp = $bitly->shorten(URL => $url);
my $post = $it->get(‘title’). ” ” .$temp;
eval{
$nt->update($post);
};
if ( $@ ) {
warn “update failed because: $@\n”;
}
}
return ;
}

3) Enabling Keys and setting up cronjob

As I mentioned in previous article to save the values of Consumer key, Consumer secret, Access Token (oauth_token) and Access Token Secret (oauth_token_secret); if you didn’t then don’t worry login into Twitter developer site i.e. http://dev.twitter.com and go to your Application. There you will find all the keys of your application. Once you have all the keys, its time to edit the above script. Make Following changes

a) Replace [YOUR_CONSUMER_KEY] with Consumer key, [YOUR_CONSUMER_SECRET] with Consumer secret, [YOUR_ACCESS_TOKEN] with Access Token (oauth_token), [YOUR_ACCESS_TOKEN_SECRET] with Access Token Secret (oauth_token_secret) at number line 6, 7, 8 and 9.

my $YOUR_CONSUMER_KEY = “[YOUR_CONSUMER_KEY]” ;
my $YOUR_CONSUMER_SECRET = “[YOUR_CONSUMER_SECRET]” ;
my $YOUR_ACCESS_TOKEN = “[YOUR_ACCESS_TOKEN]“;
my $YOUR_ACCESS_TOKEN_SECRET = “[YOUR_ACCESS_TOKEN_SECRET]” ;

b) Replace [RSS FEED URL] with RSS Feed URl from where you cant to fetch news or data at line number 22.

my $xml = get(“[RSS FEED URL]“);

c) Replace [USERNAME] with your bit.ly username and [API KEY] with API Key provided by bit.ly. (Line Number: 28)

my $bitly = WWW::Shorten::Bitly->new(URL => $url, USER => “[USERNAME]“, APIKEY => “[API KEY]“);

NOTE: You should have account with bit.ly to get API Key.

Save this script with extension “.plx” and try to execute it using command “perl filename.plx“. Now check if your Twitter account is updated or not; if it is bingo else you have to check the keys.

Now you can schedule a job for the script as per your requirement.

About: Mike

Milind Koyande loves to work on new technologies specially virtualization and troubleshoot server problems. I’m an avid photographer and love to spend my free time close to nature, trying to capture its glory on my camera.