process.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. """
  2. @author: luojunhui
  3. 对请求进行操作
  4. """
  5. import os
  6. import json
  7. import uuid
  8. import requests
  9. import urllib.parse
  10. from datetime import datetime, timedelta
  11. from applications.log import logging
  12. from applications.functions.ask_kimi import ask_kimi
  13. from applications.functions.calculate import title_mix
  14. from applications.functions.auto_white import auto_white
  15. def create_gzh_path(video_id, shared_uid):
  16. """
  17. :param video_id: 视频 id
  18. :param shared_uid: 分享 id
  19. """
  20. root_share_id = str(uuid.uuid4())
  21. url = f"pages/user-videos?id={video_id}&su={shared_uid}&fromGzh=1&rootShareId={root_share_id}&shareId={root_share_id}"
  22. # 自动把 root_share_id 加入到白名单
  23. auto_white(root_share_id)
  24. return root_share_id, f"pages/category?jumpPage={urllib.parse.quote(url)}"
  25. def request_for_info(video_id):
  26. """
  27. 请求数据
  28. :param video_id:
  29. :return:
  30. """
  31. url = "https://longvideoapi.piaoquantv.com/longvideoapi/openapi/video/batchSelectVideoInfo"
  32. data = {
  33. "videoIdList": [video_id]
  34. }
  35. header = {
  36. "Content-Type": "application/json",
  37. }
  38. response = requests.post(url, headers=header, data=json.dumps(data))
  39. return response.json()
  40. def choose_video(result):
  41. """
  42. :param result: 计算出来的结果
  43. :return: uid, video_id
  44. """
  45. # 判断 score
  46. score1, score2 = result['s1_score'], result['s2_score']
  47. if score1 == 0 and score2 == 0:
  48. return None, None
  49. elif score1 == 0 and score2 > 0:
  50. return result['s2_uid'], result['s2_vid']
  51. elif score1 > 0 and score2 == 0:
  52. return result['s1_uid'], result['s1_vid']
  53. elif score1 > 0 and score2 > 0:
  54. return result['s1_uid'], result['s1_vid']
  55. class ProcessParams(object):
  56. """
  57. Params Analysis
  58. """
  59. def __init__(self, t_id):
  60. self.trace_id = t_id
  61. def get_params(self, data):
  62. """
  63. "accountName": "公众号名称",
  64. "content": "文章正文",
  65. "title": "文章标题",
  66. "cover": "封面链接"
  67. :param data:
  68. :return: title
  69. """
  70. logging(
  71. code="1002",
  72. info="处理请求参数",
  73. function="get_params",
  74. trace_id=self.trace_id,
  75. data=data
  76. )
  77. return data
  78. def ask_kimi_and_save_to_local(self, title):
  79. """
  80. save file to local
  81. :param title:
  82. :return:
  83. """
  84. save_path = os.path.join(os.getcwd(), 'applications', 'static', "titles", "{}.json".format(title))
  85. if os.path.exists(save_path):
  86. logging(
  87. code="1002",
  88. info="该 video 信息已经挖掘完成---{}".format(title),
  89. function="ask_kimi_and_save_to_local",
  90. trace_id=self.trace_id,
  91. )
  92. return
  93. else:
  94. os.makedirs(os.path.dirname(save_path), exist_ok=True)
  95. if not title:
  96. result = {}
  97. else:
  98. result = ask_kimi(title)
  99. logging(
  100. code="1002",
  101. info="kimi-result",
  102. data=result,
  103. trace_id=self.trace_id,
  104. function="ask_kimi_and_save_to_local"
  105. )
  106. with open(save_path, "w", encoding="utf-8") as f:
  107. f.write(json.dumps(result, ensure_ascii=False))
  108. def process(self, data):
  109. """执行代码"""
  110. today = datetime.today()
  111. yesterday = today - timedelta(days=1)
  112. yesterday_str = yesterday.strftime("%Y%m%d")
  113. logging(
  114. code="1002",
  115. info="昨日的时间戳是---{}".format(yesterday),
  116. function="process",
  117. trace_id=self.trace_id,
  118. )
  119. params = self.get_params(data)
  120. title = params['title']
  121. account_name = params['accountName']
  122. title_p = os.path.join(os.getcwd(), 'applications', 'static', "titles", "{}.json".format(title))
  123. if os.path.exists(title_p):
  124. logging(
  125. code="1002",
  126. info="该标题已经被 kimi 处理过,跳过请求 kimi 操作--- {}".format(title),
  127. function="process",
  128. trace_id=self.trace_id
  129. )
  130. result = title_mix(title_p=title_p, dt=yesterday_str, trace_id=self.trace_id)
  131. else:
  132. self.ask_kimi_and_save_to_local(title)
  133. result = title_mix(title_p=title_p, dt=yesterday_str, trace_id=self.trace_id)
  134. uid, video_id = choose_video(result)
  135. logging(
  136. code="1002",
  137. info="best video_id --{}".format(video_id),
  138. function="process",
  139. trace_id=self.trace_id
  140. )
  141. if video_id and uid:
  142. root_share_id, productionPath = create_gzh_path(video_id=video_id, shared_uid=account_name)
  143. logging(
  144. code="1002",
  145. info="root_share_id --{}, productionPath -- {}".format(root_share_id, productionPath),
  146. function="process",
  147. trace_id=self.trace_id
  148. )
  149. response = request_for_info(video_id)
  150. productionCover = response['data'][0]['shareImgPath']
  151. productionName = response["data"][0]['title']
  152. programAvatar = "/static/logo.png"
  153. programId = "wx89e7eb06478361d7"
  154. programName = "票圈vlog"
  155. source = "Web"
  156. result = {
  157. "productionCover": productionCover,
  158. "productionName": productionName,
  159. "programAvatar": programAvatar,
  160. "programId": programId,
  161. "programName": programName,
  162. "source": source,
  163. "rootShareId": root_share_id,
  164. "productionPath": productionPath
  165. }
  166. else:
  167. result = {
  168. "productionCover": None,
  169. "productionName": None,
  170. "programAvatar": None,
  171. "programId": None,
  172. "programName": None,
  173. "source": None,
  174. "rootShareId": None,
  175. "productionPath": None
  176. }
  177. logging(
  178. code="1002",
  179. info="返回结果",
  180. function="process",
  181. data=result,
  182. trace_id=self.trace_id
  183. )
  184. return result