get_off_videos.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import time
  2. import traceback
  3. from tqdm import tqdm
  4. from applications.api import change_video_audit_status
  5. from applications.api import feishu_robot
  6. class GetOffVideosConst:
  7. EXPIRE_TIME = 3 * 24 * 3600
  8. EARLIEST_TIME = 7 * 24 * 3600
  9. VIDEO_AVAILABLE_STATUS = 1
  10. VIDEO_DISABLE_STATUS = 0
  11. # VIDEO_AUDIT
  12. VIDEO_AUDIT_FAIL_STATUS = 2
  13. # Task code
  14. TASK_SUCCESS_STATUS = 2
  15. TASK_FAILED_STATUS = 99
  16. class GetOffVideos(GetOffVideosConst):
  17. def __init__(self, db_client, log_client):
  18. self.db_client = db_client
  19. self.log_client = log_client
  20. self.table = "get_off_videos"
  21. async def get_task_list(self, earliest_timestamp_threshold, expire_timestamp_threshold):
  22. """get videos which need get off"""
  23. earliest_timestamp_threshold = 0
  24. query = f"""
  25. select video_id from {self.table} where video_status = %s and publish_time between %s and %s;
  26. """
  27. video_list, error = await self.db_client.async_fetch(query, params=(self.VIDEO_AVAILABLE_STATUS, earliest_timestamp_threshold, expire_timestamp_threshold))
  28. return video_list
  29. async def update_video_status(self, video_id):
  30. query = f"""
  31. update {self.table} set video_status = %s, get_off_time = %s where video_id = %s;
  32. """
  33. return await self.db_client.async_save(query, params=(self.VIDEO_DISABLE_STATUS, int(time.time()), video_id))
  34. async def update_video_audit_status(self, video_id):
  35. """use pq api to update video status"""
  36. response = await change_video_audit_status(video_id, self.VIDEO_AUDIT_FAIL_STATUS)
  37. await self.update_video_status(video_id)
  38. return response
  39. async def get_off_job(self):
  40. """get off videos out of expire time"""
  41. expire_timestamp_threshold = int(time.time()) - self.EXPIRE_TIME
  42. earliest_timestamp_threshold = int(time.time()) - self.EARLIEST_TIME
  43. task_list = await self.get_task_list(earliest_timestamp_threshold, expire_timestamp_threshold)
  44. for task in tqdm(task_list):
  45. video_id = task['video_id']
  46. try:
  47. await self.update_video_audit_status(video_id)
  48. except Exception as e:
  49. self.log_client.log(
  50. contents={
  51. "task": "get_off_videos",
  52. "function": "get_off_job",
  53. "status": "fail",
  54. "message": "get off video fail",
  55. "data": {
  56. "video_id": video_id,
  57. "error": str(e),
  58. "traceback": traceback.format_exc()
  59. }
  60. }
  61. )
  62. async def check(self):
  63. earliest_timestamp = int(time.time()) - self.EARLIEST_TIME
  64. expire_timestamp = int(time.time()) - self.EXPIRE_TIME
  65. task_list = await self.get_task_list(earliest_timestamp, expire_timestamp)
  66. if task_list:
  67. await feishu_robot.bot(
  68. title="自动下架视频失败",
  69. detail={
  70. "total_video": len(task_list),
  71. "video_list": [i['video_id'] for i in task_list]
  72. },
  73. mention=False
  74. )
  75. return self.TASK_FAILED_STATUS
  76. else:
  77. return self.TASK_SUCCESS_STATUS