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)
This commit is contained in:
geritol
2017-09-30 13:15:49 +02:00
committed by Vishnu Ks
parent 7c18180613
commit 416796678b
3 changed files with 28 additions and 17 deletions

View File

@@ -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)

View File

@@ -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 = ''

View File

@@ -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))