Created
May 7, 2011 05:01
-
-
Save Ell/960219 to your computer and use it in GitHub Desktop.
goodreads reading
This file contains 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
#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