I came up with this half-baked solution for reading my friends' twitter updates in Google Reader. The problem is that Google Reader does not support authenticated RSS feeds, which is required by twitter RSS feeds because your friends may not have made their updates available to the public. The solution I use is to proxy my twitter RSS feed through my own web server, transforming the authenticated feed into an unauthenticated one only accessible by Google Reader (kind of - it just checks the HTTP_USER_AGENT). Here's the python script I use.
from mod_python import apache
import urllib2
def auth_request(url, realm, user, password):
auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password(realm=realm,
uri=url,
user=user,
passwd=password)
opener = urllib2.build_opener(auth_handler)
return opener.open(url)
def twitter_rss(req):
req.add_common_vars()
if (req.subprocess_env.has_key("HTTP_USER_AGENT") and
req.subprocess_env["HTTP_USER_AGENT"].startswith("Feedfetcher-Google")):
req.content_type = "text/xml"
request = auth_request("http://twitter.com/statuses/friends_timeline/$YOUR_RSS_FEED_NUMBER.rss",
"Twitter API",
"$YOUR_TWITTER_LOGIN",
"$YOUR_TWITTER_PASSWORD")
req.write(request.read())
return apache.OK
req.content_type = "text/plain"
req.write(req.get_remote_host())
return apache.OK
You'll have to substitute $YOUR_RSS_FEED_NUMBER (search for the "RSS feed" link on your twitter home page), $YOUR_TWITTER_LOGIN, and $YOUR_TWITTER_PASSWORD in the script above. This uses apache2 and mod_python - I had to set up a directory on my web server with a mod_python handler by putting the following in my apache2 site config.
<Directory /var/www/matt/cgipy>
SetHandler mod_python
PythonHandler mod_python.publisher
# uncomment the following to figure out why it's not working
# PythonDebug On
</Directory>
Copy this python script into your mod_python-handled directory (also be sure to chmod +x), add the url to the python script as a Google Reader subscription, and you should be all set!
One downside is that Google Reader will only refresh the feed every 3 hours because there's only one subscriber - they seem to update more frequently for more popular RSS feeds. Also, you may be thinking, "Anyone can access this page by changing their HTTP_USER_AGENT!" You'd be right - in theory this opens up your friends' protected twitter updates for the world to see. So it's not a perfect solution, but I like that it lets me keep up with twitter from both desktop and mobile Google Reader, without having to visit yet another site or use yet another program.
 
No comments:
Post a Comment