Fix book importing
This commit is contained in:
@ -1,6 +1,8 @@
|
||||
import json
|
||||
import requests
|
||||
import logging
|
||||
import urllib
|
||||
|
||||
import requests
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@ -8,6 +10,8 @@ SEARCH_URL = "https://openlibrary.org/search.json?title={title}"
|
||||
ISBN_URL = "https://openlibrary.org/isbn/{isbn}.json"
|
||||
SEARCH_URL = "https://openlibrary.org/search.json?title={title}"
|
||||
COVER_URL = "https://covers.openlibrary.org/b/olid/{id}-L.jpg"
|
||||
AUTHOR_URL = "https://openlibrary.org/authors/{id}.json"
|
||||
AUTHOR_IMAGE_URL = "https://covers.openlibrary.org/a/olid/{id}-L.jpg"
|
||||
|
||||
|
||||
def get_first(key: str, result: dict) -> str:
|
||||
@ -17,8 +21,43 @@ def get_first(key: str, result: dict) -> str:
|
||||
return obj
|
||||
|
||||
|
||||
def lookup_author_from_openlibrary(olid: str) -> dict:
|
||||
author_url = AUTHOR_URL.format(id=olid)
|
||||
response = requests.get(author_url)
|
||||
|
||||
if response.status_code != 200:
|
||||
logger.warn(f"Bad response from OL: {response.status_code}")
|
||||
return {}
|
||||
|
||||
results = json.loads(response.content)
|
||||
|
||||
if not results:
|
||||
logger.warn(f"No author results found from OL for {olid}")
|
||||
return {}
|
||||
|
||||
remote_ids = results.get("remote_ids", {})
|
||||
bio = ""
|
||||
if results.get("bio"):
|
||||
try:
|
||||
bio = results.get("bio").get("value")
|
||||
except AttributeError:
|
||||
bio = results.get("bio")
|
||||
return {
|
||||
"name": results.get("name"),
|
||||
"wikipedia_url": results.get("wikipedia"),
|
||||
"wikidata_id": remote_ids.get("wikidata"),
|
||||
"isni": remote_ids.get("isni"),
|
||||
"goodreads_id": remote_ids.get("goodreads"),
|
||||
"librarything_id": remote_ids.get("librarything"),
|
||||
"amazon_id": remote_ids.get("amazon"),
|
||||
"bio": bio,
|
||||
"author_headshot_url": AUTHOR_IMAGE_URL.format(id=olid),
|
||||
}
|
||||
|
||||
|
||||
def lookup_book_from_openlibrary(title: str, author: str = None) -> dict:
|
||||
search_url = SEARCH_URL.format(title=title)
|
||||
title_quoted = urllib.parse.quote(title)
|
||||
search_url = SEARCH_URL.format(title=title_quoted)
|
||||
response = requests.get(search_url)
|
||||
|
||||
if response.status_code != 200:
|
||||
@ -37,14 +76,22 @@ def lookup_book_from_openlibrary(title: str, author: str = None) -> dict:
|
||||
f"Lookup for {title} found top result with mismatched author"
|
||||
)
|
||||
ol_id = top.get("cover_edition_key")
|
||||
author_ol_id = get_first("author_key", top)
|
||||
first_sentence = ""
|
||||
if top.get("first_sentence"):
|
||||
try:
|
||||
first_sentence = top.get("first_sentence")[0].get("value")
|
||||
except AttributeError:
|
||||
first_sentence = top.get("first_sentence")[0]
|
||||
print(top)
|
||||
return {
|
||||
"title": top.get("title"),
|
||||
"isbn": top.get("isbn")[0],
|
||||
"openlibrary_id": top.get("cover_edition_key"),
|
||||
"author_name": get_first("author_name", top),
|
||||
"author_openlibrary_id": get_first("author_key", top),
|
||||
"openlibrary_id": ol_id,
|
||||
"goodreads_id": get_first("id_goodreads", top),
|
||||
"first_publish_year": top.get("first_publish_year"),
|
||||
"pages": top.get("number_of_pages_median"),
|
||||
"first_sentence": first_sentence,
|
||||
"pages": top.get("number_of_pages_median", None),
|
||||
"cover_url": COVER_URL.format(id=ol_id),
|
||||
"ol_author_id": author_ol_id,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user