response.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. class Response(object):
  12. """
  13. Response
  14. """
  15. def __init__(self, params, mysql_client, config):
  16. """
  17. Response 接口
  18. """
  19. self.trace_id = None
  20. self.mini_program_type = None
  21. self.mysql_client = mysql_client
  22. self.params = params
  23. self.article_videos = config.articleVideos
  24. self.mini_map = json.loads(config.getConfigValue("miniMap"))
  25. def checkParams(self):
  26. """
  27. 请求参数校验
  28. :return:
  29. """
  30. try:
  31. self.mini_program_type = self.params['miniprogramUseType']
  32. self.trace_id = self.params['traceId']
  33. return None
  34. except Exception as e:
  35. return {
  36. "error": "params error",
  37. "msg": str(e),
  38. "info": self.params
  39. }
  40. async def getVideosResult(self):
  41. """
  42. 获取结果
  43. :return:
  44. """
  45. select_sql = f"""
  46. SELECT gh_id, content_status, response, process_times
  47. FROM {self.article_videos}
  48. WHERE trace_id = '{self.trace_id}';
  49. """
  50. info_tuple = await self.mysql_client.asyncSelect(select_sql)
  51. gh_id, content_status, response, process_times = info_tuple[0]
  52. return {
  53. "ghId": gh_id,
  54. "contentStatus": content_status,
  55. "response": json.loads(response),
  56. "processTimes": process_times
  57. }
  58. def createGzhPath(self, video_id, shared_uid, gh_id):
  59. """
  60. :param gh_id: 公众号账号的gh_id
  61. :param video_id: 视频 id
  62. :param shared_uid: 分享 id
  63. """
  64. def generate_source_id():
  65. """
  66. generate_source_id
  67. :return:
  68. """
  69. timestamp = str(int(time.time() * 1000))
  70. random_str = str(random.randint(1000, 9999))
  71. hash_input = f"{timestamp}-{random_str}"
  72. return hashlib.md5(hash_input.encode()).hexdigest()
  73. root_share_id = str(uuid.uuid4())
  74. if self.mini_program_type == 2:
  75. source_id = (
  76. "touliu_tencentGzhArticle_{}_".format(gh_id) + generate_source_id()
  77. )
  78. elif self.mini_program_type == 1:
  79. source_id = "longArticles_" + generate_source_id()
  80. elif self.mini_program_type == 3:
  81. source_id = "WeCom_" + generate_source_id()
  82. else:
  83. source_id = "Error mini_program_type {}".format(self.mini_program_type)
  84. url = f"pages/user-videos?id={video_id}&su={shared_uid}&fromGzh=1&rootShareId={root_share_id}&shareId={root_share_id}&rootSourceId={source_id}"
  85. # 自动把 root_share_id 加入到白名单
  86. # auto_white(root_share_id)
  87. return (
  88. root_share_id,
  89. source_id,
  90. f"pages/category?jumpPage={urllib.parse.quote(url, safe='')}",
  91. )
  92. async def generateCard(self, index, gh_id, mini_id, item):
  93. """
  94. 生成单个分享卡片
  95. :param item: 单个视频结果
  96. :param mini_id: 小程序 appType
  97. :param gh_id: 公众号 id
  98. :param index: 视频位置
  99. :return:
  100. """
  101. str_mini_id = str(mini_id)
  102. mini_info = self.mini_map[str_mini_id]
  103. avatar, app_id, app_name = mini_info['avatar'], mini_info['id'], mini_info['name']
  104. root_share_id, root_source_id, production_path = self.createGzhPath(
  105. video_id=item['videoId'],
  106. shared_uid=item['uid'],
  107. gh_id=gh_id
  108. )
  109. logging(
  110. code="1002",
  111. info="root_share_id --{}, productionPath -- {}".format(
  112. root_share_id, production_path
  113. ),
  114. function="process",
  115. trace_id=self.trace_id,
  116. )
  117. result = {
  118. "productionCover": item['videoCover'],
  119. "productionName": item['kimiTitle'],
  120. "programAvatar": avatar,
  121. "programId": app_id,
  122. "programName": app_name,
  123. "source": item['source'],
  124. "rootShareId": root_share_id,
  125. "productionPath": production_path,
  126. "videoUrl": item['videoPath'],
  127. "mini_id": mini_id,
  128. "paragraphPosition": index * 0.25
  129. }
  130. if index == 1:
  131. result['paragraphPosition'] = 0.01
  132. item['rootSourceId'] = root_source_id
  133. return result, item
  134. async def generateCards(self, result):
  135. """
  136. 生成返回卡片
  137. :return:
  138. """
  139. gh_id = result['ghId']
  140. response = result['response']
  141. match self.mini_program_type:
  142. case 1:
  143. L = []
  144. new_item_list = []
  145. for index, item in enumerate(response, 1):
  146. random_num = random.randint(1, 10)
  147. if random_num in [1, 2, 3, 4, 5, 6]:
  148. mini_id = 25
  149. elif random_num in [7, 8]:
  150. mini_id = 29
  151. else:
  152. mini_id = 31
  153. card, new_item = await self.generateCard(index, gh_id, mini_id, item)
  154. L.append(card)
  155. new_item_list.append(new_item)
  156. return L, new_item_list
  157. case 2:
  158. L = []
  159. new_item_list = []
  160. for index, item in enumerate(response, 1):
  161. card, new_item = await self.generateCard(index, gh_id, 33, item)
  162. L.append(card)
  163. new_item_list.append(new_item)
  164. return L, new_item_list
  165. case 3:
  166. L = []
  167. new_item_list = []
  168. for index, item in enumerate(response, 1):
  169. card, new_item = await self.generateCard(index, gh_id, 27, item)
  170. L.append(card)
  171. new_item_list.append(card)
  172. return L, new_item_list
  173. async def job(self):
  174. """
  175. 执行方法
  176. :return:
  177. """
  178. response = await self.getVideosResult()
  179. status_code = response.get('contentStatus')
  180. process_times = response.get('processTimes')
  181. match status_code:
  182. case 0:
  183. if process_times > 3:
  184. result = {
  185. "traceId": self.trace_id,
  186. "code": 0,
  187. "error": "匹配失败,处理超过 3 次"
  188. }
  189. else:
  190. result = {
  191. "traceId": self.trace_id,
  192. "code": 0,
  193. "Message": "该请求还没处理"
  194. }
  195. return result
  196. case 1:
  197. return {
  198. "traceId": self.trace_id,
  199. "code": 1,
  200. "Message": "该请求正在处理中"
  201. }
  202. case 2:
  203. card_list, new_items = await self.generateCards(result=response)
  204. update_sql = f"""
  205. UPDATE {self.article_videos}
  206. SET response = %s, success_status = %s
  207. WHERE trace_id = %s;
  208. """
  209. await self.mysql_client.asyncInsert(
  210. sql=update_sql,
  211. params=(json.dumps(new_items, ensure_ascii=False), 1, self.trace_id)
  212. )
  213. return {"traceId": self.trace_id, "miniprogramList": card_list}
  214. case 3:
  215. return {
  216. "traceId": self.trace_id,
  217. "code": 3,
  218. "error": "匹配失败,超过三次"
  219. }
  220. async def deal(self):
  221. """
  222. api process starts from here
  223. :return:
  224. """
  225. params_error = self.checkParams()
  226. if params_error:
  227. return params_error
  228. else:
  229. return await self.job()