Merge pull request #1062 from alisww/storygraph-import

add storygraph import
This commit is contained in:
Mouse Reeve 2021-05-10 16:23:00 -07:00 committed by GitHub
commit 27fe09a32b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 49 additions and 2 deletions

View file

@ -3,3 +3,4 @@
from .importer import Importer from .importer import Importer
from .goodreads_import import GoodreadsImporter from .goodreads_import import GoodreadsImporter
from .librarything_import import LibrarythingImporter from .librarything_import import LibrarythingImporter
from .storygraph_import import StorygraphImporter

View file

@ -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

View file

@ -128,7 +128,9 @@ class ImportItem(models.Model):
@property @property
def rating(self): def rating(self):
"""x/5 star rating for a book""" """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 @property
def date_added(self): def date_added(self):

View file

@ -20,6 +20,9 @@
<option value="GoodReads" {% if current == 'GoodReads' %}selected{% endif %}> <option value="GoodReads" {% if current == 'GoodReads' %}selected{% endif %}>
GoodReads (CSV) GoodReads (CSV)
</option> </option>
<option value="Storygraph" {% if current == 'Storygraph' %}selected{% endif %}>
Storygraph (CSV)
</option>
<option value="LibraryThing" {% if current == 'LibraryThing' %}selected{% endif %}> <option value="LibraryThing" {% if current == 'LibraryThing' %}selected{% endif %}>
LibraryThing (TSV) LibraryThing (TSV)
</option> </option>

View file

@ -10,7 +10,12 @@ from django.utils.decorators import method_decorator
from django.views import View from django.views import View
from bookwyrm import forms, models 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 from bookwyrm.tasks import app
# pylint: disable= no-self-use # pylint: disable= no-self-use
@ -42,6 +47,8 @@ class Import(View):
importer = None importer = None
if source == "LibraryThing": if source == "LibraryThing":
importer = LibrarythingImporter() importer = LibrarythingImporter()
elif source == "Storygraph":
importer = StorygraphImporter()
else: else:
# Default : GoodReads # Default : GoodReads
importer = GoodreadsImporter() importer = GoodreadsImporter()