recall_deal.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. """
  2. @author: luojunhui
  3. """
  4. import json
  5. import uuid
  6. import time
  7. import random
  8. import hashlib
  9. import urllib.parse
  10. from applications.functions.log import logging
  11. from applications.static.config import db_article
  12. from applications.functions.common import request_for_info
  13. class RecallDeal(object):
  14. """
  15. 召回逻辑
  16. """
  17. def __init__(self, trace_id, mysql_client, mini_program_type):
  18. self.trace_id = trace_id
  19. self.mysql_client = mysql_client
  20. self.mini_program_type = mini_program_type
  21. async def get_result(self):
  22. """
  23. 获取结果
  24. :return:
  25. """
  26. select_sql = f"""
  27. SELECT gh_id, recall_video_id1, recall_video_id2, recall_video_id3, kimi_title, content_status, process_times
  28. FROM {db_article}
  29. WHERE trace_id = '{self.trace_id}';
  30. """
  31. info_tuple = await self.mysql_client.async_select(select_sql)
  32. gh_id, vid1, vid2, vid3, kimi_title, content_status, process_times = info_tuple[0]
  33. response = {
  34. "gh_id": gh_id,
  35. "vid_list": [vid1, vid2, vid3],
  36. "kimi_title": kimi_title,
  37. "content_status": content_status,
  38. "process_times": process_times
  39. }
  40. return response
  41. def create_gzh_path(self, video_id, shared_uid):
  42. """
  43. :param video_id: 视频 id
  44. :param shared_uid: 分享 id
  45. """
  46. def generate_source_id():
  47. """
  48. generate_source_id
  49. :return:
  50. """
  51. timestamp = str(int(time.time() * 1000))
  52. random_str = str(random.randint(1000, 9999))
  53. hash_input = f"{timestamp}-{random_str}"
  54. return hashlib.md5(hash_input.encode()).hexdigest()
  55. root_share_id = str(uuid.uuid4())
  56. if self.mini_program_type == 1:
  57. source_id = "touliu_tencentGzhArticle_" + generate_source_id()
  58. elif self.mini_program_type == 2:
  59. source_id = "longArticles_" + generate_source_id()
  60. else:
  61. source_id = "Error mini_program_type {}".format(self.mini_program_type)
  62. url = f"pages/user-videos?id={video_id}&su={shared_uid}&fromGzh=1&rootShareId={root_share_id}&shareId={root_share_id}&rootSourceId={source_id}"
  63. # 自动把 root_share_id 加入到白名单
  64. # auto_white(root_share_id)
  65. return root_share_id, source_id, f"pages/category?jumpPage={urllib.parse.quote(url, safe='')}"
  66. def choose_mini_program(self):
  67. """
  68. 获取小程序分享卡片
  69. :return:
  70. """
  71. if self.mini_program_type == 1:
  72. # 正常长文业务
  73. programAvatar = "https://rescdn.yishihui.com/0temp/ssyqsh.png"
  74. programId = "wx59d9e2c05f00f880"
  75. programName = "刷刷有趣生活"
  76. elif self.mini_program_type == 2:
  77. # 投流业务
  78. programAvatar = "https://rescdn.yishihui.com/0temp/zfyfyc.jpeg"
  79. programId = "wxcddf231abd0dabdc"
  80. programName = "祝福有福有财"
  81. else:
  82. programAvatar = "https://rescdn.yishihui.com/0temp/ssyqsh.png"
  83. programId = "wx59d9e2c05f00f880"
  84. programName = "刷刷有趣生活"
  85. return programAvatar, programId, programName
  86. async def generate_card(self, video_id, kimi_title, index):
  87. """
  88. 生成分享卡片
  89. :return:
  90. """
  91. response = request_for_info(video_id)
  92. productionCover = response['data'][0]['shareImgPath']
  93. productionName = kimi_title
  94. videoUrl = response['data'][0]['videoPath']
  95. user_id = response['data'][0]['user']['uid']
  96. programAvatar, programId, programName = self.choose_mini_program()
  97. root_share_id, source_id, productionPath = self.create_gzh_path(video_id, user_id)
  98. source = "Web"
  99. logging(
  100. code="1002",
  101. info="root_share_id --{}, productionPath -- {}".format(root_share_id, productionPath),
  102. function="process",
  103. trace_id=self.trace_id
  104. )
  105. result = {
  106. "productionCover": productionCover,
  107. "productionName": productionName,
  108. "programAvatar": programAvatar,
  109. "programId": programId,
  110. "programName": programName,
  111. "source": source,
  112. "rootShareId": root_share_id,
  113. "productionPath": productionPath,
  114. "videoUrl": videoUrl,
  115. "paragraphPosition": index * 0.25
  116. }
  117. update_result_sql = f"""
  118. UPDATE {db_article}
  119. SET
  120. result{index} = %s,
  121. success = %s
  122. WHERE
  123. trace_id = %s;
  124. """
  125. await self.mysql_client.async_insert(
  126. sql=update_result_sql,
  127. params=(json.dumps(result, ensure_ascii=False), 1, self.trace_id)
  128. )
  129. logging(
  130. code="2000",
  131. info="统计 root_share_id && video_id",
  132. function="process",
  133. trace_id=self.trace_id,
  134. data={
  135. "rootShareId": root_share_id,
  136. "videoId": video_id,
  137. "sourceId": source_id
  138. }
  139. )
  140. return result
  141. async def deal(self):
  142. """
  143. Recall Deal
  144. :return:
  145. """
  146. response = await self.get_result()
  147. status_code = response.get("content_status")
  148. process_times = response.get("process_times")
  149. if status_code == 0:
  150. if process_times > 5:
  151. result = {
  152. "traceId": self.trace_id,
  153. "code": 0,
  154. "error": "匹配失败,处理超过五次,文章敏感"
  155. }
  156. else:
  157. result = {
  158. "traceId": self.trace_id,
  159. "code": 0,
  160. "Message": "该请求还没处理"
  161. }
  162. elif status_code == 1:
  163. result = {
  164. "traceId": self.trace_id,
  165. "code": 1,
  166. "Message": "该请求正在处理中"
  167. }
  168. elif status_code == 2:
  169. L = []
  170. unEmptyList = [i for i in response['vid_list'] if i]
  171. for index, best_video_id in enumerate(unEmptyList, 1):
  172. card = await self.generate_card(best_video_id, response.get("kimi_title"), index)
  173. L.append(card)
  174. if L:
  175. result = {
  176. "traceId": self.trace_id,
  177. "miniprogramList": L
  178. }
  179. else:
  180. result = {
  181. "traceId": self.trace_id,
  182. "code": 0,
  183. "error": "匹配失败,视频下载失败返回vid为0"
  184. }
  185. elif status_code == 3:
  186. result = {
  187. "traceId": self.trace_id,
  188. "code": 0,
  189. "error": "匹配失败,处理超过五次,文章敏感"
  190. }
  191. else:
  192. result = {
  193. "traceId": self.trace_id,
  194. "Message": "UnKnow Error"
  195. }
  196. logging(
  197. code="1002",
  198. info="返回结果",
  199. function="process",
  200. data=result,
  201. trace_id=self.trace_id
  202. )
  203. return result