Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#!/usr/bin/env python
"""Grab the books from the current year from goodreads and emit some HTML.
"""
import requests
from xml.dom import minidom
import datetime, time
from email.utils import parsedate
import sys
goodreads_key = sys.argv[1]
# URL =
url = "https://www.goodreads.com/review/list?key={}&v=2&format=xml&id=1519786&per_page=200&shelf=read".format(goodreads_key)
timeformat = "%a %b %d %H:%M:%S %X %Y"
req = requests.get(url)
if req.status_code != 200:
print('Bad request!')
sys.exit(1)
xmldoc = minidom.parseString(req.content)
year = datetime.datetime.now().year - 1
reviews = xmldoc.firstChild.childNodes[3]
for review in reviews.getElementsByTagName('review'):
if review.getElementsByTagName('read_at')[0].firstChild is None:
continue
read_at = review.getElementsByTagName('read_at')[0].firstChild.data
d = datetime.datetime.fromtimestamp(time.mktime(parsedate(read_at)))
if d.year == year:
title = review.getElementsByTagName('title')[0].childNodes[0].data.encode('UTF-8')
isbn_13 = review.getElementsByTagName('isbn13')[0].firstChild
authors = review.getElementsByTagName('authors')
rating = review.getElementsByTagName('rating')[0].firstChild.data.encode('UTF-8')
if len(authors) == 1:
author = authors[0].getElementsByTagName('name')[0].firstChild.data.encode('UTF-8')
else:
author = 'Multiple Authors'
if isbn_13:
print '<li><a href="http://www.amazon.com/gp/search?keywords=%s&amp;index=books&amp;linkCode=qs&amp;tag=slackorama-20">%s</a> by %s (%s stars)</li>' % (isbn_13.data.encode('UTF-8'),title, author, rating)
else:
print '<li>%s</li>' % title
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment