Monday, December 20, 2010

An aside: Twitter and RSS

The Department of Engineering twitter news feed has been dead since the autumn, when Twitter ended simple authentication in favour of OAuth. I've just resurrected it (the sort of job I get round to doing in the week before Christmas), and here's how (for the OAuth part I found this blog post very helpful - there are various broken and/or poorly-documented attempts to link Python, OAuth and Twitter around the web).
  1. Sign in to Twitter, and go to http://dev.twitter.com/ - then "Your apps" (top of the page) and register a new application.
  2. Assuming you've registered correctly, the next page will include a consumer key (a long alphanumeric string) and a consumer secret (an even longer alphanumeric string). Put these in a text file called consumer_keys.txt.
  3. Go to "My Access Token" (right hand side of the page) to get your access token. Two long strings again, put these in a text file called access_token.txt.
  4. Go to http://bit.ly, sign in, and then http://bit.ly/a/your_api_key
    - put your bit.ly username and this API key in a text file called bitly.txt
  5. Install tweepy and feedparser. (Some useful info on feedparser here).
  6. Code is below (note that I have shortened the RSS URL to prevent it running off the screen on this blog entry - in the actual program it needs to be the full RSS URL). I now just need to add a cron job to my PC, and it will check the website's news RSS feed and update Twitter periodically. You could, of course, simply put the various API and OAuth keys directly into the code, instead of having them in their own files.

#!/usr/bin/python

import tweepy
import feedparser
import urllib
import urllib2

rss_url = "http://www2.le.ac.uk/.../RSS"

oauth_file = open("access_token.txt", "r")
oauth_token = oauth_file.readline().rstrip()
oauth_token_secret = oauth_file.readline().rstrip()

consumer_file = open("consumer_keys.txt", "r")
consumer_key = consumer_file.readline().rstrip()
consumer_secret = consumer_file.readline().rstrip()

bitly_file = open("bitly.txt", "r")
bitly_username = bitly_file.readline().rstrip()
bitly_apikey = bitly_file.readline().rstrip()

bitly_base = "http://api.bit.ly/v3/shorten?"
bitly_data = {
"login" : bitly_username,
"apiKey" : bitly_apikey,
"format" : "txt",
"longUrl" : ""
}

already_done = []
done_file = open("done.txt", "r")
for line in done_file:
already_done.append(line.rstrip())
done_file.close()

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(oauth_token, oauth_token_secret)

api = tweepy.API(auth)
feed = feedparser.parse(rss_url)

for item in feed["items"]:
url = item["link"]
title = item["title"]
if url not in already_done:
bitly_data["longUrl"] = url
to_shorten = bitly_base + urllib.urlencode(bitly_data)
result = urllib2.urlopen(to_shorten).read()
api.update_status(title + " : " + result)
already_done.append(url)

done_file = open("done.txt", "w")
for url in already_done:
done_file.write(url + "\n")
done_file.close()


No comments:

Post a Comment