bookwyrm/fedireads/connectors/fedireads_connector.py

164 lines
5 KiB
Python
Raw Normal View History

2020-03-28 19:55:53 +00:00
''' using another fedireads instance as a source of book data '''
import requests
2020-03-28 19:55:53 +00:00
from django.core.exceptions import ObjectDoesNotExist
from django.core.files.base import ContentFile
from django.db import transaction
2020-03-28 19:55:53 +00:00
from fedireads import models
from .abstract_connector import AbstractConnector, SearchResult, get_date
from .abstract_connector import match_from_mappings, update_from_mappings
2020-03-28 19:55:53 +00:00
class Connector(AbstractConnector):
2020-05-04 00:53:14 +00:00
''' interact with other instances '''
def __init__(self, identifier):
self.key_mappings = {
'isbn_13': ('isbn_13', None),
'isbn_10': ('isbn_10', None),
'oclc_numbers': ('oclc_number', None),
'lccn': ('lccn', None),
}
super().__init__(identifier)
2020-05-04 00:53:14 +00:00
def format_search_result(self, search_result):
return SearchResult(**search_result)
2020-03-28 19:55:53 +00:00
2020-05-04 00:53:14 +00:00
def get_or_create_book(self, remote_id):
2020-03-28 19:55:53 +00:00
''' pull up a book record by whatever means possible '''
book = models.Book.objects.select_subclasses().filter(
remote_id=remote_id
).first()
if book:
if isinstance(book, models.Work):
return book.default_edition
2020-03-28 19:55:53 +00:00
return book
# no book was found, so we start creating a new one
response = requests.get(
remote_id,
headers={
'Accept': 'application/activity+json; charset=utf-8',
},
)
if not response.ok:
response.raise_for_status()
data = response.json()
if data['book_type'] == 'work':
work_data = data
try:
edition_data = data['editions'][0]
except KeyError:
# hack: re-use the work data as the edition data
edition_data = data
else:
edition_data = data
try:
work_data = data['work']
except KeyError:
# hack: re-use the work data as the edition data
work_data = data
with transaction.atomic():
# create both work and a default edition
work_key = edition_data.get('url')
work = self.create_book(work_key, work_data, models.Work)
ed_key = edition_data.get('url')
edition = self.create_book(ed_key, edition_data, models.Edition)
edition.default = True
edition.parent_work = work
edition.save()
print(work, edition)
return edition
2020-03-28 19:55:53 +00:00
2020-03-28 23:30:54 +00:00
2020-05-04 02:49:32 +00:00
def update_book(self, book, data=None):
2020-03-28 23:30:54 +00:00
''' add remote data to a local book '''
2020-05-04 02:49:32 +00:00
if not data:
response = requests.get(
book.remote_id,
2020-05-04 02:49:32 +00:00
headers={
'Accept': 'application/activity+json; charset=utf-8',
},
)
if not response.ok:
response.raise_for_status()
2020-03-28 19:55:53 +00:00
2020-05-04 02:49:32 +00:00
data = response.json()
2020-03-28 19:55:53 +00:00
match = match_from_mappings(data, {})
if match:
return match
2020-03-28 19:55:53 +00:00
# great, we can update our book.
2020-03-28 20:14:06 +00:00
mappings = {
'published_date': ('published_date', get_date),
'first_published_date': ('first_published_date', get_date),
2020-03-28 19:55:53 +00:00
}
2020-03-28 20:14:06 +00:00
book = update_from_mappings(book, data, mappings)
2020-03-28 19:55:53 +00:00
2020-05-04 01:58:12 +00:00
if not book.remote_id:
book.remote_id = response.url
2020-03-28 23:30:54 +00:00
if not book.connector:
book.connector = self.connector
2020-03-28 19:55:53 +00:00
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))
2020-04-22 13:53:22 +00:00
if book.sync_cover and data.get('covers') and data['covers']:
2020-03-30 00:40:51 +00:00
book.cover.save(*get_cover(data['covers'][0]), save=True)
2020-03-28 19:55:53 +00:00
return book
2020-05-04 00:53:14 +00:00
def get_or_create_author(self, remote_id):
2020-03-28 19:55:53 +00:00
''' load that author '''
try:
2020-05-04 00:53:14 +00:00
return models.Author.objects.get(remote_id=remote_id)
2020-03-28 19:55:53 +00:00
except ObjectDoesNotExist:
pass
2020-05-04 00:53:14 +00:00
resp = requests.get('%s/authors/%s.json' % (self.url, remote_id))
2020-03-28 19:55:53 +00:00
if not resp.ok:
resp.raise_for_status()
data = resp.json()
# ingest a new author
2020-05-04 00:53:14 +00:00
author = models.Author(remote_id=remote_id)
2020-03-28 19:55:53 +00:00
mappings = {
'born': ('born', get_date),
'died': ('died', get_date),
}
author = update_from_mappings(author, data, mappings)
author.save()
return author
def expand_book_data(self, book):
pass
2020-03-30 00:40:51 +00:00
def get_cover(cover_url):
''' ask openlibrary for the cover '''
image_name = cover_url.split('/')[-1]
response = requests.get(cover_url)
if not response.ok:
response.raise_for_status()
image_content = ContentFile(response.content)
return [image_name, image_content]