get_off_videos.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import time
  2. import traceback
  3. from typing import List, Optional
  4. from tqdm import tqdm
  5. from applications.api import change_video_audit_status
  6. from applications.api import fetch_piaoquan_video_list_detail
  7. from applications.api import feishu_robot
  8. class GetOffVideosConst:
  9. EXPIRE_TIME = 3 * 24 * 3600
  10. EARLIEST_TIME = 7 * 24 * 3600
  11. VIDEO_AVAILABLE_STATUS = 1
  12. VIDEO_DISABLE_STATUS = 0
  13. # VIDEO_AUDIT
  14. VIDEO_AUDIT_FAIL_STATUS = 2
  15. VIDEO_AUDIT_SUCCESS_STATUS = 5
  16. # Task code
  17. TASK_SUCCESS_STATUS = 2
  18. TASK_FAILED_STATUS = 99
  19. # check status
  20. CHECK_INIT_STATUS = 0
  21. CHECK_FINISHED_STATUS = 1
  22. # table
  23. table = "get_off_videos"
  24. class GetOffVideos(GetOffVideosConst):
  25. def __init__(self, db_client, log_client):
  26. self.db_client = db_client
  27. self.log_client = log_client
  28. async def get_task_list(
  29. self, earliest_timestamp_threshold: int, expire_timestamp_threshold: int
  30. ):
  31. """get videos which need get off"""
  32. query = f"""
  33. select video_id from {self.table} where video_status = %s and publish_time between %s and %s;
  34. """
  35. video_list, error = await self.db_client.async_fetch(
  36. query,
  37. params=(
  38. self.VIDEO_AVAILABLE_STATUS,
  39. earliest_timestamp_threshold,
  40. expire_timestamp_threshold,
  41. ),
  42. )
  43. return video_list
  44. async def update_video_status(self, video_id):
  45. query = f"""
  46. update {self.table} set video_status = %s, get_off_time = %s where video_id = %s;
  47. """
  48. return await self.db_client.async_save(
  49. query, params=(self.VIDEO_DISABLE_STATUS, int(time.time()), video_id)
  50. )
  51. async def update_video_audit_status(self, video_id):
  52. """use pq api to update video status"""
  53. response = await change_video_audit_status(
  54. video_id, self.VIDEO_AUDIT_FAIL_STATUS
  55. )
  56. await self.update_video_status(video_id)
  57. return response
  58. async def get_off_job(self):
  59. """get off videos out of expire time"""
  60. expire_timestamp_threshold = int(time.time()) - self.EXPIRE_TIME
  61. earliest_timestamp_threshold = int(time.time()) - self.EARLIEST_TIME
  62. task_list = await self.get_task_list(
  63. earliest_timestamp_threshold, expire_timestamp_threshold
  64. )
  65. success_count = 0
  66. failed_count = 0
  67. for task in tqdm(task_list):
  68. video_id = task["video_id"]
  69. try:
  70. await self.update_video_audit_status(video_id)
  71. success_count += 1
  72. except Exception as e:
  73. await self.log_client.log(
  74. contents={
  75. "task": "get_off_videos",
  76. "function": "get_off_job",
  77. "status": "fail",
  78. "message": "get off video fail",
  79. "data": {
  80. "video_id": video_id,
  81. "error": str(e),
  82. "traceback": traceback.format_exc(),
  83. },
  84. }
  85. )
  86. failed_count += 1
  87. if success_count or failed_count:
  88. await feishu_robot.bot(
  89. title="自动下架任务完成",
  90. detail={
  91. "成功下架视频数量": success_count,
  92. "失败数量": failed_count,
  93. }
  94. )
  95. async def check(self):
  96. earliest_timestamp = int(time.time()) - self.EARLIEST_TIME
  97. expire_timestamp = int(time.time()) - self.EXPIRE_TIME
  98. task_list = await self.get_task_list(earliest_timestamp, expire_timestamp)
  99. if task_list:
  100. await feishu_robot.bot(
  101. title="自动下架视频失败",
  102. detail={
  103. "total_video": len(task_list),
  104. "video_list": [i["video_id"] for i in task_list],
  105. },
  106. mention=False,
  107. )
  108. return self.TASK_FAILED_STATUS
  109. else:
  110. return self.TASK_SUCCESS_STATUS
  111. class CheckVideoAuditStatus(GetOffVideosConst):
  112. def __init__(self, db_client, log_client):
  113. self.db_client = db_client
  114. self.log_client = log_client
  115. async def get_video_list_status(self, video_list: List[int]):
  116. response = await fetch_piaoquan_video_list_detail(video_list)
  117. video_detail_list = response.get("data", [])
  118. if video_detail_list:
  119. bad_video_list = [
  120. i["id"]
  121. for i in video_detail_list
  122. if i["auditStatus"] != self.VIDEO_AUDIT_SUCCESS_STATUS
  123. ]
  124. else:
  125. bad_video_list = []
  126. return bad_video_list
  127. async def get_unchecked_video_list(self) -> Optional[List[int]]:
  128. """find unchecked videos"""
  129. query = f"""
  130. select video_id from {self.table} where check_status = %s and video_status = %s limit 1000;
  131. """
  132. video_id_list, error = await self.db_client.async_fetch(
  133. query, params=(self.CHECK_INIT_STATUS, self.VIDEO_AVAILABLE_STATUS)
  134. )
  135. if error:
  136. print("error", error)
  137. return None
  138. else:
  139. return [i["video_id"] for i in video_id_list]
  140. async def update_check_status(self, video_list: List[int]):
  141. query = f"""update {self.table} set check_status = %s where video_id in %s;"""
  142. return await self.db_client.async_save(
  143. query, params=(self.CHECK_FINISHED_STATUS, tuple(video_list))
  144. )
  145. async def deal(self):
  146. def chuck_iterator(arr, chunk_size):
  147. for i in range(0, len(arr), chunk_size):
  148. yield arr[i : i + chunk_size]
  149. video_id_list = await self.get_unchecked_video_list()
  150. video_chunks = chuck_iterator(video_id_list, 10)
  151. bad_videos_count = 0
  152. fail_list = []
  153. for video_chunk in video_chunks:
  154. bad_video_id_list = await self.get_video_list_status(video_chunk)
  155. if bad_video_id_list:
  156. bad_videos_count += len(bad_video_id_list)
  157. for bad_video_id in tqdm(bad_video_id_list):
  158. response = await change_video_audit_status(bad_video_id)
  159. if not response:
  160. fail_list.append(bad_video_id)
  161. await self.update_check_status(video_chunk)
  162. if fail_list:
  163. await feishu_robot.bot(
  164. title="校验已发布视频状态出现错误", detail=fail_list, mention=False
  165. )
  166. return self.TASK_FAILED_STATUS
  167. else:
  168. return self.TASK_SUCCESS_STATUS