forked from mirrors/bookwyrm
Generalizes http request for json data in connectors
This commit is contained in:
parent
3a8d84e9b1
commit
5924e8ed63
3 changed files with 25 additions and 46 deletions
|
@ -206,6 +206,20 @@ def get_date(date_string):
|
|||
return None
|
||||
|
||||
|
||||
def get_data(url):
|
||||
''' wrapper for request.get '''
|
||||
resp = requests.get(
|
||||
url,
|
||||
headers={
|
||||
'Accept': 'application/activity+json; charset=utf-8',
|
||||
},
|
||||
)
|
||||
if not resp.ok:
|
||||
resp.raise_for_status()
|
||||
data = response.json()
|
||||
return data
|
||||
|
||||
|
||||
class SearchResult:
|
||||
''' standardized search result object '''
|
||||
def __init__(self, title, key, author, year):
|
||||
|
|
|
@ -7,7 +7,7 @@ from django.core.files.base import ContentFile
|
|||
from django.db import transaction
|
||||
|
||||
from fedireads import models
|
||||
from .abstract_connector import AbstractConnector, SearchResult, get_date
|
||||
from .abstract_connector import AbstractConnector, SearchResult, get_date, get_data
|
||||
from .abstract_connector import match_from_mappings, update_from_mappings
|
||||
|
||||
|
||||
|
@ -45,15 +45,7 @@ class Connector(AbstractConnector):
|
|||
return book
|
||||
|
||||
# no book was found, so we start creating a new one
|
||||
response = requests.get(
|
||||
remote_id,
|
||||
headers={
|
||||
'Accept': 'application/activity+json; charset=utf-8',
|
||||
},
|
||||
)
|
||||
if not response.ok:
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
data = get_data(remote_id)
|
||||
|
||||
if data['book_type'] == 'work':
|
||||
work_data = data
|
||||
|
@ -99,16 +91,7 @@ class Connector(AbstractConnector):
|
|||
def update_book(self, book, data=None):
|
||||
''' add remote data to a local book '''
|
||||
if not data:
|
||||
response = requests.get(
|
||||
book.remote_id,
|
||||
headers={
|
||||
'Accept': 'application/activity+json; charset=utf-8',
|
||||
},
|
||||
)
|
||||
if not response.ok:
|
||||
response.raise_for_status()
|
||||
|
||||
data = response.json()
|
||||
data = get_data(book.remote_id)
|
||||
|
||||
match = match_from_mappings(data, {})
|
||||
if match:
|
||||
|
@ -150,16 +133,7 @@ class Connector(AbstractConnector):
|
|||
except ObjectDoesNotExist:
|
||||
pass
|
||||
|
||||
resp = requests.get(
|
||||
remote_id,
|
||||
headers={
|
||||
'Accept': 'application/activity+json; charset=utf-8',
|
||||
},
|
||||
)
|
||||
if not resp.ok:
|
||||
resp.raise_for_status()
|
||||
|
||||
data = resp.json()
|
||||
data = get_data(remote_id)
|
||||
|
||||
# ingest a new author
|
||||
author = models.Author(remote_id=remote_id)
|
||||
|
|
|
@ -8,7 +8,7 @@ from django.db import transaction
|
|||
from fedireads import models
|
||||
from .abstract_connector import AbstractConnector, SearchResult
|
||||
from .abstract_connector import update_from_mappings
|
||||
from .abstract_connector import get_date
|
||||
from .abstract_connector import get_date, get_data
|
||||
from .openlibrary_languages import languages
|
||||
|
||||
|
||||
|
@ -136,21 +136,14 @@ class Connector(AbstractConnector):
|
|||
|
||||
def load_book_data(self, olkey):
|
||||
''' query openlibrary for data on a book '''
|
||||
response = requests.get('%s/works/%s.json' % (self.books_url, olkey))
|
||||
if not response.ok:
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
return data
|
||||
url = '%s/works/%s.json' % (self.books_url, olkey)
|
||||
return get_data(url)
|
||||
|
||||
|
||||
def load_edition_data(self, olkey):
|
||||
''' query openlibrary for editions of a work '''
|
||||
response = requests.get(
|
||||
'%s/works/%s/editions.json' % (self.books_url, olkey))
|
||||
if not response.ok:
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
return data
|
||||
url = '%s/works/%s/editions.json' % (self.books_url, olkey)
|
||||
return get_data(url)
|
||||
|
||||
|
||||
def expand_book_data(self, book):
|
||||
|
@ -179,11 +172,9 @@ class Connector(AbstractConnector):
|
|||
except models.Author.DoesNotExist:
|
||||
pass
|
||||
|
||||
response = requests.get('%s/authors/%s.json' % (self.base_url, olkey))
|
||||
if not response.ok:
|
||||
response.raise_for_status()
|
||||
url = '%s/authors/%s.json' % (self.base_url, olkey)
|
||||
data = get_data(url)
|
||||
|
||||
data = response.json()
|
||||
author = models.Author(openlibrary_key=olkey)
|
||||
mappings = {
|
||||
'birth_date': ('born', get_date),
|
||||
|
|
Loading…
Reference in a new issue