mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-12-25 09:30:33 +00:00
Load covers in fedireads connector
This commit is contained in:
parent
5924e8ed63
commit
8c3e208282
3 changed files with 41 additions and 62 deletions
|
@ -94,6 +94,27 @@ class AbstractConnector(ABC):
|
||||||
return book
|
return book
|
||||||
|
|
||||||
|
|
||||||
|
def update_book(self, book, data=None):
|
||||||
|
''' load new data '''
|
||||||
|
if not book.sync and not book.sync_cover:
|
||||||
|
return
|
||||||
|
|
||||||
|
if not data:
|
||||||
|
key = getattr(book, self.key_name)
|
||||||
|
data = self.load_book_data(key)
|
||||||
|
|
||||||
|
if book.sync_cover:
|
||||||
|
book.cover.save(*self.get_cover_from_data(data), save=True)
|
||||||
|
if book.sync:
|
||||||
|
book = self.update_book_from_data(book, data)
|
||||||
|
return book
|
||||||
|
|
||||||
|
|
||||||
|
def load_book_data(self, remote_id):
|
||||||
|
''' default method for loading book data '''
|
||||||
|
return get_data(remote_id)
|
||||||
|
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def get_authors_from_data(self, data):
|
def get_authors_from_data(self, data):
|
||||||
''' load author data '''
|
''' load author data '''
|
||||||
|
@ -131,12 +152,6 @@ class AbstractConnector(ABC):
|
||||||
# return book model obj
|
# return book model obj
|
||||||
|
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
def update_book(self, book_obj, data=None):
|
|
||||||
''' sync a book with the canonical remote copy '''
|
|
||||||
# return book model obj
|
|
||||||
|
|
||||||
|
|
||||||
def update_from_mappings(obj, data, mappings):
|
def update_from_mappings(obj, data, mappings):
|
||||||
''' assign data to model with mappings '''
|
''' assign data to model with mappings '''
|
||||||
noop = lambda x: x
|
noop = lambda x: x
|
||||||
|
|
|
@ -1,13 +1,15 @@
|
||||||
''' using another fedireads instance as a source of book data '''
|
''' using another fedireads instance as a source of book data '''
|
||||||
import re
|
import re
|
||||||
import requests
|
from uuid import uuid4
|
||||||
|
|
||||||
from django.core.exceptions import ObjectDoesNotExist
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
from django.core.files.base import ContentFile
|
from django.core.files.base import ContentFile
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
|
import requests
|
||||||
|
|
||||||
from fedireads import models
|
from fedireads import models
|
||||||
from .abstract_connector import AbstractConnector, SearchResult, get_date, get_data
|
from .abstract_connector import AbstractConnector, SearchResult
|
||||||
|
from .abstract_connector import get_date, get_data
|
||||||
from .abstract_connector import match_from_mappings, update_from_mappings
|
from .abstract_connector import match_from_mappings, update_from_mappings
|
||||||
|
|
||||||
|
|
||||||
|
@ -20,6 +22,11 @@ class Connector(AbstractConnector):
|
||||||
'oclc_numbers': ('oclc_number', None),
|
'oclc_numbers': ('oclc_number', None),
|
||||||
'lccn': ('lccn', None),
|
'lccn': ('lccn', None),
|
||||||
}
|
}
|
||||||
|
self.book_mappings = self.key_mappings.copy()
|
||||||
|
self.book_mappings.update({
|
||||||
|
'published_date': ('published_date', get_date),
|
||||||
|
'first_published_date': ('first_published_date', get_date),
|
||||||
|
})
|
||||||
super().__init__(identifier)
|
super().__init__(identifier)
|
||||||
|
|
||||||
|
|
||||||
|
@ -77,7 +84,17 @@ class Connector(AbstractConnector):
|
||||||
|
|
||||||
|
|
||||||
def get_cover_from_data(self, data):
|
def get_cover_from_data(self, data):
|
||||||
|
cover_url = data.get('cover')
|
||||||
|
if not cover_url:
|
||||||
return None
|
return None
|
||||||
|
response = requests.get(cover_url)
|
||||||
|
if not response.ok:
|
||||||
|
response.raise_for_status()
|
||||||
|
|
||||||
|
image_name = uuid4() + cover_url.split('.')[-1]
|
||||||
|
image_content = ContentFile(response.content)
|
||||||
|
return [image_name, image_content]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def get_authors_from_data(self, data):
|
def get_authors_from_data(self, data):
|
||||||
|
@ -88,44 +105,6 @@ class Connector(AbstractConnector):
|
||||||
return authors
|
return authors
|
||||||
|
|
||||||
|
|
||||||
def update_book(self, book, data=None):
|
|
||||||
''' add remote data to a local book '''
|
|
||||||
if not data:
|
|
||||||
data = get_data(book.remote_id)
|
|
||||||
|
|
||||||
match = match_from_mappings(data, {})
|
|
||||||
if match:
|
|
||||||
return match
|
|
||||||
|
|
||||||
# great, we can update our book.
|
|
||||||
mappings = {
|
|
||||||
'published_date': ('published_date', get_date),
|
|
||||||
'first_published_date': ('first_published_date', get_date),
|
|
||||||
}
|
|
||||||
book = update_from_mappings(book, data, mappings)
|
|
||||||
|
|
||||||
if not book.remote_id:
|
|
||||||
book.remote_id = response.url
|
|
||||||
if not book.connector:
|
|
||||||
book.connector = self.connector
|
|
||||||
book.save()
|
|
||||||
|
|
||||||
if data.get('parent_work'):
|
|
||||||
work = self.get_or_create_book(data.get('parent_work'))
|
|
||||||
book.parent_work = work
|
|
||||||
|
|
||||||
for author_blob in data.get('authors', []):
|
|
||||||
author_blob = author_blob.get('author', author_blob)
|
|
||||||
author_id = author_blob['key']
|
|
||||||
author_id = author_id.split('/')[-1]
|
|
||||||
book.authors.add(self.get_or_create_author(author_id))
|
|
||||||
|
|
||||||
if book.sync_cover and data.get('covers') and data['covers']:
|
|
||||||
book.cover.save(*get_cover(data['covers'][0]), save=True)
|
|
||||||
|
|
||||||
return book
|
|
||||||
|
|
||||||
|
|
||||||
def get_or_create_author(self, remote_id):
|
def get_or_create_author(self, remote_id):
|
||||||
''' load that author '''
|
''' load that author '''
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -108,21 +108,6 @@ class Connector(AbstractConnector):
|
||||||
return edition
|
return edition
|
||||||
|
|
||||||
|
|
||||||
def update_book(self, book, data=None):
|
|
||||||
''' load new data '''
|
|
||||||
if not book.sync and not book.sync_cover:
|
|
||||||
return
|
|
||||||
|
|
||||||
if not data:
|
|
||||||
data = self.load_book_data(book.openlibrary_key)
|
|
||||||
|
|
||||||
if book.sync_cover and data.get('covers'):
|
|
||||||
book.cover.save(*self.get_cover_from_data(data, save=True))
|
|
||||||
if book.sync:
|
|
||||||
book = self.update_book_from_data(book, data)
|
|
||||||
return book
|
|
||||||
|
|
||||||
|
|
||||||
def get_authors_from_data(self, data):
|
def get_authors_from_data(self, data):
|
||||||
''' parse author json and load or create authors '''
|
''' parse author json and load or create authors '''
|
||||||
authors = []
|
authors = []
|
||||||
|
|
Loading…
Reference in a new issue