Ruby: Twitter, RSS, and XML

As a fun (and really quite odd) side project, I decided to write a bot program to simulate one of my friends on Twitter. Things I figured it should be able to do:
1. Send an update (obviously). I chose to just create a textfile with quotes and updates that my friend has said or may say for a list of possible updates.
2. Find a political news story and post it, shortening the URL if needed.

The first obvious hurdle was using the Twitter API. Thankfully, they actually keep a list of API libraries in various languages. I chose the Twitter package for Ruby (as oppsed to Twitter4R and Twittery for reasons I can’t quite recall, although it’s likely that they weren’t super strong reasons. Just had to pick one. ).

Installation of it was as simple as anything in Ruby should be: gem install twitter (you’ll need to answer y several times to dependencies).
They have the ruby doc’s generated, but they’re a bit lacking in information. I had to play around in fxri a bit examing methods and how things are handled to get a grasp on everything.

First, initializing the bot is pretty simple:
require 'twitter'
bot = Twitter::Base.new(username, password)

where username and password are those of your Twitter account. This will automatically create the connection between Twitter.
And to post, I just have to
status = bot.post(message)

Simple enough. First requirement fulfilled.

The second bit was a bit more involved. Firstly, I gathered up some political RSS feed links, as he’s a politcal kid.
require 'rss'
rssFeeds = ["http://feeds.digg.com/digg/topic/politics/popular.rss","http://www.msnbc.msn.com/id/3032552/device/rss/rss.xml"]

Then to pick a feed and a story at random (info found from here):
rss_source = @rssFeeds[rand(@rssFeeds.length)]
rss = nil
begin
rss = RSS::Parser.parse(rss_source)
rescue RSS::InvalidRSSError
rss = RSS::Parser.parse(rss_source, false)
end
story =rss.channel.items[rand(rss.channel.items.length)]
link = story.link
title =story.title

A little more involved, but not too hard. However, the link may be a bit too long for Twitter’s 140 character limit (extra is truncated unless full status is shown), especially if I post the title, so there may be need to shorten it. For this, I chose is.gd and bit.ly.
Is good is nice because it doesn’t require registration, and there’s no parsing to get the link out of the response.
require 'net/http'
require 'uri'

def getShortURL_isgd(longurl)
isgdpage = "http://is.gd"
if(longurl.class != String)
return nil;
end
url = URI.parse(isgdpage)
res = Net::HTTP.start(url.host, url.port) { |http|
http.get('/api.php?longurl=' + longurl) }
if(res.class == Net::HTTPOK)
return res.body
end
return nil
end

and all is well. And good.
My other choice was bit.ly, which was slightly more involved simply because it returns xml with more info than I really needed for this project.
bit.ly requires registering with them for free to use their API, but it can store a history of your links and track clicks, which is nice.
I chose to go with xml-simple for the XML parsing, which is built upon the rexml included with ruby (if I understood correctly). Once again, no super strong reason, just personal preference in how it looked when used in tutorials)

gem install xml-simple

require 'xmlsimple'
require 'net/http'
require 'uri'

def getShortURL_bitly(longurl)
bitlypage = "http://api.bit.ly"
if(longurl.class != String)
return nil;
end
url = URI.parse(bitlypage)
res = Net::HTTP.start(url.host, url.port) { |http|
http.get('/shorten?version=2.0.1&format=xml&login=username&apiKey=apikey&history=1&longUrl=' + longurl) }
if(res.class == Net::HTTPOK)
bitly = XmlSimple.xml_in(res.body, { 'KeyAttr' => 'name' })
if(bitly["errorCode"][0] != "0")
return nil
end
return bitly["results"][0]["nodeKeyVal"][0]["shortUrl"][0]
end
return nil
end

After that, it’s just combing the parts into my bot class, then writing the controlling program to create some Threads to post random updates and stories and random intervals.

If I get time, I’ll refine my bot code to be a bit more generic and post it.

Post project reflection:

I had trouble with HTTParty (used by the Twitter library for searches) on my Windows Web Server 2008 computer but not on my Vista computer. I was going to use the search to add new possible statuses by monitoring Twitter.

Twitter4R looks like it may be better for the REST API (status updates and you would do when logged on), and then using RSS parsing for the searches.

Advertisement
Explore posts in the same categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.