Friday, November 29, 2013

Twitter app using perl – Part 1


Hi All,

I thought it would be nice quest to create an aap with Step by Step guide.
Here in this series , I will gonna create an app for twitter using Cpan module “Net::Twitter“.


Step 1 .
You need to create an account on dev.twitter.com and need to register your new app.

Step 2.
If you have already finished up with step one , by now you must be having following details for new registered app
Consumer key
Consumer secret
Access token
Access token secret

Do not share your oauth_token_secret with anyone.As it is used to access your twitter account

Step 3.
Install the cpan module ”Net::Twitter

Now you are ready for a simple app

Here in this app I will try to list out all my friend list  and further in the series we will upgrade the app for adding other functionality.

#!/usr/bin/perl
use Data::Dumper;
use Net::Twitter;
##fill in your dev.twitter stuff below
my $twitterconsumer = "ur_consumer";
my $twitterconsumersecret = "ur_consumer_secret";
my $twitteraccesstoken = "ur_access_token";
my $twitteraccesstokensecret = "ur_access_token_secret";
my $nt = Net::Twitter->new(
traits => [qw/API::RESTv1_1/],
consumer_key => $twitterconsumer,
consumer_secret => $twitterconsumersecret,
access_token => $twitteraccesstoken,
access_token_secret => $twitteraccesstokensecret,
);
if (1) {
my $r = $nt->friends;
my @friends = @{$r->{users}};
my %temp = %$r;
# the data stucture is quite complex, I would suggest u
# to print that in file and a to know the various field
print Dumper(\%temp);
foreach my $new_hash (@friends) {
print $new_hash->{'name'},"\n";
}
}
view raw TwitterApp.pl hosted with ❤ by GitHub



Here U can see I have assign the variable $r to hash %temp at line 29 and dumped  the output using dumper.
As the return data is quite complex , it will most easiest to see the structure of the return data using Dumper.
Here I am printing the list of my friend name , you can further explore other keys of the hash as per your choice.
Hope you enjoyed the tutorial.

original source : link

No comments:

Post a Comment