_bol.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import requests
  2. from bs4 import BeautifulSoup
  3. from isbnlib.dev import stdmeta
  4. from isbnlib.dev._exceptions import NoDataForSelectorError
  5. class Book:
  6. LANGUAGE_MAP = {"Engels": "en", "Nederlands": "nl", "Duits": "de"}
  7. def __init__(self, isbn, html):
  8. self.html = html
  9. self.specs = {}
  10. self.isbn = isbn
  11. self.parse_specs()
  12. @property
  13. def title(self):
  14. return self.html.find("h1", class_="pdp-header__title").text.strip()
  15. @property
  16. def authors(self):
  17. return self.specs["Auteur"].split("\n")
  18. @property
  19. def publisher(self):
  20. return self.specs["Uitgever"]
  21. @property
  22. def year(self):
  23. return int(self.specs["Verschijningsdatum"].split(" ")[-1])
  24. @property
  25. def language(self):
  26. lang = self.specs["Taal"]
  27. if lang in self.LANGUAGE_MAP:
  28. return self.LANGUAGE_MAP[lang]
  29. else:
  30. print(f"Warning: language {lang} not found in mapping")
  31. return lang
  32. def as_record(self):
  33. return {"Title": self.title,
  34. "Authors": self.authors,
  35. "Year": self.year,
  36. "Publisher": self.publisher,
  37. "Language": self.language,
  38. "ISBN-13": self.isbn}
  39. def parse_specs(self):
  40. specs_lists = self.html.find_all("dl", class_="specs__list")
  41. for specs_list in specs_lists:
  42. keys = [el.text.strip() for el in specs_list.find_all("dt")]
  43. values = [el.text.strip() for el in specs_list.find_all("dd")]
  44. specs_dict = dict(zip(keys, values))
  45. self.specs.update(specs_dict)
  46. def __str__(self):
  47. return f"""Title: {self.title}
  48. Author: "; ".join({self.authors})
  49. Publisher: {self.publisher}
  50. Year: {self.year}
  51. Language: {self.language}
  52. ISBN: {self.isbn}"""
  53. def __repr__(self):
  54. return self.__str__()
  55. @classmethod
  56. def find(cls, isbn):
  57. url = "https://www.bol.com/nl/rnwy/search.html"
  58. r = requests.get(url, params={"Ntt": isbn})
  59. if r.status_code == 200:
  60. soup = BeautifulSoup(r.text, 'html.parser')
  61. return Book(isbn, soup)
  62. else:
  63. return None
  64. def query(isbn):
  65. book = Book.find(isbn)
  66. if book:
  67. return book.as_record()
  68. else:
  69. raise NoDataForSelectorError(isbn)