2020-03-28 02:52:05 +00:00
|
|
|
''' federate book data '''
|
|
|
|
from fedireads.settings import DOMAIN
|
|
|
|
|
2020-05-09 00:36:02 +00:00
|
|
|
def get_book(book, recursive=True):
|
2020-03-28 02:52:05 +00:00
|
|
|
''' activitypub serialize a book '''
|
|
|
|
|
2020-03-28 04:28:52 +00:00
|
|
|
fields = [
|
2020-05-08 23:56:49 +00:00
|
|
|
'title',
|
2020-03-28 04:28:52 +00:00
|
|
|
'sort_title',
|
|
|
|
'subtitle',
|
2020-04-29 17:09:14 +00:00
|
|
|
'isbn_13',
|
2020-03-28 04:28:52 +00:00
|
|
|
'oclc_number',
|
|
|
|
'openlibrary_key',
|
|
|
|
'librarything_key',
|
|
|
|
'lccn',
|
|
|
|
'oclc_number',
|
|
|
|
'pages',
|
|
|
|
'physical_format',
|
|
|
|
'misc_identifiers',
|
|
|
|
|
|
|
|
'description',
|
2020-03-30 20:15:49 +00:00
|
|
|
'languages',
|
2020-03-28 04:28:52 +00:00
|
|
|
'series',
|
|
|
|
'series_number',
|
|
|
|
'subjects',
|
|
|
|
'subject_places',
|
|
|
|
'pages',
|
|
|
|
'physical_format',
|
|
|
|
]
|
|
|
|
|
2020-05-08 23:56:49 +00:00
|
|
|
book_type = type(book).__name__
|
2020-03-28 02:52:05 +00:00
|
|
|
activity = {
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
|
|
'type': 'Document',
|
2020-05-08 23:56:49 +00:00
|
|
|
'book_type': book_type,
|
2020-03-28 02:52:05 +00:00
|
|
|
'name': book.title,
|
|
|
|
'url': book.absolute_id,
|
|
|
|
|
2020-05-09 19:09:40 +00:00
|
|
|
'authors': [a.absolute_id for a in book.authors.all()],
|
2020-03-28 02:52:05 +00:00
|
|
|
'first_published_date': book.first_published_date.isoformat() if \
|
|
|
|
book.first_published_date else None,
|
|
|
|
'published_date': book.published_date.isoformat() if \
|
|
|
|
book.published_date else None,
|
|
|
|
}
|
2020-05-09 00:36:02 +00:00
|
|
|
if recursive:
|
|
|
|
if book_type == 'Edition':
|
|
|
|
activity['work'] = get_book(book.parent_work, recursive=False)
|
|
|
|
else:
|
|
|
|
editions = book.edition_set.order_by('default')
|
|
|
|
activity['editions'] = [
|
|
|
|
get_book(b, recursive=False) for b in editions]
|
2020-05-08 23:56:49 +00:00
|
|
|
|
2020-03-28 04:28:52 +00:00
|
|
|
for field in fields:
|
|
|
|
if hasattr(book, field):
|
|
|
|
activity[field] = book.__getattribute__(field)
|
2020-03-28 02:52:05 +00:00
|
|
|
|
|
|
|
if book.cover:
|
|
|
|
image_path = book.cover.url
|
|
|
|
image_type = image_path.split('.')[-1]
|
|
|
|
activity['attachment'] = [{
|
|
|
|
'type': 'Document',
|
|
|
|
'mediaType': 'image/%s' % image_type,
|
|
|
|
'url': 'https://%s%s' % (DOMAIN, image_path),
|
|
|
|
'name': 'Cover of "%s"' % book.title,
|
|
|
|
}]
|
2020-03-28 04:28:52 +00:00
|
|
|
return {k: v for (k, v) in activity.items() if v}
|
2020-03-28 02:52:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_author(author):
|
|
|
|
''' serialize an author '''
|
2020-05-09 19:09:40 +00:00
|
|
|
fields = [
|
|
|
|
'name',
|
|
|
|
'born',
|
|
|
|
'died',
|
|
|
|
'aliases',
|
|
|
|
'bio'
|
|
|
|
'openlibrary_key',
|
|
|
|
'wikipedia_link',
|
|
|
|
]
|
|
|
|
activity = {
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
2020-03-28 02:52:05 +00:00
|
|
|
'url': author.absolute_id,
|
2020-05-09 19:09:40 +00:00
|
|
|
'type': 'Person',
|
2020-03-28 02:52:05 +00:00
|
|
|
}
|
2020-05-09 19:09:40 +00:00
|
|
|
for field in fields:
|
|
|
|
if hasattr(author, field):
|
|
|
|
activity[field] = author.__getattribute__(field)
|
|
|
|
return activity
|