moviewyrm/bookwyrm/librarything_import.py

43 lines
1.4 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 csv
import re
import math
from bookwyrm import models
from bookwyrm.models import ImportItem
from bookwyrm.importer import Importer
class LibrarythingImporter(Importer):
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
def parse_fields(self, initial):
data = {}
2021-03-08 16:49:10 +00:00
data["import_source"] = self.service
data["Book Id"] = initial["Book Id"]
data["Title"] = initial["Title"]
data["Author"] = initial["Primary Author"]
data["ISBN13"] = initial["ISBN"]
data["My Review"] = initial["Review"]
if initial["Rating"]:
data["My Rating"] = math.ceil(float(initial["Rating"]))
else:
2021-03-08 16:49:10 +00:00
data["My Rating"] = ""
data["Date Added"] = re.sub("\[|\]", "", initial["Entry Date"])
data["Date Started"] = re.sub("\[|\]", "", initial["Date Started"])
data["Date Read"] = re.sub("\[|\]", "", initial["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