Skip to content

Instantly share code, notes, and snippets.

@Ell
Created May 7, 2011 05:01
Show Gist options
  • Save Ell/960219 to your computer and use it in GitHub Desktop.
Save Ell/960219 to your computer and use it in GitHub Desktop.
goodreads reading
#same response
#http://www.goodreads.com/book/isbn?isbn=0441172717&key=1NzniiGBie03MWduCDH8g
#
#code currently returns index out of range
from mobi import Mobi
from HTMLParser import HTMLParser
import textwrap
import urllib, urllib2, cookielib
from lxml import etree
import lxml.html as poop
import mechanize
GOODREADS_API_KEY = '1NzniiGBie03MWduCDH8g'
class BookParse(object):
def __init__(self, apikey):
self.apikey = apikey
def getBookInfo(self, isbn):
if isbn == 'poop':
return None
else:
br = mechanize.Browser()
r = br.open("http://www.goodreads.com/book/isbn?isbn=%s&key=%s" % (isbn, self.apikey))
parse = poop.fromstring(r.read())
title = parse.xpath("//GoodreadsResponse/book/title/text()")[0]
author = parse.xpath("//GoodreadsResponse/book/authors/author/name/text()")[0]
synopsis = parse.xpath("//GoodreadsResponse/book/description/text()")[0]
rating = parse.xpath("//GoodreadsResponse/book/average_rating/text()")[0]
#TODO: add link to cover
return {"title": title, "author": author, "synopsis": synopsis, "rating": rating}
def getISBN(self, bookFile):
book = Mobi(bookFile)
book.parse()
records = book.config['exth']['records']
try:
isbn = records[104]
return isbn
except:
return "poop"
if __name__ == '__main__':
parsebook = BookParse(GOODREADS_API_KEY)
isbn = parsebook.getISBN("book.mobi")
bookparse = parsebook.getBookInfo(isbn)
print bookparse["title"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment