diff --git a/bookwyrm/importers/__init__.py b/bookwyrm/importers/__init__.py index f13672e0..d4890070 100644 --- a/bookwyrm/importers/__init__.py +++ b/bookwyrm/importers/__init__.py @@ -3,3 +3,4 @@ from .importer import Importer from .goodreads_import import GoodreadsImporter from .librarything_import import LibrarythingImporter +from .storygraph_import import StorygraphImporter diff --git a/bookwyrm/importers/storygraph_import.py b/bookwyrm/importers/storygraph_import.py new file mode 100644 index 00000000..25498432 --- /dev/null +++ b/bookwyrm/importers/storygraph_import.py @@ -0,0 +1,34 @@ +""" handle reading a csv from librarything """ +import re +import math + +from . import Importer + + +class StorygraphImporter(Importer): + """csv downloads from librarything""" + + service = "Storygraph" + # mandatory_fields : fields matching the book title and author + mandatory_fields = ["Title"] + + def parse_fields(self, entry): + """custom parsing for storygraph""" + data = {} + data["import_source"] = self.service + data["Title"] = entry["Title"] + data["Author"] = entry["Authors"] if "Authors" in entry else entry["Author"] + data["ISBN13"] = entry["ISBN"] + data["My Review"] = entry["Review"] + if entry["Star Rating"]: + data["My Rating"] = math.ceil(float(entry["Star Rating"])) + else: + data["My Rating"] = "" + + data["Date Added"] = re.sub(r"[/]", "-", entry["Date Added"]) + data["Date Read"] = re.sub(r"[/]", "-", entry["Last Date Read"]) + + data["Exclusive Shelf"] = ( + {"read": "read", "currently-reading": "reading", "to-read": "to-read"} + ).get(entry["Read Status"], None) + return data diff --git a/bookwyrm/models/import_job.py b/bookwyrm/models/import_job.py index 1b1152ab..c8130af2 100644 --- a/bookwyrm/models/import_job.py +++ b/bookwyrm/models/import_job.py @@ -128,7 +128,9 @@ class ImportItem(models.Model): @property def rating(self): """x/5 star rating for a book""" - return int(self.data["My Rating"]) + if self.data.get("My Rating", None): + return int(self.data["My Rating"]) + return None @property def date_added(self): diff --git a/bookwyrm/templates/import.html b/bookwyrm/templates/import.html index c4f0dfae..2b7d69e1 100644 --- a/bookwyrm/templates/import.html +++ b/bookwyrm/templates/import.html @@ -20,6 +20,9 @@ + diff --git a/bookwyrm/views/import_data.py b/bookwyrm/views/import_data.py index a2abbc69..2bbb5947 100644 --- a/bookwyrm/views/import_data.py +++ b/bookwyrm/views/import_data.py @@ -10,7 +10,12 @@ from django.utils.decorators import method_decorator from django.views import View from bookwyrm import forms, models -from bookwyrm.importers import Importer, LibrarythingImporter, GoodreadsImporter +from bookwyrm.importers import ( + Importer, + LibrarythingImporter, + GoodreadsImporter, + StorygraphImporter, +) from bookwyrm.tasks import app # pylint: disable= no-self-use @@ -42,6 +47,8 @@ class Import(View): importer = None if source == "LibraryThing": importer = LibrarythingImporter() + elif source == "Storygraph": + importer = StorygraphImporter() else: # Default : GoodReads importer = GoodreadsImporter()