forked from mirrors/bookwyrm
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
|
||||
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):
|
||||
try:
|
||||
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:
|
||||
# hack: re-use the work data as the edition data
|
||||
# this is why remote ids aren't necessarily unique
|
||||
|
@ -111,7 +112,7 @@ class AbstractConnector(AbstractMinimalConnector):
|
|||
else:
|
||||
try:
|
||||
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:
|
||||
work_data = mapped_data
|
||||
edition_data = mapped_data
|
||||
|
@ -140,13 +141,17 @@ class AbstractConnector(AbstractMinimalConnector):
|
|||
return edition
|
||||
|
||||
|
||||
def dict_from_mappings(self, data):
|
||||
''' create a dict in Activitypub format, using mappings supplies by
|
||||
the subclass '''
|
||||
result = {}
|
||||
for mapping in self.book_mapping:
|
||||
result[mapping.local_field] = mapping.get_value(data)
|
||||
return result
|
||||
def get_or_create_author(self, remote_id):
|
||||
''' load that author '''
|
||||
existing = models.Author.find_exising_by_remote_id(remote_id)
|
||||
if existing:
|
||||
return existing
|
||||
|
||||
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
|
||||
|
@ -178,20 +183,13 @@ class AbstractConnector(AbstractMinimalConnector):
|
|||
''' get more info on a book '''
|
||||
|
||||
|
||||
def get_date(date_string):
|
||||
''' helper function to try to interpret dates '''
|
||||
if not date_string:
|
||||
return None
|
||||
|
||||
try:
|
||||
return pytz.utc.localize(parser.parse(date_string))
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
try:
|
||||
return parser.parse(date_string)
|
||||
except ValueError:
|
||||
return None
|
||||
def dict_from_mappings(self, data, mappings):
|
||||
''' create a dict in Activitypub format, using mappings supplies by
|
||||
the subclass '''
|
||||
result = {}
|
||||
for mapping in mappings:
|
||||
result[mapping.local_field] = mapping.get_value(data)
|
||||
return result
|
||||
|
||||
|
||||
def get_data(url):
|
||||
|
|
|
@ -6,8 +6,8 @@ from django.core.files.base import ContentFile
|
|||
|
||||
from bookwyrm import models
|
||||
from .abstract_connector import AbstractConnector, SearchResult, Mapping
|
||||
from .abstract_connector import ConnectorException
|
||||
from .abstract_connector import get_date, get_data, update_from_mappings
|
||||
from .abstract_connector import ConnectorException, dict_from_mappings
|
||||
from .abstract_connector import get_data, update_from_mappings
|
||||
from .openlibrary_languages import languages
|
||||
|
||||
|
||||
|
@ -51,8 +51,8 @@ class Connector(AbstractConnector):
|
|||
|
||||
self.author_mappings = [
|
||||
Mapping('name'),
|
||||
Mapping('born', remote_field='birth_date', formatter=get_date),
|
||||
Mapping('died', remote_field='death_date', formatter=get_date),
|
||||
Mapping('born', remote_field='birth_date'),
|
||||
Mapping('died', remote_field='death_date'),
|
||||
Mapping('bio', formatter=get_description),
|
||||
]
|
||||
|
||||
|
@ -92,9 +92,10 @@ class Connector(AbstractConnector):
|
|||
''' parse author json and load or create authors '''
|
||||
for author_blob in data.get('authors', []):
|
||||
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]
|
||||
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):
|
||||
|
@ -158,24 +159,6 @@ class Connector(AbstractConnector):
|
|||
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):
|
||||
''' descriptions can be a string or a dict '''
|
||||
if isinstance(description_blob, dict):
|
||||
|
|
Loading…
Reference in a new issue