2020-09-21 15:10:37 +00:00
|
|
|
''' using another bookwyrm instance as a source of book data '''
|
2020-11-04 21:09:11 +00:00
|
|
|
from django.db import transaction
|
2020-03-28 19:55:53 +00:00
|
|
|
|
2020-11-25 00:05:00 +00:00
|
|
|
from bookwyrm import activitypub, models
|
|
|
|
from .abstract_connector import AbstractConnector, SearchResult
|
|
|
|
from .abstract_connector import get_data
|
2020-03-28 19:55:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Connector(AbstractConnector):
|
2020-05-04 00:53:14 +00:00
|
|
|
''' interact with other instances '''
|
2020-11-25 00:05:00 +00:00
|
|
|
|
|
|
|
def update_from_mappings(self, obj, data, mappings):
|
|
|
|
''' serialize book data into a model '''
|
|
|
|
if self.is_work_data(data):
|
|
|
|
work_data = activitypub.Work(**data)
|
|
|
|
return work_data.to_model(models.Work, instance=obj)
|
|
|
|
edition_data = activitypub.Edition(**data)
|
|
|
|
return edition_data.to_model(models.Edition, instance=obj)
|
2020-05-08 23:56:49 +00:00
|
|
|
|
2020-05-04 00:53:14 +00:00
|
|
|
|
2020-10-31 00:04:10 +00:00
|
|
|
def get_remote_id_from_data(self, data):
|
|
|
|
return data.get('id')
|
|
|
|
|
|
|
|
|
2020-05-10 19:56:59 +00:00
|
|
|
def is_work_data(self, data):
|
2020-11-25 19:44:19 +00:00
|
|
|
return data.get('type') == 'Work'
|
2020-03-28 19:55:53 +00:00
|
|
|
|
|
|
|
|
2020-05-10 19:56:59 +00:00
|
|
|
def get_edition_from_work_data(self, data):
|
2020-10-29 19:32:37 +00:00
|
|
|
''' we're served a list of edition urls '''
|
|
|
|
path = data['editions'][0]
|
|
|
|
return get_data(path)
|
2020-05-09 00:56:24 +00:00
|
|
|
|
|
|
|
|
2020-05-10 19:56:59 +00:00
|
|
|
def get_work_from_edition_date(self, data):
|
2020-10-29 19:32:37 +00:00
|
|
|
return get_data(data['work'])
|
2020-05-10 19:56:59 +00:00
|
|
|
|
2020-05-08 23:56:49 +00:00
|
|
|
|
2020-05-10 19:56:59 +00:00
|
|
|
def get_authors_from_data(self, data):
|
2020-11-25 00:05:00 +00:00
|
|
|
''' load author data '''
|
|
|
|
for author_id in data.get('authors', []):
|
|
|
|
try:
|
|
|
|
yield models.Author.objects.get(origin_id=author_id)
|
|
|
|
except models.Author.DoesNotExist:
|
2020-11-27 22:54:08 +00:00
|
|
|
pass
|
2020-11-25 00:05:00 +00:00
|
|
|
data = get_data(author_id)
|
|
|
|
author_data = activitypub.Author(**data)
|
2020-11-27 22:54:08 +00:00
|
|
|
author = author_data.to_model(models.Author)
|
|
|
|
yield author
|
2020-03-28 19:55:53 +00:00
|
|
|
|
2020-03-28 23:30:54 +00:00
|
|
|
|
2020-05-09 19:09:40 +00:00
|
|
|
def get_cover_from_data(self, data):
|
2020-11-25 00:05:00 +00:00
|
|
|
pass
|
2020-03-28 19:55:53 +00:00
|
|
|
|
|
|
|
|
2020-05-10 19:56:59 +00:00
|
|
|
def parse_search_data(self, data):
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
def format_search_result(self, search_result):
|
|
|
|
return SearchResult(**search_result)
|
|
|
|
|
|
|
|
|
2020-05-04 19:36:55 +00:00
|
|
|
def expand_book_data(self, book):
|
2020-11-04 21:09:11 +00:00
|
|
|
work = book
|
|
|
|
# go from the edition to the work, if necessary
|
|
|
|
if isinstance(book, models.Edition):
|
|
|
|
work = book.parent_work
|
|
|
|
|
|
|
|
# it may be that we actually want to request this url
|
2020-11-04 21:18:30 +00:00
|
|
|
editions_url = '%s/editions?page=true' % work.remote_id
|
2020-11-04 21:09:11 +00:00
|
|
|
edition_options = get_data(editions_url)
|
2020-11-04 21:18:30 +00:00
|
|
|
for edition_data in edition_options['orderedItems']:
|
2020-11-04 21:09:11 +00:00
|
|
|
with transaction.atomic():
|
|
|
|
edition = self.create_book(
|
|
|
|
edition_data['id'],
|
|
|
|
edition_data,
|
|
|
|
models.Edition
|
|
|
|
)
|
|
|
|
edition.parent_work = work
|
|
|
|
edition.save()
|
|
|
|
if not edition.authors.exists() and work.authors.exists():
|
|
|
|
edition.authors.set(work.authors.all())
|