Merge pull request #1108 from bookwyrm-social/ol-redirects

Handle openlibrary redirects
This commit is contained in:
Mouse Reeve 2021-05-20 16:18:51 -07:00 committed by GitHub
commit b6c17defd7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -58,6 +58,13 @@ class Connector(AbstractConnector):
Mapping("bio", formatter=get_description),
]
def get_book_data(self, remote_id):
data = get_data(remote_id)
if data.get("type", {}).get("key") == "/type/redirect":
remote_id = self.base_url + data.get("location")
return get_data(remote_id)
return data
def get_remote_id_from_data(self, data):
"""format a url from an openlibrary id field"""
try:
@ -75,7 +82,7 @@ class Connector(AbstractConnector):
except KeyError:
raise ConnectorException("Invalid book data")
url = "%s%s/editions" % (self.books_url, key)
data = get_data(url)
data = self.get_book_data(url)
edition = pick_default_edition(data["entries"])
if not edition:
raise ConnectorException("No editions for work")
@ -87,7 +94,7 @@ class Connector(AbstractConnector):
except (IndexError, KeyError):
raise ConnectorException("No work found for edition")
url = "%s%s" % (self.books_url, key)
return get_data(url)
return self.get_book_data(url)
def get_authors_from_data(self, data):
"""parse author json and load or create authors"""
@ -146,7 +153,7 @@ class Connector(AbstractConnector):
def load_edition_data(self, olkey):
"""query openlibrary for editions of a work"""
url = "%s/works/%s/editions" % (self.books_url, olkey)
return get_data(url)
return self.get_book_data(url)
def expand_book_data(self, book):
work = book