response.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 static.config import db_article
  12. from applications.functions.common import request_for_info
  13. class Response(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. self.mini_map = {
  22. 25: {
  23. "avatar": "https://rescdn.yishihui.com/0temp/ttmhzfsh.png",
  24. "id": "wx0b7d95eb293b783b",
  25. "name": "天天美好祝福生活",
  26. "index": 25
  27. },
  28. 29: {
  29. "avatar": "https://rescdn.yishihui.com/0temp/cyfyld.png",
  30. "id": "wx65c76bb4c67934db",
  31. "name": "财运福运来到",
  32. "index": 29
  33. },
  34. 31: {
  35. "avatar": "https://rescdn.yishihui.com/0temp/mhzfshxf2.png",
  36. "id": "wx2e4478b1641b3b15",
  37. "name": "美好祝福生活幸福",
  38. "index": 31
  39. },
  40. 36: {
  41. "avatar": "https://rescdn.yishihui.com/0temp/zfyfyc.jpeg",
  42. "id": "wxcddf231abd0dabdc",
  43. "name": "祝福有福有财",
  44. "index": 36
  45. },
  46. 27: {
  47. "avatar": "https://rescdn.yishihui.com/0temp/xymhfqdd.png",
  48. "id": "wx7187c217efef24a7",
  49. "name": "幸运美好福气多多",
  50. "index": 27
  51. }
  52. }
  53. async def get_result(self):
  54. """
  55. 获取结果
  56. :return:
  57. """
  58. select_sql = f"""
  59. SELECT gh_id, recall_video_id1, recall_video_id2, recall_video_id3, kimi_title, content_status, process_times
  60. FROM {db_article}
  61. WHERE trace_id = '{self.trace_id}';
  62. """
  63. info_tuple = await self.mysql_client.async_select(select_sql)
  64. gh_id, vid1, vid2, vid3, kimi_title, content_status, process_times = info_tuple[0]
  65. response = {
  66. "gh_id": gh_id,
  67. "vid_list": [vid1, vid2, vid3],
  68. "kimi_title": kimi_title,
  69. "content_status": content_status,
  70. "process_times": process_times
  71. }
  72. print(response)
  73. return response
  74. def create_gzh_path(self, video_id, shared_uid, gh_id):
  75. """
  76. :param gh_id: 公众号账号的gh_id
  77. :param video_id: 视频 id
  78. :param shared_uid: 分享 id
  79. """
  80. def generate_source_id():
  81. """
  82. generate_source_id
  83. :return:
  84. """
  85. timestamp = str(int(time.time() * 1000))
  86. random_str = str(random.randint(1000, 9999))
  87. hash_input = f"{timestamp}-{random_str}"
  88. return hashlib.md5(hash_input.encode()).hexdigest()
  89. root_share_id = str(uuid.uuid4())
  90. if self.mini_program_type == 2:
  91. source_id = "touliu_tencentGzhArticle_{}_".format(gh_id) + generate_source_id()
  92. elif self.mini_program_type == 1:
  93. source_id = "longArticles_" + generate_source_id()
  94. elif self.mini_program_type == 3:
  95. source_id = "WeCom_" + generate_source_id()
  96. else:
  97. source_id = "Error mini_program_type {}".format(self.mini_program_type)
  98. url = f"pages/user-videos?id={video_id}&su={shared_uid}&fromGzh=1&rootShareId={root_share_id}&shareId={root_share_id}&rootSourceId={source_id}"
  99. # 自动把 root_share_id 加入到白名单
  100. # auto_white(root_share_id)
  101. return root_share_id, source_id, f"pages/category?jumpPage={urllib.parse.quote(url, safe='')}"
  102. def choose_mini_program(self, mini_id):
  103. """
  104. 获取小程序分享卡片
  105. :return:
  106. """
  107. mini_info = self.mini_map[mini_id]
  108. return mini_info['avatar'], mini_info['id'], mini_info['name'], mini_info['index']
  109. async def generate_card(self, video_id, kimi_title, index, gh_id, mini_id):
  110. """
  111. 生成分享卡片
  112. :return:
  113. """
  114. response = request_for_info(video_id)
  115. productionCover = response['data'][0]['shareImgPath']
  116. productionName = kimi_title
  117. videoUrl = response['data'][0]['videoPath']
  118. user_id = response['data'][0]['user']['uid']
  119. programAvatar, programId, programName, pqMiniId = self.choose_mini_program(mini_id)
  120. root_share_id, source_id, productionPath = self.create_gzh_path(video_id, user_id, gh_id)
  121. source = "Web"
  122. logging(
  123. code="1002",
  124. info="root_share_id --{}, productionPath -- {}".format(root_share_id, productionPath),
  125. function="process",
  126. trace_id=self.trace_id
  127. )
  128. result = {
  129. "productionCover": productionCover,
  130. "productionName": productionName,
  131. "programAvatar": programAvatar,
  132. "programId": programId,
  133. "programName": programName,
  134. "source": source,
  135. "rootShareId": root_share_id,
  136. "productionPath": productionPath,
  137. "videoUrl": videoUrl,
  138. "mini_id": mini_id,
  139. "paragraphPosition": index * 0.25
  140. }
  141. update_result_sql = f"""
  142. UPDATE {db_article}
  143. SET
  144. result{index} = %s,
  145. success = %s
  146. WHERE
  147. trace_id = %s;
  148. """
  149. await self.mysql_client.async_insert(
  150. sql=update_result_sql,
  151. params=(json.dumps(result, ensure_ascii=False), 1, self.trace_id)
  152. )
  153. logging(
  154. code="2000",
  155. info="统计 root_share_id && video_id",
  156. function="process",
  157. trace_id=self.trace_id,
  158. data={
  159. "rootShareId": root_share_id,
  160. "videoId": video_id,
  161. "sourceId": source_id
  162. }
  163. )
  164. return result
  165. async def generate_cards(self, video_list, kimi_title, gh_id):
  166. """
  167. 生成一组卡片
  168. :param video_list:
  169. :param kimi_title:
  170. :param gh_id:
  171. :return:
  172. """
  173. L = []
  174. if self.mini_program_type == 1:
  175. mini_id_list = [25, 29, 31]
  176. video_count = len(video_list)
  177. mini_choice_index = random.sample(range(3), video_count)
  178. mini_choice_item = [mini_id_list[i] for i in mini_choice_index]
  179. for index, video_id in enumerate(video_list, 1):
  180. result = await self.generate_card(video_id=video_id, kimi_title=kimi_title, index=index, gh_id=gh_id,
  181. mini_id=mini_choice_item[index - 1])
  182. L.append(result)
  183. elif self.mini_program_type == 2:
  184. for index, video_id in enumerate(video_list, 1):
  185. result = await self.generate_card(video_id=video_id, kimi_title=kimi_title, index=index, gh_id=gh_id,
  186. mini_id=36)
  187. L.append(result)
  188. elif self.mini_program_type == 3:
  189. for index, video_id in enumerate(video_list, 1):
  190. result = await self.generate_card(video_id=video_id, kimi_title=kimi_title, index=index, gh_id=gh_id,
  191. mini_id=27)
  192. L.append(result)
  193. return L
  194. async def deal(self):
  195. """
  196. Recall Deal
  197. :return:
  198. """
  199. response = await self.get_result()
  200. status_code = response.get("content_status")
  201. process_times = response.get("process_times")
  202. if status_code == 0:
  203. if process_times > 3:
  204. result = {
  205. "traceId": self.trace_id,
  206. "code": 0,
  207. "error": "匹配失败,处理超过五次,文章敏感"
  208. }
  209. else:
  210. result = {
  211. "traceId": self.trace_id,
  212. "code": 0,
  213. "Message": "该请求还没处理"
  214. }
  215. elif status_code == 1:
  216. result = {
  217. "traceId": self.trace_id,
  218. "code": 1,
  219. "Message": "该请求正在处理中"
  220. }
  221. elif status_code == 2:
  222. unEmptyList = [i for i in response['vid_list'] if i]
  223. cards = await self.generate_cards(unEmptyList, response.get("kimi_title"), response['gh_id'])
  224. if cards:
  225. result = {
  226. "traceId": self.trace_id,
  227. "miniprogramList": cards
  228. }
  229. else:
  230. result = {
  231. "traceId": self.trace_id,
  232. "code": 0,
  233. "error": "匹配失败,视频下载失败返回vid为0"
  234. }
  235. elif status_code == 3:
  236. result = {
  237. "traceId": self.trace_id,
  238. "code": 0,
  239. "error": "匹配失败,处理超过五次,文章敏感"
  240. }
  241. else:
  242. result = {
  243. "traceId": self.trace_id,
  244. "Message": "UnKnow Error"
  245. }
  246. logging(
  247. code="1002",
  248. info="返回结果",
  249. function="process",
  250. data=result,
  251. trace_id=self.trace_id
  252. )
  253. return result