Created
January 1, 2016 16:29
-
-
Save slackorama/d879bafad762d1c2b2c3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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&index=books&linkCode=qs&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