diff --git a/lazylibrarian/bookwork.py b/lazylibrarian/bookwork.py index ca6dab307..4a76ba2c1 100644 --- a/lazylibrarian/bookwork.py +++ b/lazylibrarian/bookwork.py @@ -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 @@ -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: diff --git a/lazylibrarian/gr.py b/lazylibrarian/gr.py index d4a9889ee..b5fa356eb 100644 --- a/lazylibrarian/gr.py +++ b/lazylibrarian/gr.py @@ -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" @@ -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) @@ -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, diff --git a/lazylibrarian/librarysync.py b/lazylibrarian/librarysync.py index a700fa637..57c40634c 100644 --- a/lazylibrarian/librarysync.py +++ b/lazylibrarian/librarysync.py @@ -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: