diff --git a/bookwyrm/connectors/abstract_connector.py b/bookwyrm/connectors/abstract_connector.py index 1dfc406c..20a17526 100644 --- a/bookwyrm/connectors/abstract_connector.py +++ b/bookwyrm/connectors/abstract_connector.py @@ -256,9 +256,7 @@ def get_data(url, params=None, timeout=10): params=params, headers={ # pylint: disable=line-too-long "Accept": ( - "application/activity+json," - ' application/ld+json; profile="https://www.w3.org/ns/activitystreams",' - " application/json; charset=utf-8" + 'application/json, application/activity+json, application/ld+json; profile="https://www.w3.org/ns/activitystreams"; charset=utf-8' ), "User-Agent": settings.USER_AGENT, }, @@ -266,7 +264,7 @@ def get_data(url, params=None, timeout=10): ) except RequestException as err: logger.exception(err) - raise ConnectorException() + raise ConnectorException(err) if not resp.ok: raise ConnectorException() @@ -274,7 +272,7 @@ def get_data(url, params=None, timeout=10): data = resp.json() except ValueError as err: logger.exception(err) - raise ConnectorException() + raise ConnectorException(err) return data diff --git a/bookwyrm/importers/__init__.py b/bookwyrm/importers/__init__.py index d4890070..dd3d62e8 100644 --- a/bookwyrm/importers/__init__.py +++ b/bookwyrm/importers/__init__.py @@ -3,4 +3,5 @@ from .importer import Importer from .goodreads_import import GoodreadsImporter from .librarything_import import LibrarythingImporter +from .openlibrary_import import OpenLibraryImporter from .storygraph_import import StorygraphImporter diff --git a/bookwyrm/importers/importer.py b/bookwyrm/importers/importer.py index 94e6734e..32800e77 100644 --- a/bookwyrm/importers/importer.py +++ b/bookwyrm/importers/importer.py @@ -26,7 +26,7 @@ class Importer: ("authors", ["author", "authors", "primary author"]), ("isbn_10", ["isbn10", "isbn"]), ("isbn_13", ["isbn13", "isbn", "isbns"]), - ("shelf", ["shelf", "exclusive shelf", "read status"]), + ("shelf", ["shelf", "exclusive shelf", "read status", "bookshelf"]), ("review_name", ["review name"]), ("review_body", ["my review", "review"]), ("rating", ["my rating", "rating", "star rating"]), @@ -36,9 +36,9 @@ class Importer: ] date_fields = ["date_added", "date_started", "date_finished"] shelf_mapping_guesses = { - "to-read": ["to-read"], - "read": ["read"], - "reading": ["currently-reading", "reading"], + "to-read": ["to-read", "want to read"], + "read": ["read", "already read"], + "reading": ["currently-reading", "reading", "currently reading"], } def create_job(self, user, csv_file, include_reviews, privacy): @@ -90,7 +90,10 @@ class Importer: def get_shelf(self, normalized_row): """determine which shelf to use""" - shelf_name = normalized_row["shelf"] + shelf_name = normalized_row.get("shelf") + if not shelf_name: + return None + shelf_name = shelf_name.lower() shelf = [ s for (s, gs) in self.shelf_mapping_guesses.items() if shelf_name in gs ] @@ -106,6 +109,7 @@ class Importer: user=user, include_reviews=original_job.include_reviews, privacy=original_job.privacy, + source=original_job.source, # TODO: allow users to adjust mappings mappings=original_job.mappings, retry=True, diff --git a/bookwyrm/importers/openlibrary_import.py b/bookwyrm/importers/openlibrary_import.py new file mode 100644 index 00000000..ef103060 --- /dev/null +++ b/bookwyrm/importers/openlibrary_import.py @@ -0,0 +1,13 @@ +""" handle reading a csv from openlibrary""" +from . import Importer + + +class OpenLibraryImporter(Importer): + """csv downloads from OpenLibrary""" + + service = "OpenLibrary" + + def __init__(self, *args, **kwargs): + self.row_mappings_guesses.append(("openlibrary_key", ["edition id"])) + self.row_mappings_guesses.append(("openlibrary_work_key", ["work id"])) + super().__init__(*args, **kwargs) diff --git a/bookwyrm/importers/storygraph_import.py b/bookwyrm/importers/storygraph_import.py index 9368115d..67cbaa66 100644 --- a/bookwyrm/importers/storygraph_import.py +++ b/bookwyrm/importers/storygraph_import.py @@ -3,6 +3,6 @@ from . import Importer class StorygraphImporter(Importer): - """csv downloads from librarything""" + """csv downloads from Storygraph""" service = "Storygraph" diff --git a/bookwyrm/models/import_job.py b/bookwyrm/models/import_job.py index c4679585..919bbf0d 100644 --- a/bookwyrm/models/import_job.py +++ b/bookwyrm/models/import_job.py @@ -25,7 +25,7 @@ def construct_search_term(title, author): # Strip brackets (usually series title from search term) title = re.sub(r"\s*\([^)]*\)\s*", "", title) # Open library doesn't like including author initials in search term. - author = re.sub(r"(\w\.)+\s*", "", author) + author = re.sub(r"(\w\.)+\s*", "", author) if author else "" return " ".join([title, author]) @@ -88,7 +88,9 @@ class ImportItem(models.Model): return if self.isbn: - self.book = self.get_book_from_isbn() + self.book = self.get_book_from_identifier() + elif self.openlibrary_key: + self.book = self.get_book_from_identifier(field="openlibrary_key") else: # don't fall back on title/author search if isbn is present. # you're too likely to mismatch @@ -98,10 +100,10 @@ class ImportItem(models.Model): else: self.book_guess = book - def get_book_from_isbn(self): - """search by isbn""" + def get_book_from_identifier(self, field="isbn"): + """search by isbn or other unique identifier""" search_result = connector_manager.first_search_result( - self.isbn, min_confidence=0.999 + getattr(self, field), min_confidence=0.999 ) if search_result: # it's already in the right format @@ -114,6 +116,8 @@ class ImportItem(models.Model): def get_book_from_title_author(self): """search by title and author""" + if not self.title: + return None, 0 search_term = construct_search_term(self.title, self.author) search_result = connector_manager.first_search_result( search_term, min_confidence=0.1 @@ -145,6 +149,13 @@ class ImportItem(models.Model): self.normalized_data.get("isbn_10") ) + @property + def openlibrary_key(self): + """the edition identifier is preferable to the work key""" + return self.normalized_data.get("openlibrary_key") or self.normalized_data.get( + "openlibrary_work_key" + ) + @property def shelf(self): """the goodreads shelf field""" diff --git a/bookwyrm/templates/import/import.html b/bookwyrm/templates/import/import.html index 314a6861..fdeb0e55 100644 --- a/bookwyrm/templates/import/import.html +++ b/bookwyrm/templates/import/import.html @@ -31,6 +31,9 @@ +