mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-12-23 08:36:32 +00:00
Use author activitypub in OL connector
This commit is contained in:
parent
2128219b05
commit
d3671d981f
2 changed files with 29 additions and 48 deletions
|
@ -98,11 +98,12 @@ class AbstractConnector(AbstractMinimalConnector):
|
||||||
|
|
||||||
# load the json
|
# load the json
|
||||||
data = get_data(remote_id)
|
data = get_data(remote_id)
|
||||||
mapped_data = self.dict_from_mappings(data)
|
mapped_data = dict_from_mappings(data, self.book_mappings)
|
||||||
if self.is_work_data(data):
|
if self.is_work_data(data):
|
||||||
try:
|
try:
|
||||||
edition_data = self.get_edition_from_work_data(data)
|
edition_data = self.get_edition_from_work_data(data)
|
||||||
edition_data = self.dict_from_mappings(edition_data)
|
edition_data = dict_from_mappings(\
|
||||||
|
edition_data, self.book_mappings)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
# hack: re-use the work data as the edition data
|
# hack: re-use the work data as the edition data
|
||||||
# this is why remote ids aren't necessarily unique
|
# this is why remote ids aren't necessarily unique
|
||||||
|
@ -111,7 +112,7 @@ class AbstractConnector(AbstractMinimalConnector):
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
work_data = self.get_work_from_edition_data(data)
|
work_data = self.get_work_from_edition_data(data)
|
||||||
work_data = self.dict_from_mappings(work_data)
|
work_data = dict_from_mappings(work_data, self.book_mappings)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
work_data = mapped_data
|
work_data = mapped_data
|
||||||
edition_data = mapped_data
|
edition_data = mapped_data
|
||||||
|
@ -140,13 +141,17 @@ class AbstractConnector(AbstractMinimalConnector):
|
||||||
return edition
|
return edition
|
||||||
|
|
||||||
|
|
||||||
def dict_from_mappings(self, data):
|
def get_or_create_author(self, remote_id):
|
||||||
''' create a dict in Activitypub format, using mappings supplies by
|
''' load that author '''
|
||||||
the subclass '''
|
existing = models.Author.find_exising_by_remote_id(remote_id)
|
||||||
result = {}
|
if existing:
|
||||||
for mapping in self.book_mapping:
|
return existing
|
||||||
result[mapping.local_field] = mapping.get_value(data)
|
|
||||||
return result
|
data = get_data(remote_id)
|
||||||
|
|
||||||
|
author_activity = dict_from_mappings(data, self.author_mappings)
|
||||||
|
# this will dedupe
|
||||||
|
return activitypub.Author(**author_activity).to_model()
|
||||||
|
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
|
@ -178,20 +183,13 @@ class AbstractConnector(AbstractMinimalConnector):
|
||||||
''' get more info on a book '''
|
''' get more info on a book '''
|
||||||
|
|
||||||
|
|
||||||
def get_date(date_string):
|
def dict_from_mappings(self, data, mappings):
|
||||||
''' helper function to try to interpret dates '''
|
''' create a dict in Activitypub format, using mappings supplies by
|
||||||
if not date_string:
|
the subclass '''
|
||||||
return None
|
result = {}
|
||||||
|
for mapping in mappings:
|
||||||
try:
|
result[mapping.local_field] = mapping.get_value(data)
|
||||||
return pytz.utc.localize(parser.parse(date_string))
|
return result
|
||||||
except ValueError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
try:
|
|
||||||
return parser.parse(date_string)
|
|
||||||
except ValueError:
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def get_data(url):
|
def get_data(url):
|
||||||
|
|
|
@ -6,8 +6,8 @@ from django.core.files.base import ContentFile
|
||||||
|
|
||||||
from bookwyrm import models
|
from bookwyrm import models
|
||||||
from .abstract_connector import AbstractConnector, SearchResult, Mapping
|
from .abstract_connector import AbstractConnector, SearchResult, Mapping
|
||||||
from .abstract_connector import ConnectorException
|
from .abstract_connector import ConnectorException, dict_from_mappings
|
||||||
from .abstract_connector import get_date, get_data, update_from_mappings
|
from .abstract_connector import get_data, update_from_mappings
|
||||||
from .openlibrary_languages import languages
|
from .openlibrary_languages import languages
|
||||||
|
|
||||||
|
|
||||||
|
@ -51,8 +51,8 @@ class Connector(AbstractConnector):
|
||||||
|
|
||||||
self.author_mappings = [
|
self.author_mappings = [
|
||||||
Mapping('name'),
|
Mapping('name'),
|
||||||
Mapping('born', remote_field='birth_date', formatter=get_date),
|
Mapping('born', remote_field='birth_date'),
|
||||||
Mapping('died', remote_field='death_date', formatter=get_date),
|
Mapping('died', remote_field='death_date'),
|
||||||
Mapping('bio', formatter=get_description),
|
Mapping('bio', formatter=get_description),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -92,9 +92,10 @@ class Connector(AbstractConnector):
|
||||||
''' parse author json and load or create authors '''
|
''' parse author json and load or create authors '''
|
||||||
for author_blob in data.get('authors', []):
|
for author_blob in data.get('authors', []):
|
||||||
author_blob = author_blob.get('author', author_blob)
|
author_blob = author_blob.get('author', author_blob)
|
||||||
# this id is "/authors/OL1234567A" and we want just "OL1234567A"
|
# this id is "/authors/OL1234567A"
|
||||||
author_id = author_blob['key'].split('/')[-1]
|
author_id = author_blob['key'].split('/')[-1]
|
||||||
yield self.get_or_create_author(author_id)
|
url = '%s/%s.json' % (self.base_url, author_id)
|
||||||
|
yield self.get_or_create_author(url)
|
||||||
|
|
||||||
|
|
||||||
def get_cover_from_data(self, data):
|
def get_cover_from_data(self, data):
|
||||||
|
@ -158,24 +159,6 @@ class Connector(AbstractConnector):
|
||||||
edition.authors.set(work.authors.all())
|
edition.authors.set(work.authors.all())
|
||||||
|
|
||||||
|
|
||||||
def get_or_create_author(self, olkey):
|
|
||||||
''' load that author '''
|
|
||||||
if not re.match(r'^OL\d+A$', olkey):
|
|
||||||
raise ValueError('Invalid OpenLibrary author ID')
|
|
||||||
author = models.Author.objects.filter(openlibrary_key=olkey).first()
|
|
||||||
if author:
|
|
||||||
return author
|
|
||||||
|
|
||||||
url = '%s/authors/%s.json' % (self.base_url, olkey)
|
|
||||||
data = get_data(url)
|
|
||||||
|
|
||||||
author = models.Author(openlibrary_key=olkey)
|
|
||||||
author = update_from_mappings(author, data, self.author_mappings)
|
|
||||||
author.save()
|
|
||||||
|
|
||||||
return author
|
|
||||||
|
|
||||||
|
|
||||||
def get_description(description_blob):
|
def get_description(description_blob):
|
||||||
''' descriptions can be a string or a dict '''
|
''' descriptions can be a string or a dict '''
|
||||||
if isinstance(description_blob, dict):
|
if isinstance(description_blob, dict):
|
||||||
|
|
Loading…
Reference in a new issue