process.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. """
  2. @author: luojunhui
  3. 对请求进行操作
  4. """
  5. import os
  6. import json
  7. import uuid
  8. import requests
  9. import urllib.parse
  10. from applications.functions.ask_kimi import ask_kimi
  11. from applications.functions.calculate import title_mix
  12. class ProcessParams(object):
  13. """
  14. Params Analysis
  15. """
  16. @classmethod
  17. def get_params(cls, data):
  18. """
  19. "accountName": "公众号名称",
  20. "content": "文章正文",
  21. "title": "文章标题",
  22. "cover": "封面链接"
  23. :param data:
  24. :return: title
  25. """
  26. return data['title']
  27. @classmethod
  28. def ask_kimi_and_save_to_local(cls, title):
  29. """
  30. save file to local
  31. :param title:
  32. :return:
  33. """
  34. save_path = os.path.join(os.getcwd(), 'applications', 'static', "titles", "{}.json".format(title))
  35. if os.path.exists(save_path):
  36. return
  37. else:
  38. os.makedirs(os.path.dirname(save_path), exist_ok=True)
  39. if not title:
  40. result = "{}"
  41. else:
  42. result = ask_kimi(title)
  43. with open(save_path, "w", encoding="utf-8") as f:
  44. f.write(json.dumps(result, ensure_ascii=False))
  45. @classmethod
  46. def create_gzh_path(cls, video_id, shared_uid):
  47. """
  48. :param video_id: 视频 id
  49. :param shared_uid: 分享 id
  50. """
  51. root_share_id = str(uuid.uuid4())
  52. url = f"pages/user-videos?id={video_id}&su={shared_uid}&fromGzh=1&rootShareId={root_share_id}&shareId={root_share_id}"
  53. return root_share_id, f"pages/category?jumpPage={urllib.parse.quote(url)}"
  54. @classmethod
  55. def request_for_info(cls, video_id):
  56. """
  57. 请求数据
  58. :param video_id:
  59. :return:
  60. """
  61. url = "https://longvideoapi.piaoquantv.com/longvideoapi/openapi/video/batchSelectVideoInfo"
  62. data = {
  63. "videoIdList": [video_id]
  64. }
  65. header = {
  66. "Content-Type": "application/json",
  67. }
  68. response = requests.post(url, headers=header, data=json.dumps(data))
  69. return response.json()
  70. @classmethod
  71. def process(cls, data):
  72. """执行代码"""
  73. title = cls.get_params(data)
  74. title_p = os.path.join(os.getcwd(), 'applications', 'static', "titles", "{}.json".format(title))
  75. if os.path.exists(title_p):
  76. result = title_mix(title_p=title_p, dt="20240417")
  77. else:
  78. cls.ask_kimi_and_save_to_local(title)
  79. result = title_mix(title_p=title_p, dt="20240417")
  80. uid, video_id = result['s1_uid'], result['s1_vid']
  81. root_share_id, productionPath = cls.create_gzh_path(uid, video_id)
  82. response = cls.request_for_info(video_id)
  83. productionCover = response["data"][0]['coverImg']['coverImgPath']
  84. productionName = response["data"][0]['title']
  85. programAvatar = "Avatar"
  86. programId = "wx89e7eb06478361d7"
  87. programName = "票圈vlog"
  88. source = "Web"
  89. result = {
  90. "productionCover": productionCover,
  91. "productionName": productionName,
  92. "programAvatar": programAvatar,
  93. "programId": programId,
  94. "programName": programName,
  95. "source": source,
  96. "rootShareId": root_share_id,
  97. "productionPath": productionPath
  98. }
  99. return result