openlibrary.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. SEARCH_URL = "https://openlibrary.org/search.json?title={title}"
  8. COVER_URL = "https://covers.openlibrary.org/b/olid/{id}-L.jpg"
  9. def get_first(key: str, result: dict) -> str:
  10. obj = ""
  11. if obj_list := result.get(key):
  12. obj = obj_list[0]
  13. return obj
  14. def lookup_book_from_openlibrary(title: str, author: str = None) -> dict:
  15. search_url = SEARCH_URL.format(title=title)
  16. response = requests.get(search_url)
  17. if response.status_code != 200:
  18. logger.warn(f"Bad response from OL: {response.status_code}")
  19. return {}
  20. results = json.loads(response.content)
  21. if len(results.get("docs")) == 0:
  22. logger.warn(f"No results found from OL for {title}")
  23. return {}
  24. top = results.get("docs")[0]
  25. if author and author not in top["author_name"]:
  26. logger.warn(
  27. f"Lookup for {title} found top result with mismatched author"
  28. )
  29. ol_id = top.get("cover_edition_key")
  30. return {
  31. "title": top.get("title"),
  32. "isbn": top.get("isbn")[0],
  33. "openlibrary_id": top.get("cover_edition_key"),
  34. "author_name": get_first("author_name", top),
  35. "author_openlibrary_id": get_first("author_key", top),
  36. "goodreads_id": get_first("id_goodreads", top),
  37. "first_publish_year": top.get("first_publish_year"),
  38. "pages": top.get("number_of_pages_median"),
  39. "cover_url": COVER_URL.format(id=ol_id),
  40. }