history_task.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. """
  2. @author: luojunhui
  3. """
  4. import json
  5. import asyncio
  6. from applications.config import Config
  7. from applications.log import logging
  8. from applications.functions.pqFunctions import publishToPQ
  9. class historyContentIdTask(object):
  10. """
  11. 处理已经匹配过小程序的文章
  12. """
  13. def __init__(self, mysql_client):
  14. """
  15. :param mysql_client:
  16. """
  17. self.mysql_client = mysql_client
  18. self.article_text = Config().articleText
  19. self.article_video = Config().articleVideos
  20. self.article_crawler_video = Config().articleCrawlerVideos
  21. self.history_coroutines = Config().getConfigValue("historyArticleCoroutines")
  22. async def getTaskList(self):
  23. """
  24. 获取任务
  25. :return:
  26. """
  27. select_sql1 = f"""
  28. SELECT
  29. ART.trace_id,
  30. ART.content_id,
  31. ART.flow_pool_level,
  32. ART.gh_id,
  33. ART.process_times
  34. FROM {self.article_video} ART
  35. JOIN (
  36. select content_id, count(1) as cnt
  37. from {self.article_crawler_video}
  38. where download_status = 2
  39. group by content_id
  40. ) VID on ART.content_id = VID.content_id and VID.cnt >= 3
  41. WHERE ART.content_status = 0 and ART.process_times <= 3
  42. ORDER BY request_timestamp
  43. LIMIT {self.history_coroutines};
  44. """
  45. tasks = await self.mysql_client.asyncSelect(sql=select_sql1)
  46. task_obj_list = [
  47. {
  48. "trace_id": item[0],
  49. "content_id": item[1],
  50. "flow_pool_level": item[2],
  51. "gh_id": item[3],
  52. "process_times": item[4]
  53. } for item in tasks
  54. ]
  55. logging(
  56. code="9001",
  57. info="本次任务获取到 {} 条视频".format(len(task_obj_list)),
  58. data=task_obj_list
  59. )
  60. return task_obj_list
  61. async def getVideoList(self, content_id):
  62. """
  63. content_id
  64. :return:
  65. """
  66. sql = f"""
  67. SELECT platform, play_count, like_count, video_oss_path, cover_oss_path, user_id
  68. FROM {self.article_crawler_video}
  69. WHERE content_id = '{content_id}' and download_status = 2;
  70. """
  71. res_tuple = await self.mysql_client.asyncSelect(sql)
  72. if len(res_tuple) >= 3:
  73. return [
  74. {
  75. "platform": i[0],
  76. "play_count": i[1],
  77. "like_count": i[2],
  78. "video_oss_path": i[3],
  79. "cover_oss_path": i[4],
  80. "uid": i[5]
  81. }
  82. for i in res_tuple]
  83. else:
  84. return []
  85. async def getKimiTitle(self, content_id):
  86. """
  87. 获取 kimiTitle
  88. :param content_id:
  89. :return:
  90. """
  91. select_sql = f"""
  92. select kimi_title from {self.article_text} where content_id = '{content_id}';
  93. """
  94. res_tuple = await self.mysql_client.asyncSelect(select_sql)
  95. if res_tuple:
  96. return res_tuple[0][0]
  97. else:
  98. return False
  99. async def publishVideosToPq(self, trace_id, flow_pool_level, kimi_title, gh_id, download_videos, process_times):
  100. """
  101. 发布至 pq
  102. :param process_times:
  103. :param trace_id:
  104. :param download_videos: 已下载的视频---> list [{}, {}, {}.... ]
  105. :param gh_id: 公众号 id ---> str
  106. :param kimi_title: kimi 标题 ---> str
  107. :param flow_pool_level: 流量池层级 ---> str
  108. :return:
  109. """
  110. video_list = download_videos[:3]
  111. match flow_pool_level:
  112. case "autoArticlePoolLevel4":
  113. print("冷启层")
  114. video_list = []
  115. case "autoArticlePoolLevel3":
  116. print("暂时未知层")
  117. video_list = []
  118. case "autoArticlePoolLevel2":
  119. print("次条层")
  120. video_list = []
  121. case "autoArticlePoolLevel1":
  122. print("头条层")
  123. video_list = []
  124. L = []
  125. for video_obj in video_list:
  126. params = {
  127. "videoPath": video_obj['video_oss_path'],
  128. "uid": video_obj['uid'],
  129. "title": kimi_title
  130. }
  131. response = await publishToPQ(params)
  132. # time.sleep(2)
  133. obj = {
  134. "uid": video_obj['uid'],
  135. "source": video_obj['platform'],
  136. "kimiTitle": kimi_title,
  137. "videoId": response['data']['id'],
  138. "videoCover": response['data']['shareImgPath'],
  139. "videoPath": response['data']['videoPath'],
  140. "videoOss": video_obj['video_oss_path'].split("/")[-1]
  141. }
  142. L.append(obj)
  143. update_sql = f"""
  144. UPDATE {self.article_video}
  145. SET content_status = %s, response = %s, process_times = %s
  146. WHERE trace_id = %s;
  147. """
  148. await self.mysql_client.asyncInsert(
  149. sql=update_sql,
  150. params=(2, json.dumps(L, ensure_ascii=False), process_times + 1, trace_id)
  151. )
  152. async def processTask(self, params):
  153. """
  154. 异步执行
  155. :param params:
  156. :return:
  157. """
  158. content_id = params['content_id']
  159. trace_id = params['trace_id']
  160. flow_pool_level = params['flow_pool_level'],
  161. gh_id = params['gh_id']
  162. process_times = params['process_times']
  163. # 判断该篇文章是否存在未下架的视频,且判断是否有3条, 如果没有三条,则启动新抓取任务,后续优化点
  164. download_videos = await self.getVideoList(content_id=content_id)
  165. if download_videos:
  166. # 把状态修改为 4
  167. update_sql = f"""
  168. UPDATE {self.article_video}
  169. SET content_status = %s
  170. WHERE trace_id = %s;
  171. """
  172. await self.mysql_client.asyncInsert(
  173. sql=update_sql,
  174. params=(4, trace_id)
  175. )
  176. kimi_title = await self.getKimiTitle(content_id)
  177. if kimi_title:
  178. await self.publishVideosToPq(
  179. flow_pool_level=flow_pool_level,
  180. kimi_title=kimi_title,
  181. gh_id=gh_id,
  182. trace_id=trace_id,
  183. download_videos=download_videos,
  184. process_times=process_times
  185. )
  186. else:
  187. print("Kimi title 生成失败---后续加报警")
  188. else:
  189. pass
  190. async def deal(self):
  191. """
  192. 处理
  193. :return:
  194. """
  195. task_list = await self.getTaskList()
  196. if task_list:
  197. tasks = [self.processTask(params) for params in task_list]
  198. await asyncio.gather(*tasks)
  199. else:
  200. logging(
  201. code="9008",
  202. info="没有要处理的请求"
  203. )