get_off_videos.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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(
  22. self, earliest_timestamp_threshold, expire_timestamp_threshold
  23. ):
  24. """get videos which need get off"""
  25. earliest_timestamp_threshold = 0
  26. query = f"""
  27. select video_id from {self.table} where video_status = %s and publish_time between %s and %s;
  28. """
  29. video_list, error = await self.db_client.async_fetch(
  30. query,
  31. params=(
  32. self.VIDEO_AVAILABLE_STATUS,
  33. earliest_timestamp_threshold,
  34. expire_timestamp_threshold,
  35. ),
  36. )
  37. return video_list
  38. async def update_video_status(self, video_id):
  39. query = f"""
  40. update {self.table} set video_status = %s, get_off_time = %s where video_id = %s;
  41. """
  42. return await self.db_client.async_save(
  43. query, params=(self.VIDEO_DISABLE_STATUS, int(time.time()), video_id)
  44. )
  45. async def update_video_audit_status(self, video_id):
  46. """use pq api to update video status"""
  47. response = await change_video_audit_status(
  48. video_id, self.VIDEO_AUDIT_FAIL_STATUS
  49. )
  50. await self.update_video_status(video_id)
  51. return response
  52. async def get_off_job(self):
  53. """get off videos out of expire time"""
  54. expire_timestamp_threshold = int(time.time()) - self.EXPIRE_TIME
  55. earliest_timestamp_threshold = int(time.time()) - self.EARLIEST_TIME
  56. task_list = await self.get_task_list(
  57. earliest_timestamp_threshold, expire_timestamp_threshold
  58. )
  59. for task in tqdm(task_list):
  60. video_id = task["video_id"]
  61. try:
  62. await self.update_video_audit_status(video_id)
  63. except Exception as e:
  64. self.log_client.log(
  65. contents={
  66. "task": "get_off_videos",
  67. "function": "get_off_job",
  68. "status": "fail",
  69. "message": "get off video fail",
  70. "data": {
  71. "video_id": video_id,
  72. "error": str(e),
  73. "traceback": traceback.format_exc(),
  74. },
  75. }
  76. )
  77. async def check(self):
  78. earliest_timestamp = int(time.time()) - self.EARLIEST_TIME
  79. expire_timestamp = int(time.time()) - self.EXPIRE_TIME
  80. task_list = await self.get_task_list(earliest_timestamp, expire_timestamp)
  81. if task_list:
  82. await feishu_robot.bot(
  83. title="自动下架视频失败",
  84. detail={
  85. "total_video": len(task_list),
  86. "video_list": [i["video_id"] for i in task_list],
  87. },
  88. mention=False,
  89. )
  90. return self.TASK_FAILED_STATUS
  91. else:
  92. return self.TASK_SUCCESS_STATUS