google.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import json
  2. import logging
  3. import pendulum
  4. import requests
  5. from django.conf import settings
  6. API_KEY = settings.GOOGLE_API_KEY
  7. GOOGLE_BOOKS_URL = (
  8. "https://www.googleapis.com/books/v1/volumes?q={title}&key={key}"
  9. )
  10. logger = logging.getLogger(__name__)
  11. def lookup_book_from_google(title: str) -> dict:
  12. book_dict = {"title": title}
  13. url = GOOGLE_BOOKS_URL.format(title=title, key=API_KEY)
  14. headers = {"User-Agent": "Vrobbler 0.11.12"}
  15. response = requests.get(url, headers=headers)
  16. if response.status_code != 200:
  17. logger.warning(
  18. "Bad response from Google", extra={"response": response}
  19. )
  20. return book_dict
  21. google_result = (
  22. json.loads(response.content).get("items", [{}])[0].get("volumeInfo")
  23. )
  24. publish_date = pendulum.parse(google_result.get("publishedDate"))
  25. isbn_13 = ""
  26. isbn_10 = ""
  27. for ident in google_result.get("industryIdentifiers", []):
  28. if ident.get("type") == "ISBN_13":
  29. isbn_13 = ident.get("identifier")
  30. if ident.get("type") == "ISBN_10":
  31. isbn_10 = ident.get("identifier")
  32. # TODO this may lead to issues with the first get if Google changes our title
  33. # book_metadata.title = google_result.get("title")
  34. # if google_result.get("subtitle"):
  35. # book_metadata["title"] = ": ".join(
  36. # [google_result.get("title"), google_result.get("subtitle")]
  37. # )
  38. book_dict["subtitle"] = google_result.get("subtitle")
  39. book_dict["authors"] = google_result.get("authors")
  40. book_dict["publisher"] = google_result.get("publisher")
  41. book_dict["first_publish_year"] = publish_date.year
  42. book_dict["pages"] = google_result.get("pageCount")
  43. book_dict["isbn_13"] = isbn_13
  44. book_dict["isbn_10"] = isbn_10
  45. book_dict["publish_date"] = google_result.get("publishedDate")
  46. book_dict["language"] = google_result.get("language")
  47. book_dict["summary"] = google_result.get("description")
  48. book_dict["genres"] = google_result.get("categories")
  49. book_dict["cover_url"] = (
  50. google_result.get("imageLinks", {})
  51. .get("thumbnail")
  52. .replace("zoom=1", "zoom=15")
  53. .replace("&edge=curl", "")
  54. )
  55. book_dict["run_time_seconds"] = book_dict.get("pages", 10) * getattr(
  56. settings, "AVERAGE_PAGE_READING_SECONDS", 60
  57. )
  58. return book_dict