autoadder.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import json
  2. import base64
  3. import requests
  4. import ddgs
  5. import typing
  6. from .validator import barcode_validator
  7. from .exception import bad_request_exception
  8. from .exception import autoadder_exception
  9. from .gemini_search import gemini_search
  10. class autoadder:
  11. def __init__(self, apikey: str, engine: str) -> None:
  12. self.__apikey = apikey
  13. self.__engine = engine
  14. self.__gemini = gemini_search(apikey)
  15. def __ddg(self) -> ddgs.DDGS:
  16. return ddgs.DDGS()
  17. def __search(self, phrase: str) -> str:
  18. for count in self.__ddg().text(phrase, max_results = 1):
  19. return count["title"]
  20. return phrase
  21. def __search_images(self, phrase: str) -> typing.Iterable[str]:
  22. for count in self.__ddg().images(phrase, max_results = 10):
  23. yield count["image"]
  24. yield phrase
  25. def __check_barcode(self, barcode: str) -> None:
  26. if barcode_validator(barcode).invalid:
  27. raise bad_request_exception("Invalid barcode")
  28. def __image_request(self, source: str) -> [str, str]:
  29. request = requests.get(source, timeout = 3)
  30. if not request.ok or not "Content-Type" in request.headers:
  31. raise bad_request_exception("Can nor fetch image")
  32. image = request.content
  33. encoded = base64.b64encode(image)
  34. extension = request.headers["Content-Type"]
  35. return encoded, extension
  36. def __try_images(self, images: list) -> [str, str]:
  37. for count in images:
  38. try:
  39. return self.__image_request(count)
  40. except:
  41. continue
  42. def __gemini_find(self, barcode: str) -> dict:
  43. result = self.__gemini.search(barcode)
  44. title = result["title"]
  45. description = result["description"]
  46. author = result["author"]
  47. images_url = self.__search_images(barcode)
  48. image, image_type = self.__try_images(images_url)
  49. splited = title.split(" ")
  50. if len(splited) > 3:
  51. title = " ".join(splited[0:3])
  52. return {
  53. "title": title,
  54. "description": description,
  55. "author": author,
  56. "barcode": barcode,
  57. "image": image,
  58. "image_type": image_type,
  59. "from": "gemini"
  60. }
  61. def __duck_duck_find(self, barcode: str) -> dict:
  62. self.__check_barcode(barcode)
  63. title = self.__search(barcode)
  64. description = title
  65. author = "Somebody"
  66. image = ""
  67. image_type = ""
  68. images_url = self.__search_images(barcode)
  69. image, image_type = self.__try_images(images_url)
  70. splited = title.split(" ")
  71. if len(splited) > 3:
  72. title = " ".join(splited[0:3])
  73. return {
  74. "title": title,
  75. "description": description,
  76. "author": author,
  77. "barcode": barcode,
  78. "image": image,
  79. "image_type": image_type,
  80. "from": "duckduckgo"
  81. }
  82. def find(self, barcode: str) -> dict:
  83. try:
  84. return self.__gemini_find(barcode)
  85. except Exception as error:
  86. duck_duck_go = self.__duck_duck_find(barcode)
  87. duck_duck_go["cause"] = str(error)
  88. return duck_duck_go