2021-04-06 18:58:07 +00:00
|
|
|
""" inventaire data connector """
|
|
|
|
from .abstract_connector import AbstractConnector, SearchResult, Mapping
|
|
|
|
from .abstract_connector import get_data
|
2021-04-06 20:39:10 +00:00
|
|
|
from .connector_manager import ConnectorException
|
2021-04-06 18:58:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Connector(AbstractConnector):
|
|
|
|
""" instantiate a connector for OL """
|
|
|
|
|
|
|
|
def __init__(self, identifier):
|
|
|
|
super().__init__(identifier)
|
|
|
|
|
2021-04-06 20:53:58 +00:00
|
|
|
get_first = lambda a, *args: a[0]
|
2021-04-06 20:39:10 +00:00
|
|
|
get_remote_id_list = lambda a, *_: [self.get_remote_id(v) for v in a]
|
2021-04-06 18:58:07 +00:00
|
|
|
self.book_mappings = [
|
2021-04-06 20:53:58 +00:00
|
|
|
Mapping("title", remote_field="wdt:P1476", formatter=get_first),
|
|
|
|
Mapping("subtitle", remote_field="wdt:P1680", formatter=get_first),
|
2021-04-06 20:39:10 +00:00
|
|
|
Mapping("id", remote_field="uri", formatter=self.get_remote_id),
|
2021-04-06 20:53:58 +00:00
|
|
|
# Mapping("authors", remote_field="wdt:P50", formatter=get_remote_id_list),
|
2021-04-06 20:03:22 +00:00
|
|
|
Mapping("inventaireId", remote_field="uri"),
|
2021-04-06 18:58:07 +00:00
|
|
|
Mapping("cover", remote_field="image", formatter=self.get_cover_url),
|
2021-04-06 20:53:58 +00:00
|
|
|
Mapping("isbn13", remote_field="wdt:P212", formatter=get_first),
|
|
|
|
Mapping("isbn10", remote_field="wdt:P957", formatter=get_first),
|
|
|
|
# Mapping("languages", remote_field="wdt:P407", formatter=get_language),
|
|
|
|
# Mapping("publishers", remote_field="wdt:P123", formatter=resolve_key),
|
|
|
|
Mapping("publishedDate", remote_field="wdt:P577", formatter=get_first),
|
|
|
|
Mapping("pages", remote_field="wdt:P1104", formatter=get_first),
|
|
|
|
Mapping("goodreadsKey", remote_field="wdt:P2969", formatter=get_first),
|
|
|
|
Mapping("openlibraryKey", remote_field="wdt:P648", formatter=get_first),
|
|
|
|
# Mapping("subjectPlaces", remote_field="wdt:P840", formatter=resolve_key),
|
|
|
|
# Mapping("subjects", remote_field="wdt:P921", formatter=resolve_key),
|
|
|
|
Mapping("librarythingKey", remote_field="wdt:P1085", formatter=get_first),
|
|
|
|
Mapping("oclcNumber", remote_field="wdt:P5331", formatter=get_first),
|
|
|
|
Mapping("asin", remote_field="wdt:P5749", formatter=get_first),
|
2021-04-06 18:58:07 +00:00
|
|
|
]
|
2021-04-06 20:03:22 +00:00
|
|
|
# TODO: P136: genre, P268 bnf id, P674 characters, P950 bne
|
2021-04-06 18:58:07 +00:00
|
|
|
|
2021-04-06 20:39:10 +00:00
|
|
|
def get_remote_id(self, value, *_):
|
|
|
|
""" convert an id/uri into a url """
|
2021-04-06 20:53:58 +00:00
|
|
|
return "{:s}?action=by-uris&uris={:s}".format(self.books_url, value)
|
2021-04-06 20:39:10 +00:00
|
|
|
|
2021-04-06 19:29:06 +00:00
|
|
|
def get_book_data(self, remote_id):
|
|
|
|
data = get_data(remote_id)
|
|
|
|
extracted = list(data.get("entities").values())
|
2021-04-06 20:53:58 +00:00
|
|
|
try:
|
|
|
|
data = extracted[0]
|
|
|
|
except KeyError:
|
|
|
|
raise ConnectorException("Invalid book data")
|
|
|
|
# flatten the data so that images, uri, and claims are on the same level
|
|
|
|
return {
|
|
|
|
**data.get("claims"),
|
|
|
|
"uri": data.get("uri"),
|
|
|
|
"image": data.get("image"),
|
|
|
|
}
|
2021-04-06 19:29:06 +00:00
|
|
|
|
2021-04-06 18:58:07 +00:00
|
|
|
def parse_search_data(self, data):
|
2021-04-06 20:53:58 +00:00
|
|
|
return data.get("results")
|
2021-04-06 18:58:07 +00:00
|
|
|
|
|
|
|
def format_search_result(self, search_result):
|
|
|
|
images = search_result.get("image")
|
2021-04-06 20:53:58 +00:00
|
|
|
cover = (
|
|
|
|
"{:s}/img/entities/{:s}".format(self.covers_url, images[0])
|
|
|
|
if images
|
|
|
|
else None
|
|
|
|
)
|
2021-04-06 18:58:07 +00:00
|
|
|
return SearchResult(
|
|
|
|
title=search_result.get("label"),
|
2021-04-06 20:39:10 +00:00
|
|
|
key="{:s}?action=by-uris&uris={:s}".format(
|
|
|
|
self.books_url, search_result.get("uri")
|
|
|
|
),
|
2021-04-06 19:17:58 +00:00
|
|
|
view_link="{:s}{:s}".format(self.base_url, search_result.get("uri")),
|
2021-04-06 18:58:07 +00:00
|
|
|
cover=cover,
|
|
|
|
connector=self,
|
|
|
|
)
|
|
|
|
|
|
|
|
def parse_isbn_search_data(self, data):
|
|
|
|
""" boop doop """
|
|
|
|
|
|
|
|
def format_isbn_search_result(self, search_result):
|
|
|
|
""" beep bloop """
|
|
|
|
|
|
|
|
def is_work_data(self, data):
|
2021-04-06 19:29:06 +00:00
|
|
|
return data.get("type") == "work"
|
2021-04-06 18:58:07 +00:00
|
|
|
|
|
|
|
def get_edition_from_work_data(self, data):
|
2021-04-06 20:39:10 +00:00
|
|
|
value = data.get("uri")
|
|
|
|
url = "{:s}?action=reverse-claims&property=P629&value={:s}".format(
|
|
|
|
self.books_url, value
|
|
|
|
)
|
|
|
|
data = get_data(url)
|
|
|
|
try:
|
|
|
|
uri = data["uris"][0]
|
|
|
|
except KeyError:
|
|
|
|
raise ConnectorException("Invalid book data")
|
|
|
|
return self.get_book_data(self.get_remote_id(uri))
|
2021-04-06 18:58:07 +00:00
|
|
|
|
|
|
|
def get_work_from_edition_data(self, data):
|
2021-04-06 20:39:10 +00:00
|
|
|
try:
|
|
|
|
uri = data["claims"]["wdt:P629"]
|
|
|
|
except KeyError:
|
|
|
|
raise ConnectorException("Invalid book data")
|
|
|
|
return self.get_book_data(self.get_remote_id(uri))
|
|
|
|
|
2021-04-06 18:58:07 +00:00
|
|
|
def get_authors_from_data(self, data):
|
|
|
|
return []
|
|
|
|
|
|
|
|
def expand_book_data(self, book):
|
|
|
|
return
|
|
|
|
|
|
|
|
def get_cover_url(self, cover_blob, *_):
|
2021-04-06 20:53:58 +00:00
|
|
|
"""format the relative cover url into an absolute one:
|
2021-04-06 18:58:07 +00:00
|
|
|
{"url": "/img/entities/e794783f01b9d4f897a1ea9820b96e00d346994f"}
|
|
|
|
"""
|
|
|
|
cover_id = cover_blob[0].get("url")
|
|
|
|
if not cover_id:
|
|
|
|
return None
|
|
|
|
return "%s%s" % (self.covers_url, cover_id)
|
|
|
|
|
|
|
|
|
|
|
|
def get_language(wikidata_key, *_):
|
|
|
|
""" who here speaks "wd:Q150" """
|
|
|
|
return wikidata_key # TODO
|
|
|
|
|
2021-04-06 20:53:58 +00:00
|
|
|
|
2021-04-06 18:58:07 +00:00
|
|
|
def resolve_key(wikidata_key, *_):
|
|
|
|
""" cool, it's "wd:Q3156592" now what the heck does that mean """
|
|
|
|
return wikidata_key # TODO
|