Skip to content

Instantly share code, notes, and snippets.

@dreid
Created January 29, 2011 14:22
Show Gist options
  • Save dreid/801863 to your computer and use it in GitHub Desktop.
Save dreid/801863 to your computer and use it in GitHub Desktop.
<li>
<a href="{{link}}">
{{title}} &mdash; {{author_name}}
</a>
</li>
---
title: "Reading List"
date: {{now}}
categories: books, reading
---
### Read
<ul>
{{#read_books}}
{{> book}}
{{/read_books}}
</ul>
### Reading
<ul>
{{#reading_books}}
{{> book}}
{{/reading_books}}
</ul>
<a href="http://www.goodreads.com/user/show/{{uid}}">Follow me at goodreads!</a>
#!/usr/bin/env python
import os, sys, re, rfc822
import urllib2
import pystache
import datetime
from xml.etree import ElementTree as ET
GOOD_READS_BASE="http://www.goodreads.com/review/list_rss"
GOOD_READS_ID="YOUR_GOOD_READS_USER_ID"
GOOD_READS_KEY="YOUR_GOOD_READS_KEY"
AMAZON_ID="AMAZON_ASSOCIATES_ID"
def shelf_rss_url(shelf, sort='date_read', order='a'):
return ("{0}/{1}?key={2}&shelf={3}&sort={4}&order={6}&source=flash"
"&amazon={5}&dest_site=amazon").format(
GOOD_READS_BASE,
GOOD_READS_ID,
GOOD_READS_KEY,
shelf,
sort,
AMAZON_ID,
order,
)
def make_item_dict(item):
d = {}
for elem in item:
value = None
if elem.text:
value = elem.text.strip()
d[elem.tag] = value
return d
FIX_CDATA_REGEX = re.compile(
r"<(guid|link)\b[^>]*>(?!<\!\[CDATA\[)(.*?)</\1>",
re.MULTILINE|re.DOTALL)
def fix_cdata(m):
return '<{0}><![CDATA[{1}]]></{0}>'.format(*m.groups())
def parse_shelf(shelf, sort, order):
url = shelf_rss_url(shelf, sort, order)
handle = urllib2.urlopen(url)
items = []
data = FIX_CDATA_REGEX.sub(fix_cdata, handle.read())
for item in ET.fromstring(data).findall('channel/item'):
items.append(make_item_dict(item))
return items
pystache.View.template_path = os.path.abspath(os.path.dirname(sys.argv[0]))
class ReadingList(pystache.View):
uid = GOOD_READS_ID
def now(self):
return datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S')
def read_books(self):
this_month = datetime.date.today().month
items = []
for item in parse_shelf('read', 'date_read', 'a'):
if datetime.date(*rfc822.parsedate_tz(
item['user_read_at'])[:3]).month == this_month:
items.append(item)
return items
def reading_books(self):
return parse_shelf('currently-reading', 'date_added', 'd')
def main():
print ReadingList().render()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment