openlibrary.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import json
  2. import requests
  3. import logging
  4. logger = logging.getLogger(__name__)
  5. SEARCH_URL = "https://openlibrary.org/search.json?title={title}"
  6. ISBN_URL = "https://openlibrary.org/isbn/{isbn}.json"
  7. def get_first(key: str, result: dict) -> str:
  8. obj = ""
  9. if obj_list := result.get(key):
  10. obj = obj_list[0]
  11. return obj
  12. def lookup_book_from_openlibrary(title: str, author: str = None) -> dict:
  13. search_url = SEARCH_URL.format(title=title)
  14. response = requests.get(search_url)
  15. if response.status_code != 200:
  16. logger.warn(f"Bad response from OL: {response.status_code}")
  17. return {}
  18. results = json.loads(response.content)
  19. if len(results.get("docs")) == 0:
  20. logger.warn(f"No results found from OL for {title}")
  21. return {}
  22. top = results.get("docs")[0]
  23. if author and author not in top["author_name"]:
  24. logger.warn(
  25. f"Lookup for {title} found top result with mismatched author"
  26. )
  27. return {
  28. "title": top.get("title"),
  29. "isbn": top.get("isbn")[0],
  30. "openlibrary_id": top.get("cover_edition_key"),
  31. "author_name": get_first("author_name", top),
  32. "author_openlibrary_id": get_first("author_key", top),
  33. "goodreads_id": get_first("id_goodreads", top),
  34. "first_publish_year": top.get("first_publish_year"),
  35. }