Last active
August 29, 2015 14:28
-
-
Save ta1hia/051833e7fa11793ece8d to your computer and use it in GitHub Desktop.
get goodreads shelves
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
import xmltodict | |
import os | |
import json | |
import requests | |
def request_user_shelves(parms): | |
p = dict(parms.items() + {"key":os.environ.get("GOODREADS_ACCESS_KEY")}.items()) | |
resp = requests.get("https://www.goodreads.com/shelf/list.xml",p) | |
data_dict = xmltodict.parse(resp.content) | |
data_dict = data_dict['GoodreadsResponse']['shelves'] | |
shelf_count = {} | |
for shelf in data_dict['user_shelf']: | |
shelf_count[str(shelf['name'])] = int(shelf['book_count']['#text']) | |
return shelf_count | |
def request_shelf_books(parms): | |
p = dict(parms.items() + {"key":os.environ.get("GOODREADS_ACCESS_KEY"), "v":"2"}.items()) | |
resp = requests.get("https://www.goodreads.com/review/list.xml/", p) | |
data_dict = xmltodict.parse(resp.content) | |
data_dict = data_dict['GoodreadsResponse']['reviews']['review'] | |
books = [] | |
for book in data_dict: | |
books.append({"title": book['book']['title'].strip(" "), "author":book['book']['authors']['author']['name']}) | |
return books | |
def create_shelf_file(): | |
shelf_info = request_user_shelves({"user_id": "4933497-tahia"}) | |
shelves = [] | |
for name,total in shelf_info.iteritems(): | |
pg = 1 | |
left = total | |
books = [] | |
while left > 0: | |
books.extend(request_shelf_books({"shelf": name, "id": "4933497-tahia", "per_page":200, "page":pg})) | |
left -= 200 | |
pg+=1 | |
shelves.append({"name":name, "total":total, "books":books}) | |
with open('shelf.json', 'w') as outfile: | |
json.dump(shelves, outfile) | |
print json.dumps(create_shelf_file(), indent=4) |
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
xmltodict | |
requests |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment