checkVideoStatusDaily.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. """
  2. @author: luojunhui
  3. @description: 校验视频状态,若视频状态为不通过,则修改视频状态
  4. """
  5. import traceback
  6. from tqdm import tqdm
  7. from applications import PQAPI, PQMySQL, bot
  8. class VideoStatusManager(object):
  9. """
  10. 视频状态校验 and 修改
  11. """
  12. def __init__(self):
  13. self.db_client = None
  14. self.pq_api = None
  15. def base_init(self):
  16. """
  17. 初始化数据库连接和 pq 方法类
  18. """
  19. try:
  20. self.db_client = PQMySQL()
  21. except Exception as e:
  22. error_msg = traceback.format_exc()
  23. bot(
  24. title="视频状态校验任务,每 20 分钟执行一次,数据库初始化异常",
  25. detail={
  26. "error": str(e),
  27. "error_msg": error_msg
  28. }
  29. )
  30. return False
  31. try:
  32. self.pq_api = PQAPI()
  33. except Exception as e:
  34. error_msg = traceback.format_exc()
  35. bot(
  36. title="视频状态校验任务,每 20 分钟执行一次,apollo连接异常",
  37. detail={
  38. "error": str(e),
  39. "error_msg": error_msg
  40. }
  41. )
  42. return False
  43. return True
  44. def get_video_list_status(self, videoList):
  45. """
  46. 获取视频 list 的状态,并且返回状态为不通过的视频 list
  47. :param videoList: 视频 id_list, 最长为 20
  48. :return: bad video list
  49. """
  50. response = self.pq_api.getPQVideoListDetail(video_list=videoList)
  51. detail_list = response.get('data', [])
  52. if detail_list:
  53. bad_video_list = [i for i in detail_list if i['auditStatus'] != 5]
  54. bad_id_list = [i['id'] for i in bad_video_list]
  55. else:
  56. bad_id_list = []
  57. return bad_id_list
  58. def get_published_video_ids_daily(self):
  59. """
  60. 获取每日发布的视频 id 的状态
  61. :return:
  62. publish_time > {today_time_stamp} and
  63. """
  64. select_sql = f"""
  65. SELECT video_id
  66. FROM get_off_videos
  67. WHERE check_status = 0 and video_status = 1;
  68. """
  69. video_id_tuple = self.db_client.select(select_sql)
  70. video_id_list = [i[0] for i in video_id_tuple]
  71. return video_id_list
  72. def update_check_status(self, vid_list):
  73. """
  74. :param vid_list:
  75. :return:
  76. """
  77. sql = f"""
  78. UPDATE get_off_videos
  79. SET check_status = %s
  80. where video_id in %s;
  81. """
  82. self.db_client.update(
  83. sql=sql,
  84. params=(1, tuple(vid_list))
  85. )
  86. print("更新 check_status 成功")
  87. def deal(self):
  88. """
  89. Deal Function
  90. :return:
  91. """
  92. def chunk_iterator(arr, chunk_size):
  93. """生成器函数,将数组分组成指定大小的chunks"""
  94. for i in range(0, len(arr), chunk_size):
  95. yield arr[i:i + chunk_size]
  96. video_id_list = self.get_published_video_ids_daily()
  97. video_chunks = chunk_iterator(video_id_list, 10)
  98. bad_count = 0
  99. for video_temp in video_chunks:
  100. bad_id_list = self.get_video_list_status(video_temp)
  101. fail_list = []
  102. if bad_id_list:
  103. bad_count += len(bad_id_list)
  104. for bad_id in tqdm(bad_id_list):
  105. response = self.pq_api.changeVideoStatus(bad_id)
  106. if not response:
  107. fail_list.append(bad_id)
  108. if fail_list:
  109. bot(
  110. title="修改视频状态失败",
  111. detail=fail_list
  112. )
  113. self.update_check_status(video_temp)
  114. print("total", len(video_id_list))
  115. print("bad_total", bad_count)
  116. def main():
  117. """
  118. task
  119. :return:
  120. """
  121. VM = VideoStatusManager()
  122. if VM.base_init():
  123. VM.deal()
  124. if __name__ == '__main__':
  125. main()