history_task.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. logging(
  153. code="9002",
  154. info="已经从历史文章更新",
  155. trace_id=trace_id
  156. )
  157. async def processTask(self, params):
  158. """
  159. 异步执行
  160. :param params:
  161. :return:
  162. """
  163. content_id = params['content_id']
  164. trace_id = params['trace_id']
  165. flow_pool_level = params['flow_pool_level'],
  166. gh_id = params['gh_id']
  167. process_times = params['process_times']
  168. # 判断该篇文章是否存在未下架的视频,且判断是否有3条, 如果没有三条,则启动新抓取任务,后续优化点
  169. download_videos = await self.getVideoList(content_id=content_id)
  170. if download_videos:
  171. # 把状态修改为 4
  172. update_sql = f"""
  173. UPDATE {self.article_video}
  174. SET content_status = %s
  175. WHERE trace_id = %s;
  176. """
  177. await self.mysql_client.asyncInsert(
  178. sql=update_sql,
  179. params=(4, trace_id)
  180. )
  181. kimi_title = await self.getKimiTitle(content_id)
  182. if kimi_title:
  183. await self.publishVideosToPq(
  184. flow_pool_level=flow_pool_level,
  185. kimi_title=kimi_title,
  186. gh_id=gh_id,
  187. trace_id=trace_id,
  188. download_videos=download_videos,
  189. process_times=process_times
  190. )
  191. else:
  192. print("Kimi title 生成失败---后续加报警")
  193. else:
  194. pass
  195. async def deal(self):
  196. """
  197. 处理
  198. :return:
  199. """
  200. task_list = await self.getTaskList()
  201. logging(
  202. code="5002",
  203. info="History content_task Task Got {} this time".format(len(task_list)),
  204. function="History Contents Task"
  205. )
  206. if task_list:
  207. tasks = [self.processTask(params) for params in task_list]
  208. await asyncio.gather(*tasks)
  209. else:
  210. print("暂时未获得历史已存在文章")