history_task.py 7.0 KB

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