From 416796678ba9fa145a72f643d15f63fc207ef595 Mon Sep 17 00:00:00 2001 From: geritol Date: Sat, 30 Sep 2017 13:15:49 +0200 Subject: [PATCH] small fixes - fixed a bug where table header was rendered for titles with no book children - now correctly parsing url (some of the record whereat parsed properly) --- utils/gooodreads.py | 34 ++++++++++++++++++++++------------ utils/read_file.py | 6 +++--- utils/write_file.py | 5 +++-- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/utils/gooodreads.py b/utils/gooodreads.py index 0b6c3f9..77c5dd4 100644 --- a/utils/gooodreads.py +++ b/utils/gooodreads.py @@ -8,9 +8,9 @@ from config import GOODREADS_PUBLIC_API_KEY def get_details(book_object): - url = "http://www.goodreads.com/book/title.xml?key={}&title={}".format(GOODREADS_PUBLIC_API_KEY, urllib.parse.quote_plus(book_object['title'])) - # url = url.replace(' ', '%20') - print(url) + url = "http://www.goodreads.com/book/title.xml?key={}&title={}".format(GOODREADS_PUBLIC_API_KEY, + urllib.parse.quote_plus(book_object['title'])) + try: tree = ET.ElementTree(file=urllib.request.urlopen(url)) root = tree.getroot() @@ -20,25 +20,35 @@ def get_details(book_object): book_object['rating'] = 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 GoodReads: ') + print('Error getting book details from GoodReads for book: {}. \nGot error: '.format(book_object['title'])) print(str(e.getcode()) + ' ' + e.msg) - print(book_object) def get_goodread_info(library): - i = 0 + import sys + return + print('') + print('Getting GoodReads data...') + + processed = 0 + total_book_count = 0 + for key in library: + total_book_count += len(library[key]) + for chapter in library: book_list = library[chapter] for book in book_list: - - # do not call the api again if we already have the infomration + # do not call the api again if we already have the infomation if 'rating' in book and book['rating']: + processed += 1 continue - #print(i) - #if i == 10: - #break get_details(book) - #i += 1 + processed += 1 + + print('{}/{} records processed.'.format(processed, total_book_count), end="\b") + sys.stdout.write('\r') + sys.stdout.flush() # <- makes python print it anyway + # need to wait a second between the requests, to not abuse the API time.sleep(1) \ No newline at end of file diff --git a/utils/read_file.py b/utils/read_file.py index 0509c10..42744d6 100644 --- a/utils/read_file.py +++ b/utils/read_file.py @@ -9,7 +9,7 @@ def read_file_content(file): def parse_book_string(book_string): book = {} book['title'] = book_string.split('[')[1].split(']')[0] - book['url'] = book_string.split('(')[1].split(')')[0] + book['url'] = book_string.split(']')[1].split('(')[1].split(')')[0] book['author'] = book_string.split(' by ')[-1] book['rating'] = '' book['year'] = '' @@ -20,7 +20,7 @@ def parse_book_string(book_string): def parse_book_string_new(book_string): book = {} book_split = book_string.split('|') - print(book_split) + # print(book_split) book['title'] = book_split[1].strip() book['author'] = book_split[2].strip() book['url'] = book_split[3].strip().split('[')[1].split('(')[1].split(')')[0] @@ -31,7 +31,7 @@ def parse_book_string_new(book_string): def load(file, file_type): file = read_file_content(file) - print(file) + # we start one line after tilte # Books line_to_start = file.index('# Books') + 1 current_title = '' diff --git a/utils/write_file.py b/utils/write_file.py index c7667cd..86c17fb 100644 --- a/utils/write_file.py +++ b/utils/write_file.py @@ -23,8 +23,9 @@ def render(in_file, out_file, library): # render chapter and start of the table out_file.write(line) - out_file.write('| Name | Author | Goodreads Rating | Year Published | \n') - out_file.write('|------|--------|------------------|----------------| \n') + if len(library[line.strip()]) > 0: + out_file.write('| Name | Author | Goodreads Rating | Year Published | \n') + out_file.write('|------|--------|------------------|----------------| \n') # render books for book in library[line.strip()]: out_file.write(render_book_line(book))