Feed Twitter an RSS feed with Perl.

Here’s a more concise script to feed Twitter an RSS feed; in this case the latest post to the Sonic.net MOTD Blog. The following code grabs the latest entry in an RSS feed, pulls out the URL and as much content as it can, shortens the URL and sends it all to Twitter.

#!/usr/bin/perl

use strict;
use warnings;
use Net::Twitter;
use LWP::Simple;
use XML::RSS::Parser::Lite;
use WWW::Shorten::Metamark;

my $feed = get(‘http://corp.sonic.net/motd/feed/rss’);
my $rp = new XML::RSS::Parser::Lite;

$rp->parse($feed);

my $it = $rp->get(0);

my $long_url = $it->get(‘url’);
my $short_url = makeashorterlink($long_url);
my $message = $it->get(‘description’);

$message =~ s/^.+ — //; # remove dash seperator.
$message =~ s/^\s+//; # Nuke any leading space; every char counts.
$message =~ s/\s+$//; # Nuke any trailing space; every char counts.
$message =~ s/[_]+$//; # Nuke any trailing underscores; happens when message is
short.
$message = substr $message , 0 , 117; # Twitter can only handle 140 chars.
$message .= ‘… ‘; # Append ellipse to denote further content available.
$message .= $short_url; # Append the shortened URL.

my $twit = Net::Twitter->new(username=>”sonicnet”, password=>”password” );
$twit->update($message);

This entry was posted in General, work and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>