[tpm] Working with Net::Twitter

Scott Elcomb psema4 at gmail.com
Mon Mar 23 18:06:34 PDT 2009


Hi all,

I'm wondering if anyone on the list has worked with Net::Twitter.  I
finally broke down and grabbed an Twitter account today but haven't
found a client I like just yet.  A quick look on CPAN turned up
Net::Twitter which seems to work well enough for my immediate needs.
I'm having a bit of an issue getting the username associated with
friends' tweets though.

Here's the code I'm using (whipped together in about 5 minutes and
borrowing heavily from the module's synopsis):

## Code Starts ##

#!/usr/bin/perl
use strict;
use Net::Twitter;

my $twitter = Net::Twitter->new({username=>"psema4", password=>"****" });
my $cmd = $ARGV[0] || die("NO CMD!\n");

if ($cmd eq 'update') {
  update();
} elsif ($cmd eq 'read') {
  readTweets();
} else {
  die("BAD CMD!\n");
}

print "\nDONE.\n";

sub update {
  my $msg = $ARGV[1] || die("NO MSG!\n");
  my $result = $twitter->update({status => $msg});
}

sub readTweets {
  my $result = $twitter->friends_timeline();

  foreach my $tweet (@{ $result }) {
    my $speaker =  $tweet->{from_user};
    my $text = $tweet->{text};
    my $time = $tweet->{created_at};

    print "$time <$speaker> $text\n";
  }
}

## Code Ends ##

If I run
  selcomb at host:~/devel/perl/twitter$ twit read

then I get output similar to:

Tue Mar 24 00:59:01 +0000 2009 <> Just added myself to the
http://wefollow.com twitter directory under:  #copyright #law #privacy
Tue Mar 24 00:39:32 +0000 2009 <> [reading] Officers Have the Same
Fiduciary Duties as Directors http://tinyurl.com/cp9ql9
Wide character in print at ./twit line 32.
Tue Mar 24 00:36:42 +0000 2009 <> [News] Fwix’s Regional News Feeds
Come To The iPhone http://twurl.nl/72v5le
Tue Mar 24 00:35:13 +0000 2009 <> The eruption of Redoubt volcano
continues and the color code and volcano alert level remains at...
Read more at http://tinyurl.com/cel2e7
Tue Mar 24 00:31:52 +0000 2009 <> RT @Joi: Number of CC licensed
photos on Flickr just passed 100,000,000 (This thing might just catch
on!)
Tue Mar 24 00:23:26 +0000 2009 <> Playing with Net::Twitter
Tue Mar 24 00:15:21 +0000 2009 <> Lawsuit: Best Buy has "anti-price
matching policy" - http://ping.fm/NCNkU



The update() function works fine (entry "Playing with Net::Twitter")
and readTweets() seems to work except for the fact that $speaker
(which should be within the angle brackets in the output) does not get
set.  I found no references to a "from_user" hash key in the POD save
for the one line in the synopsis.  Could just be that I'm blind.

Anyway, if anyone has experience with the module I'd appreciate
pointers; if not I'll try contacting the module author later in the
week.  Thanks!

-- 
  Scott Elcomb
  http://www.psema4.com/


More information about the toronto-pm mailing list