Skip to content

Commit

Permalink
Bug fix for #901
Browse files Browse the repository at this point in the history
  • Loading branch information
philborman committed Jul 7, 2017
1 parent 33c6e72 commit 06917b0
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 31 deletions.
25 changes: 20 additions & 5 deletions lazylibrarian/bookwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,11 +527,20 @@ def getSeriesAuthors(seriesid):
else:
resultxml = rootxml.getiterator('work')
for item in resultxml:
booktitle = item.find('./best_book/title').text
try:
booktitle = item.find('./best_book/title').text
except (KeyError, AttributeError):
booktitle = ""
book_fuzz = fuzz.token_set_ratio(booktitle, bookname)
if book_fuzz >= 98:
author = item.find('./best_book/author/name').text
authorid = item.find('./best_book/author/id').text
try:
author = item.find('./best_book/author/name').text
except (KeyError, AttributeError):
author = ""
try:
authorid = item.find('./best_book/author/id').text
except (KeyError, AttributeError):
authorid = ""
logger.debug("Author Search found %s %s, authorid %s" % (author, booktitle, authorid))
break
if not authorid: # try again with title only
Expand All @@ -547,8 +556,14 @@ def getSeriesAuthors(seriesid):
booktitle = item.find('./best_book/title').text
book_fuzz = fuzz.token_set_ratio(booktitle, bookname)
if book_fuzz >= 98:
author = item.find('./best_book/author/name').text
authorid = item.find('./best_book/author/id').text
try:
author = item.find('./best_book/author/name').text
except (KeyError, AttributeError):
author = ""
try:
authorid = item.find('./best_book/author/id').text
except (KeyError, AttributeError):
authorid = ""
logger.debug("Title Search found %s %s, authorid %s" % (author, booktitle, authorid))
break
if not authorid:
Expand Down
49 changes: 35 additions & 14 deletions lazylibrarian/gr.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,21 @@ def find_results(self, searchterm=None, queue=None):
loopCount = 1
while resultxml:
for author in resultxml:

if author.find('original_publication_year').text is None:
try:
if author.find('original_publication_year').text is None:
bookdate = "0000"
else:
bookdate = author.find('original_publication_year').text
except (KeyError, AttributeError):
bookdate = "0000"
else:
bookdate = author.find('original_publication_year').text

authorNameResult = author.find('./best_book/author/name').text
# Goodreads sometimes puts extra whitepase in the author names!
authorNameResult = ' '.join(authorNameResult.split())
try:
authorNameResult = author.find('./best_book/author/name').text
# Goodreads sometimes puts extra whitepase in the author names!
authorNameResult = ' '.join(authorNameResult.split())
except (KeyError, AttributeError):
authorNameResult = ""

booksub = ""
bookpub = ""
booklang = "Unknown"
Expand All @@ -104,12 +110,24 @@ def find_results(self, searchterm=None, queue=None):
bookgenre = ''
bookdesc = ''
bookisbn = ''
booklink = 'http://www.goodreads.com/book/show/' + author.find('./best_book/id').text

if author.find('./best_book/title').text is None:
try:
booklink = 'http://www.goodreads.com/book/show/' + author.find('./best_book/id').text
except (KeyError, AttributeError):
booklink = ""

try:
authorid = author.find('./best_book/author/id').text
except (KeyError, AttributeError):
authorid = ""

try:
if author.find('./best_book/title').text is None:
bookTitle = ""
else:
bookTitle = author.find('./best_book/title').text
except (KeyError, AttributeError):
bookTitle = ""
else:
bookTitle = author.find('./best_book/title').text

if searchauthorname:
author_fuzz = fuzz.ratio(authorNameResult, searchauthorname)
Expand All @@ -132,12 +150,15 @@ def find_results(self, searchterm=None, queue=None):

highest_fuzz = max((author_fuzz + book_fuzz) / 2, isbn_fuzz)

bookid = author.find('./best_book/id').text
try:
bookid = author.find('./best_book/id').text
except (KeyError, AttributeError):
bookid = ""

resultlist.append({
'authorname': author.find('./best_book/author/name').text,
'authorname': authorNameResult,
'bookid': bookid,
'authorid': author.find('./best_book/author/id').text,
'authorid': authorid,
'bookname': bookTitle.encode("ascii", "ignore"),
'booksub': booksub,
'bookisbn': bookisbn,
Expand Down
31 changes: 19 additions & 12 deletions lazylibrarian/librarysync.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,22 +666,29 @@ def LibraryScan(startdir=None, library='eBook', authid=None):
else:
resultxml = rootxml.getiterator('work')
for item in resultxml:
booktitle = item.find('./best_book/title').text
try:
booktitle = item.find('./best_book/title').text
except (KeyError, AttributeError):
booktitle = ""
book_fuzz = fuzz.token_set_ratio(booktitle, book)
if book_fuzz >= 98:
logger.debug("Rescan found %s : %s" % (booktitle, language))
rescan_hits += 1
bookid = item.find('./best_book/id').text
GR_ID = GoodReads(bookid)
GR_ID.find_book(bookid, None)
if language and language != "Unknown":
# set language from book metadata
logger.debug("Setting language from metadata %s : %s" % (
booktitle, language))
myDB.action(
'UPDATE books SET BookLang=? WHERE BookID=?',
(language, bookid))
break
try:
bookid = item.find('./best_book/id').text
except (KeyError, AttributeError):
bookid = ""
if bookid:
GR_ID = GoodReads(bookid)
GR_ID.find_book(bookid, None)
if language and language != "Unknown":
# set language from book metadata
logger.debug("Setting language from metadata %s : %s" % (
booktitle, language))
myDB.action(
'UPDATE books SET BookLang=? WHERE BookID=?',
(language, bookid))
break
if not bookid:
logger.warn("GoodReads doesn't know about %s" % book)
except Exception:
Expand Down

0 comments on commit 06917b0

Please sign in to comment.