google.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. if not google_result:
  25. return {}
  26. publish_date = pendulum.parse(google_result.get("publishedDate"))
  27. isbn_13 = ""
  28. isbn_10 = ""
  29. for ident in google_result.get("industryIdentifiers", []):
  30. if ident.get("type") == "ISBN_13":
  31. isbn_13 = ident.get("identifier")
  32. if ident.get("type") == "ISBN_10":
  33. isbn_10 = ident.get("identifier")
  34. # TODO this may lead to issues with the first get if Google changes our title
  35. # book_metadata.title = google_result.get("title")
  36. # if google_result.get("subtitle"):
  37. # book_metadata["title"] = ": ".join(
  38. # [google_result.get("title"), google_result.get("subtitle")]
  39. # )
  40. # book_dict["subtitle"] = google_result.get("subtitle")
  41. book_dict["authors"] = google_result.get("authors")
  42. book_dict["publisher"] = google_result.get("publisher")
  43. book_dict["first_publish_year"] = publish_date.year
  44. book_dict["pages"] = google_result.get("pageCount")
  45. book_dict["isbn_13"] = isbn_13
  46. book_dict["isbn_10"] = isbn_10
  47. book_dict["publish_date"] = google_result.get("publishedDate")
  48. if len(book_dict["publish_date"]) == 4:
  49. book_dict["publish_date"] = f"{book_dict['publish_date']}-1-1"
  50. book_dict["language"] = google_result.get("language")
  51. book_dict["summary"] = google_result.get("description")
  52. book_dict["genres"] = google_result.get("categories")
  53. book_dict["cover_url"] = (
  54. google_result.get("imageLinks", {})
  55. .get("thumbnail", "")
  56. .replace("zoom=1", "zoom=15")
  57. .replace("&edge=curl", "")
  58. )
  59. book_dict["base_run_time_seconds"] = 3600
  60. if book_dict.get("pages"):
  61. book_dict["base_run_time_seconds"] = book_dict.get("pages", 10) * getattr(
  62. settings, "AVERAGE_PAGE_READING_SECONDS", 60
  63. )
  64. return book_dict