Adds support for openlibrary csv shelf name format

This commit is contained in:
Mouse Reeve 2021-12-14 11:58:56 -08:00
parent 79af354dfd
commit bb69c32a6c
6 changed files with 106 additions and 6 deletions

View file

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

View file

@ -26,7 +26,7 @@ class Importer:
("authors", ["author", "authors", "primary author"]),
("isbn_10", ["isbn10", "isbn"]),
("isbn_13", ["isbn13", "isbn", "isbns"]),
("shelf", ["shelf", "exclusive shelf", "read status"]),
("shelf", ["shelf", "exclusive shelf", "read status", "bookshelf"]),
("review_name", ["review name"]),
("review_body", ["my review", "review"]),
("rating", ["my rating", "rating", "star rating"]),
@ -36,9 +36,9 @@ class Importer:
]
date_fields = ["date_added", "date_started", "date_finished"]
shelf_mapping_guesses = {
"to-read": ["to-read"],
"read": ["read"],
"reading": ["currently-reading", "reading"],
"to-read": ["to-read", "want to read"],
"read": ["read", "already read"],
"reading": ["currently-reading", "reading", "currently reading"],
}
def create_job(self, user, csv_file, include_reviews, privacy):
@ -90,7 +90,7 @@ class Importer:
def get_shelf(self, normalized_row):
"""determine which shelf to use"""
shelf_name = normalized_row["shelf"]
shelf_name = normalized_row["shelf"].lower()
shelf = [
s for (s, gs) in self.shelf_mapping_guesses.items() if shelf_name in gs
]

View file

@ -0,0 +1,8 @@
""" handle reading a csv from openlibrary"""
from . import Importer
class OpenLibraryImporter(Importer):
"""csv downloads from OpenLibrary"""
service = "OpenLibrary"

View file

@ -3,6 +3,6 @@ from . import Importer
class StorygraphImporter(Importer):
"""csv downloads from librarything"""
"""csv downloads from Storygraph"""
service = "Storygraph"

View file

@ -0,0 +1,5 @@
Work Id,Edition Id,Bookshelf
OL102749W,,Currently Reading
OL361393W,OL7798182M,Currently Reading
OL1652392W,OL7194114M,Want to Read
OL17062644W,OL25726365M,Already Read
1 Work Id Edition Id Bookshelf
2 OL102749W Currently Reading
3 OL361393W OL7798182M Currently Reading
4 OL1652392W OL7194114M Want to Read
5 OL17062644W OL25726365M Already Read

View file

@ -0,0 +1,86 @@
""" testing import """
import pathlib
from unittest.mock import patch
import datetime
import pytz
from django.test import TestCase
from bookwyrm import models
from bookwyrm.importers import OpenLibraryImporter
from bookwyrm.importers.importer import handle_imported_book
def make_date(*args):
"""helper function to easily generate a date obj"""
return datetime.datetime(*args, tzinfo=pytz.UTC)
# pylint: disable=consider-using-with
@patch("bookwyrm.suggested_users.rerank_suggestions_task.delay")
@patch("bookwyrm.activitystreams.populate_stream_task.delay")
@patch("bookwyrm.activitystreams.add_book_statuses_task.delay")
class OpenLibraryImport(TestCase):
"""importing from openlibrary csv"""
def setUp(self):
"""use a test csv"""
self.importer = OpenLibraryImporter()
datafile = pathlib.Path(__file__).parent.joinpath("../data/openlibrary.csv")
self.csv = open(datafile, "r", encoding=self.importer.encoding)
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
):
self.local_user = models.User.objects.create_user(
"mouse", "mouse@mouse.mouse", "password", local=True
)
work = models.Work.objects.create(title="Test Work")
self.book = models.Edition.objects.create(
title="Example Edition",
remote_id="https://example.com/book/1",
parent_work=work,
)
def test_create_job(self, *_):
"""creates the import job entry and checks csv"""
import_job = self.importer.create_job(
self.local_user, self.csv, False, "public"
)
import_items = models.ImportItem.objects.filter(job=import_job).all()
self.assertEqual(len(import_items), 4)
self.assertEqual(import_items[0].index, 0)
self.assertEqual(import_items[0].data["Work Id"], "OL102749W")
self.assertEqual(import_items[1].data["Work Id"], "OL361393W")
self.assertEqual(import_items[1].data["Edition Id"], "OL7798182M")
self.assertEqual(import_items[0].normalized_data["shelf"], "reading")
self.assertEqual(import_items[2].normalized_data["shelf"], "to-read")
self.assertEqual(import_items[3].normalized_data["shelf"], "read")
def test_handle_imported_book(self, *_):
"""openlibrary import added a book, this adds related connections"""
shelf = self.local_user.shelf_set.filter(identifier="read").first()
self.assertIsNone(shelf.books.first())
import_job = self.importer.create_job(
self.local_user, self.csv, False, "public"
)
import_item = import_job.items.first()
import_item.book = self.book
import_item.save()
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
handle_imported_book(import_item)
shelf.refresh_from_db()
self.assertEqual(shelf.books.first(), self.book)
self.assertEqual(
shelf.shelfbook_set.first().shelved_date, make_date(2020, 10, 21)
)
readthrough = models.ReadThrough.objects.get(user=self.local_user)
self.assertEqual(readthrough.book, self.book)
self.assertEqual(readthrough.start_date, make_date(2020, 10, 21))
self.assertEqual(readthrough.finish_date, make_date(2020, 10, 25))