moviewyrm/bookwyrm/importers/librarything_import.py

43 lines
1.3 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" handle reading a csv from librarything """
2021-02-20 16:02:36 +00:00
import re
import math
2021-03-30 15:56:25 +00:00
from . import Importer
2021-02-20 16:02:36 +00:00
class LibrarythingImporter(Importer):
2021-04-26 16:15:42 +00:00
"""csv downloads from librarything"""
2021-03-30 15:43:38 +00:00
2021-03-08 16:49:10 +00:00
service = "LibraryThing"
delimiter = "\t"
encoding = "ISO-8859-1"
# mandatory_fields : fields matching the book title and author
2021-03-08 16:49:10 +00:00
mandatory_fields = ["Title", "Primary Author"]
2021-02-20 16:02:36 +00:00
2021-03-30 15:43:38 +00:00
def parse_fields(self, entry):
2021-04-26 16:15:42 +00:00
"""custom parsing for librarything"""
2021-02-20 16:02:36 +00:00
data = {}
2021-03-08 16:49:10 +00:00
data["import_source"] = self.service
2021-03-30 15:43:38 +00:00
data["Book Id"] = entry["Book Id"]
data["Title"] = entry["Title"]
data["Author"] = entry["Primary Author"]
data["ISBN13"] = entry["ISBN"]
data["My Review"] = entry["Review"]
if entry["Rating"]:
data["My Rating"] = math.ceil(float(entry["Rating"]))
else:
2021-03-08 16:49:10 +00:00
data["My Rating"] = ""
2021-03-30 15:43:38 +00:00
data["Date Added"] = re.sub(r"\[|\]", "", entry["Entry Date"])
data["Date Started"] = re.sub(r"\[|\]", "", entry["Date Started"])
data["Date Read"] = re.sub(r"\[|\]", "", entry["Date Read"])
2021-02-20 16:02:36 +00:00
2021-03-08 16:49:10 +00:00
data["Exclusive Shelf"] = None
if data["Date Read"]:
data["Exclusive Shelf"] = "read"
elif data["Date Started"]:
data["Exclusive Shelf"] = "reading"
2021-02-20 16:02:36 +00:00
else:
2021-03-08 16:49:10 +00:00
data["Exclusive Shelf"] = "to-read"
2021-02-20 16:02:36 +00:00
return data