From 553d68f2deb0660532deb677d581fd6139dc900e Mon Sep 17 00:00:00 2001 From: geritol Date: Fri, 29 Sep 2017 08:15:45 +0200 Subject: [PATCH] initial version of the working integration with Goodreads API --- utils/.gitignore | 4 +++- utils/config-sample.py | 2 ++ utils/housekeep.py | 27 +++++++++++++++++++++++++-- 3 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 utils/config-sample.py diff --git a/utils/.gitignore b/utils/.gitignore index 723ef36..c13d43b 100644 --- a/utils/.gitignore +++ b/utils/.gitignore @@ -1 +1,3 @@ -.idea \ No newline at end of file +.idea +__pycache__ +config.py \ No newline at end of file diff --git a/utils/config-sample.py b/utils/config-sample.py new file mode 100644 index 0000000..fb6d300 --- /dev/null +++ b/utils/config-sample.py @@ -0,0 +1,2 @@ +# save this file as 'config.py' and then, fill it with you api key +GOODREADS_PUBLIC_API_KEY = 'write here your goodreads public API key' \ No newline at end of file diff --git a/utils/housekeep.py b/utils/housekeep.py index da961f8..e7206f6 100644 --- a/utils/housekeep.py +++ b/utils/housekeep.py @@ -1,3 +1,5 @@ +from config import GOODREADS_PUBLIC_API_KEY + file_with_books = './../README.MD' # we assume that every line after # Books starting with * is a book title @@ -80,7 +82,25 @@ def render(file_name, library): out_file.write(line) books_not_reached = True - +def get_details(book_object): + import xml.etree.ElementTree as ET + import urllib.request + import urllib.error + url = "http://www.goodreads.com/book/title.xml?key={}&title={}".format(GOODREADS_PUBLIC_API_KEY, book_object['title']) + url = url.replace(' ', '%20') + print(url) + try: + tree = ET.ElementTree(file=urllib.request.urlopen(url)) + root = tree.getroot() + book = root.find('book') + book_object['year'] = book.find('publication_year').text + book_object['lang'] = book.find('language_code').text + book_object['avg_rt'] = book.find('average_rating').text + book_object['pages'] = book.find('num_pages').text + except urllib.error.HTTPError as e: + print('Error getting book details from goodread: ') + print(str(e.getcode()) + ' ' + e.msg) + print(book_object) library = load(file_with_books) library = sort_by(library, 'title') @@ -90,4 +110,7 @@ render('./../by-title.md', library) library = load(file_with_books) library = sort_by(library, 'author') -render('./../by-author.md', library) \ No newline at end of file +render('./../by-author.md', library) + +print(library) +get_details(library['### Miscellaneous'][0]) \ No newline at end of file