|
@@ -2,10 +2,7 @@
|
|
|
@author: luojunhui
|
|
|
@description: 校验视频状态,若视频状态为不通过,则修改视频状态
|
|
|
"""
|
|
|
-import os
|
|
|
-import time
|
|
|
-
|
|
|
-import multiprocessing
|
|
|
+import traceback
|
|
|
|
|
|
from tqdm import tqdm
|
|
|
|
|
@@ -16,17 +13,47 @@ class VideoStatusManager(object):
|
|
|
"""
|
|
|
视频状态校验 and 修改
|
|
|
"""
|
|
|
- db_client = PQMySQL()
|
|
|
- pq_api = PQAPI()
|
|
|
+ def __init__(self):
|
|
|
+ self.db_client = None
|
|
|
+ self.pq_api = None
|
|
|
|
|
|
- @classmethod
|
|
|
- def getVideoListStatus(cls, videoList):
|
|
|
+ def base_init(self):
|
|
|
+ """
|
|
|
+ 初始化数据库连接和 pq 方法类
|
|
|
+ """
|
|
|
+ try:
|
|
|
+ self.db_client = PQMySQL()
|
|
|
+ except Exception as e:
|
|
|
+ error_msg = traceback.format_exc()
|
|
|
+ bot(
|
|
|
+ title="视频状态校验任务,每 20 分钟执行一次,数据库初始化异常",
|
|
|
+ detail={
|
|
|
+ "error": str(e),
|
|
|
+ "error_msg": error_msg
|
|
|
+ }
|
|
|
+ )
|
|
|
+ return False
|
|
|
+ try:
|
|
|
+ self.pq_api = PQAPI()
|
|
|
+ except Exception as e:
|
|
|
+ error_msg = traceback.format_exc()
|
|
|
+ bot(
|
|
|
+ title="视频状态校验任务,每 20 分钟执行一次,apollo连接异常",
|
|
|
+ detail={
|
|
|
+ "error": str(e),
|
|
|
+ "error_msg": error_msg
|
|
|
+ }
|
|
|
+ )
|
|
|
+ return False
|
|
|
+ return True
|
|
|
+
|
|
|
+ def get_video_list_status(self, videoList):
|
|
|
"""
|
|
|
获取视频 list 的状态,并且返回状态为不通过的视频 list
|
|
|
:param videoList: 视频 id_list, 最长为 20
|
|
|
:return: bad video list
|
|
|
"""
|
|
|
- response = cls.pq_api.getPQVideoListDetail(video_list=videoList)
|
|
|
+ response = self.pq_api.getPQVideoListDetail(video_list=videoList)
|
|
|
detail_list = response.get('data', [])
|
|
|
if detail_list:
|
|
|
bad_video_list = [i for i in detail_list if i['auditStatus'] != 5]
|
|
@@ -35,8 +62,7 @@ class VideoStatusManager(object):
|
|
|
bad_id_list = []
|
|
|
return bad_id_list
|
|
|
|
|
|
- @classmethod
|
|
|
- def getPublishedVideoIdsDaily(cls):
|
|
|
+ def get_published_video_ids_daily(self):
|
|
|
"""
|
|
|
获取每日发布的视频 id 的状态
|
|
|
:return:
|
|
@@ -47,12 +73,11 @@ class VideoStatusManager(object):
|
|
|
FROM get_off_videos
|
|
|
WHERE check_status = 0 and video_status = 1;
|
|
|
"""
|
|
|
- video_id_tuple = cls.db_client.select(select_sql)
|
|
|
+ video_id_tuple = self.db_client.select(select_sql)
|
|
|
video_id_list = [i[0] for i in video_id_tuple]
|
|
|
return video_id_list
|
|
|
|
|
|
- @classmethod
|
|
|
- def updateCheckStatus(cls, vid_list):
|
|
|
+ def update_check_status(self, vid_list):
|
|
|
"""
|
|
|
|
|
|
:param vid_list:
|
|
@@ -63,36 +88,33 @@ class VideoStatusManager(object):
|
|
|
SET check_status = %s
|
|
|
where video_id in %s;
|
|
|
"""
|
|
|
- cls.db_client.update(
|
|
|
+ self.db_client.update(
|
|
|
sql=sql,
|
|
|
params=(1, tuple(vid_list))
|
|
|
)
|
|
|
print("更新 check_status 成功")
|
|
|
|
|
|
- @classmethod
|
|
|
- def deal(cls):
|
|
|
+ def deal(self):
|
|
|
"""
|
|
|
Deal Function
|
|
|
:return:
|
|
|
"""
|
|
|
-
|
|
|
def chunk_iterator(arr, chunk_size):
|
|
|
"""生成器函数,将数组分组成指定大小的chunks"""
|
|
|
for i in range(0, len(arr), chunk_size):
|
|
|
yield arr[i:i + chunk_size]
|
|
|
|
|
|
- video_id_list = cls.getPublishedVideoIdsDaily()
|
|
|
-
|
|
|
+ video_id_list = self.get_published_video_ids_daily()
|
|
|
video_chunks = chunk_iterator(video_id_list, 10)
|
|
|
|
|
|
bad_count = 0
|
|
|
for video_temp in video_chunks:
|
|
|
- bad_id_list = cls.getVideoListStatus(video_temp)
|
|
|
+ bad_id_list = self.get_video_list_status(video_temp)
|
|
|
fail_list = []
|
|
|
if bad_id_list:
|
|
|
bad_count += len(bad_id_list)
|
|
|
for bad_id in tqdm(bad_id_list):
|
|
|
- response = cls.pq_api.changeVideoStatus(bad_id)
|
|
|
+ response = self.pq_api.changeVideoStatus(bad_id)
|
|
|
if not response:
|
|
|
fail_list.append(bad_id)
|
|
|
if fail_list:
|
|
@@ -100,35 +122,21 @@ class VideoStatusManager(object):
|
|
|
title="修改视频状态失败",
|
|
|
detail=fail_list
|
|
|
)
|
|
|
- cls.updateCheckStatus(video_temp)
|
|
|
+ self.update_check_status(video_temp)
|
|
|
print("total", len(video_id_list))
|
|
|
print("bad_total", bad_count)
|
|
|
|
|
|
|
|
|
-def task():
|
|
|
+def main():
|
|
|
"""
|
|
|
task
|
|
|
:return:
|
|
|
"""
|
|
|
- while True:
|
|
|
- VM = VideoStatusManager()
|
|
|
+
|
|
|
+ VM = VideoStatusManager()
|
|
|
+ if VM.base_init():
|
|
|
VM.deal()
|
|
|
- # print(1)
|
|
|
- time.sleep(10 * 60)
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
- process = None
|
|
|
-
|
|
|
- try:
|
|
|
- while True:
|
|
|
- if process is None or not process.is_alive():
|
|
|
- process = multiprocessing.Process(target=task)
|
|
|
- process.start()
|
|
|
-
|
|
|
- time.sleep(60)
|
|
|
- except KeyboardInterrupt:
|
|
|
- if process and process.is_alive():
|
|
|
- process.terminate()
|
|
|
-
|
|
|
-
|
|
|
+ main()
|