Skip to Content

Monitoring Hashtags with Perl and Net::Twitter Syndicate content

Dave Sherohman's picture

I've taken an interest in writing some customized interface code to give me better control over my twitter feed and this program is my first move in that direction. Its basic function is to watch for tweets from the people you follow which contain tags from a list of tags that interest you.

#!/usr/bin/perl

use strict;
use warnings;

use Net::Twitter;

my $user = 'username';
my $pass = 'password';

my $prefix = '!#';
my @terms = qw( perl ironman );
my $check_interval = 600; # seconds

my $tag_regex = "[$prefix](" . (join '|', @terms) . ')\b';

my $twit = Net::Twitter->new( { username => $user, password => $pass } );
my $last_seen = 1;

while ($twit) {
  $last_seen = print_new_hits($twit, $last_seen);
  sleep $check_interval;
}

exit;


sub print_new_hits {
  my ($twit, $last_seen) = @_;

  my $timeline = $twit->friends_timeline( { count => 200, since_id => $last_seen } );
  return $last_seen unless $$timeline[0];

  $last_seen = $$timeline[0]->{id};
  my @hits = grep { $_->{text} =~ $tag_regex } @$timeline;

  for my $tweet (reverse @hits) {
    my $screen_name     = $tweet->{user}->{screen_name};
    my $text            = $tweet->{text};
    my $time            = $tweet->{created_at};
    print "$screen_name ($time)\n$text\n\n";
  }

  return $last_seen;
}

To use this code, you will, naturally, need to set $user and $pass to your twitter username and password.

$prefix controls which types of tags will be watched for. In addition to #hashtags, I also have it set to look for !bangtags, as I see a few people using !perl. If you use StockTwits, you may also want to add $ to $prefix. (Side question: What site/application are the !bangtags connected to?)

@terms gives the list of tags (of any of the types given in $prefix) which should be displayed. As configured here, it will show tweets containing one or more of #perl, !perl, #ironman, or !ironman.

The twitter API will allow you to call it up to 100 times per hour, or an average of once every 36 seconds. This code only checks once per 10 minutes in order to avoid issues if you are also running a standard twitter client. If you want quicker notification (or are following so many people that they produce more than 200 tweets per 10 minutes), you can change the $check_interval to suit your taste.

Update: I've expanded on this code a bit more and released it on CPAN as Twitter::TagGrep.

Update (June 16, 2009): Changed the parameter names in call to Net::Twitter->new from user/pass to username/password for compatibility with Net::Twitter 3.01. This code should now work with both version 2.12 and 3.01.