Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

97 lines
3.5KB

  1. import requests
  2. from config import conf
  3. from common.log import logger
  4. import os
  5. import html
  6. class LinkSummary:
  7. def __init__(self):
  8. pass
  9. def summary_file(self, file_path: str):
  10. file_body = {
  11. "file": open(file_path, "rb"),
  12. "name": file_path.split("/")[-1],
  13. }
  14. url = self.base_url() + "/v1/summary/file"
  15. res = requests.post(url, headers=self.headers(), files=file_body, timeout=(5, 300))
  16. return self._parse_summary_res(res)
  17. def summary_url(self, url: str):
  18. url = html.unescape(url)
  19. body = {
  20. "url": url
  21. }
  22. res = requests.post(url=self.base_url() + "/v1/summary/url", headers=self.headers(), json=body, timeout=(5, 180))
  23. return self._parse_summary_res(res)
  24. def summary_chat(self, summary_id: str):
  25. body = {
  26. "summary_id": summary_id
  27. }
  28. res = requests.post(url=self.base_url() + "/v1/summary/chat", headers=self.headers(), json=body, timeout=(5, 180))
  29. if res.status_code == 200:
  30. res = res.json()
  31. logger.debug(f"[LinkSum] chat open, res={res}")
  32. if res.get("code") == 200:
  33. data = res.get("data")
  34. return {
  35. "questions": data.get("questions"),
  36. "file_id": data.get("file_id")
  37. }
  38. else:
  39. res_json = res.json()
  40. logger.error(f"[LinkSum] summary error, status_code={res.status_code}, msg={res_json.get('message')}")
  41. return None
  42. def _parse_summary_res(self, res):
  43. if res.status_code == 200:
  44. res = res.json()
  45. logger.debug(f"[LinkSum] url summary, res={res}")
  46. if res.get("code") == 200:
  47. data = res.get("data")
  48. return {
  49. "summary": data.get("summary"),
  50. "summary_id": data.get("summary_id")
  51. }
  52. else:
  53. res_json = res.json()
  54. logger.error(f"[LinkSum] summary error, status_code={res.status_code}, msg={res_json.get('message')}")
  55. return None
  56. def base_url(self):
  57. return conf().get("linkai_api_base", "https://api.link-ai.tech")
  58. def headers(self):
  59. return {"Authorization": "Bearer " + conf().get("linkai_api_key")}
  60. def check_file(self, file_path: str, sum_config: dict) -> bool:
  61. file_size = os.path.getsize(file_path) // 1000
  62. if (sum_config.get("max_file_size") and file_size > sum_config.get("max_file_size")) or file_size > 15000:
  63. logger.warn(f"[LinkSum] file size exceeds limit, No processing, file_size={file_size}KB")
  64. return False
  65. suffix = file_path.split(".")[-1]
  66. support_list = ["txt", "csv", "docx", "pdf", "md", "jpg", "jpeg", "png"]
  67. if suffix not in support_list:
  68. logger.warn(f"[LinkSum] unsupported file, suffix={suffix}, support_list={support_list}")
  69. return False
  70. return True
  71. def check_url(self, url: str):
  72. if not url:
  73. return False
  74. support_list = ["http://mp.weixin.qq.com", "https://mp.weixin.qq.com"]
  75. black_support_list = ["https://mp.weixin.qq.com/mp/waerrpage"]
  76. for black_url_prefix in black_support_list:
  77. if url.strip().startswith(black_url_prefix):
  78. logger.warn(f"[LinkSum] unsupported url, no need to process, url={url}")
  79. return False
  80. for support_url in support_list:
  81. if url.strip().startswith(support_url):
  82. return True
  83. return False