diff --git a/bookwyrm/importers/__init__.py b/bookwyrm/importers/__init__.py index f13672e06..d4890070b 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 000000000..020adf3e0 --- /dev/null +++ b/bookwyrm/importers/storygraph_import.py @@ -0,0 +1,32 @@ +""" 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", "Author"] + + def parse_fields(self, entry): + """custom parsing for storygraph""" + data = {} + data["import_source"] = self.service + data["Title"] = entry["Title"] + data["Author"] = 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/templates/import.html b/bookwyrm/templates/import.html index c4f0dfae4..2b7d69e10 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 a2abbc695..ccf4ec6d4 100644 --- a/bookwyrm/views/import_data.py +++ b/bookwyrm/views/import_data.py @@ -10,7 +10,7 @@ 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 +42,8 @@ class Import(View): importer = None if source == "LibraryThing": importer = LibrarythingImporter() + elif source == "Storygraph": + importer = StorygraphImporter() else: # Default : GoodReads importer = GoodreadsImporter()