I’m currently working on a new design for my site and it is going to show recent stories I’ve saved in Readability. Readability, if you are not familiar, is at its core a site that allows you to save webpages for reading later. You can then read and archive them on the Readability site. They have apps on the various platforms, and may applications are adding the ability to save links directly to Readability so you can read them later. I’ve found this more useful than some of the other various readers/social bookmarking sites. In the new design I wanted to share what I have been reading lately.
In working with a few other APIs, they eventually add rate limits, or get slower to respond to requests as their user base grows. To avoid these issues I decided to cache calls to the Readability API instead of doing them live on the site, client side. It isn’t anything terribly complicated, but I’ll review the code for the script.
#!/usr/bin/python
import readability
from datetime import date, timedelta
from sqlalchemy import *
These are the required libraries for the script.
''' readability config '''
token = readability.xauth('read_username', 'read_apikey', 'read_email', 'read_pass')
rdd = readability.oauth('read_username', 'read_apikey', token=token)
These lines are the config for the oauth call to the Readability API. You’ll need to acquire your API Key from the Readability site and then substitute all the parts here.
''' sqlalchemy setup '''
engine = create_engine('mysql://db_username:db_passwd@db_host/db')
connection = engine.connect()
In searching and talking to some people it looked like SQLalchemy was going to be the best way to get Python to work with MySQL. This library made this script much easier than I expected.
''' Fetch new bookmarks since yesterday '''
yesterday = date.today() - timedelta(1)
for b in rdd.get_bookmarks(added_since=yesterday.strftime('%m-%d-%y')):
result = connection.execute("INSERT INTO bookmarks VALUES(NULL, " + str(b.id) + ", '" + b.article.title.encode("utf-8") + "', '" + b.article.url.encode("utf-8") + "')" )
For your initial pull of your bookmarks into your database you would just need to change the for statement to “rdd.get_bookmarks()”. The data that is saved in the database, as you can see, is the readability bookmark id, the title and the URL for the story. There is more available in the API, but this should be sufficient to display a ‘currently reading’ list in a widget on a webpage.
The end of this would then be to schedule this script in cron to run once a day. If you find yourself saving bookmarks more often, you may want to update these schedules.
The database structure and code are on Github