From 5148820fa39feef3eba49b1f727b8746aaf36fa2 Mon Sep 17 00:00:00 2001 From: Adam Kelly Date: Sun, 12 Apr 2020 13:54:10 +0100 Subject: [PATCH 1/2] Move attempt to resolve books to view action. --- fedireads/goodreads_import.py | 9 +-------- fedireads/view_actions.py | 5 +++++ 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/fedireads/goodreads_import.py b/fedireads/goodreads_import.py index 1333f9cc..367b29ee 100644 --- a/fedireads/goodreads_import.py +++ b/fedireads/goodreads_import.py @@ -2,7 +2,6 @@ import re import csv import itertools -from requests import HTTPError from fedireads import books_manager @@ -42,13 +41,7 @@ class GoodreadsCsv: def __iter__(self): for line in itertools.islice(self.reader, MAX_ENTRIES): - entry = GoodreadsItem(line) - try: - entry.resolve() - except HTTPError: - pass - yield entry - + yield GoodreadsItem(line) class GoodreadsItem: ''' a processed line in a goodreads csv ''' diff --git a/fedireads/view_actions.py b/fedireads/view_actions.py index 7ccf4888..c3134441 100644 --- a/fedireads/view_actions.py +++ b/fedireads/view_actions.py @@ -2,6 +2,7 @@ from io import BytesIO, TextIOWrapper import re from PIL import Image +from requests import HTTPError from django.contrib.auth import authenticate, login, logout from django.contrib.auth.decorators import login_required @@ -424,6 +425,10 @@ def import_data(request): for item in GoodreadsCsv(TextIOWrapper( request.FILES['csv_file'], encoding=request.encoding)): + try: + item.resolve() + except HttpError: + pass if item.book: results.append(item) if item.rating or item.review: From 6d7f20caad5ed32916361410b2461c761005204e Mon Sep 17 00:00:00 2001 From: Adam Kelly Date: Sun, 12 Apr 2020 13:54:59 +0100 Subject: [PATCH 2/2] Check if we already have edition in our database before using openlibrary. --- fedireads/goodreads_import.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/fedireads/goodreads_import.py b/fedireads/goodreads_import.py index 367b29ee..adcbad3c 100644 --- a/fedireads/goodreads_import.py +++ b/fedireads/goodreads_import.py @@ -4,6 +4,7 @@ import csv import itertools from fedireads import books_manager +from fedireads.models import Edition # Mapping goodreads -> fedireads shelf titles. @@ -49,22 +50,27 @@ class GoodreadsItem: self.line = line self.book = None - def resolve(self): ''' try various ways to lookup a book ''' - self.book = self.get_book_from_isbn() - if not self.book: - self.book = self.get_book_from_title_author() + self.book = ( + self.get_book_from_db_isbn() or + self.get_book_from_isbn() or + self.get_book_from_title_author() + ) + def get_book_from_db_isbn(self): + ''' see if we already know about the book ''' + try: + return Edition.objects.get(isbn=self.isbn) + except Edition.DoesNotExist: + return None def get_book_from_isbn(self): ''' search by isbn ''' - isbn = unquote_string(self.line['ISBN13']) - search_results = books_manager.search(isbn) + search_results = books_manager.search(self.isbn) if search_results: return books_manager.get_or_create_book(search_results[0].key) - def get_book_from_title_author(self): ''' search by title and author ''' search_term = construct_search_term( @@ -75,6 +81,10 @@ class GoodreadsItem: if search_results: return books_manager.get_or_create_book(search_results[0].key) + @property + def isbn(self): + return unquote_string(self.line['ISBN13']) + @property def shelf(self): ''' the goodreads shelf field '''