Use status source as connector

This commit is contained in:
Mouse Reeve 2020-05-03 19:49:32 -07:00
parent b8568a76c8
commit 7594bed5d3
3 changed files with 50 additions and 13 deletions

View file

@ -40,19 +40,20 @@ class Connector(AbstractConnector):
book = models.Book(remote_id=remote_id)
def update_book(self, book):
def update_book(self, book, data=None):
''' add remote data to a local book '''
remote_id = book.remote_id
response = requests.get(
'%s/%s' % (self.base_url, remote_id),
headers={
'Accept': 'application/activity+json; charset=utf-8',
},
)
if not response.ok:
response.raise_for_status()
if not data:
response = requests.get(
'%s/%s' % (self.base_url, remote_id),
headers={
'Accept': 'application/activity+json; charset=utf-8',
},
)
if not response.ok:
response.raise_for_status()
data = response.json()
data = response.json()
# great, we can update our book.
mappings = {

View file

@ -1,8 +1,9 @@
''' Handle user activity '''
from urllib.parse import urlparse
from django.db import IntegrityError
from fedireads import models
from fedireads.books_manager import get_or_create_book
from fedireads import books_manager, models
from fedireads.sanitize_html import InputHtmlParser
@ -25,6 +26,41 @@ def create_review_from_activity(author, activity):
return review
def get_or_create_book(remote_id):
''' try the remote id and then figure out the right connector '''
book = get_by_absolute_id(remote_id, models.Book)
if book:
return book
connector = get_or_create_connector(remote_id)
return books_manager.get_or_create_book(
remote_id,
key=connector.key_name,
connector_id=connector.id
)
def get_or_create_connector(remote_id):
''' get the connector related to the author's server '''
url = urlparse(remote_id)
identifier = url.netloc
try:
connector_info = models.Connector.objects.get(identifier=identifier)
except models.Connector.DoesNotExist:
models.Connector.objects.create(
identifier=identifier,
connector_file='fedireads_connector',
base_url='https://%s' % identifier,
books_url='https://%s/book' % identifier,
covers_url='https://%s/images/covers' % identifier,
search_url='https://%s/search?q=' % identifier,
key_name='remote_id',
priority=3
)
return books_manager.load_connector(connector_info)
def create_rating(user, book, rating):
''' a review that's just a rating '''
if not rating or rating < 1 or rating > 5:

View file

@ -31,7 +31,7 @@ Connector.objects.create(
books_url='https://%s/book' % DOMAIN,
covers_url='https://%s/images/covers' % DOMAIN,
search_url='https://%s/search?q=' % DOMAIN,
key_name='openlibrary_key',
key_name='id',
priority=1,
)