|  | @@ -0,0 +1,114 @@
 | 
	
		
			
				|  |  | +import logging
 | 
	
		
			
				|  |  | +from logging import handlers
 | 
	
		
			
				|  |  | +import os
 | 
	
		
			
				|  |  | +from model.sql_models import DB
 | 
	
		
			
				|  |  | +from data_processing import video_processing
 | 
	
		
			
				|  |  | +# TODO:上线时需要进行修改
 | 
	
		
			
				|  |  | +from config import using_config_test as db_config
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +def image_check():
 | 
	
		
			
				|  |  | +    # TODO:图片素材,进行对应修改
 | 
	
		
			
				|  |  | +    pass
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +def video_check():
 | 
	
		
			
				|  |  | +    # 1.检查media表中是否有byte为空,但是download_path有数据的
 | 
	
		
			
				|  |  | +    sql = '''select download_path from t_ads_media tam 
 | 
	
		
			
				|  |  | +        where download_path is not null
 | 
	
		
			
				|  |  | +        and video_bit_rate is null
 | 
	
		
			
				|  |  | +       and type =2 ;
 | 
	
		
			
				|  |  | +    '''
 | 
	
		
			
				|  |  | +    cursor = zx_db.session.execute(sql)
 | 
	
		
			
				|  |  | +    download_path_list = []
 | 
	
		
			
				|  |  | +    for line in cursor.fetchall():
 | 
	
		
			
				|  |  | +        download_path_list.append(line[0])
 | 
	
		
			
				|  |  | +    zx_db.session.commit()
 | 
	
		
			
				|  |  | +    # 2.检查signature(md5),是否已经存在于media,如果已经存在,一切用原来的信息
 | 
	
		
			
				|  |  | +    for download_path in download_path_list:
 | 
	
		
			
				|  |  | +        # TODO:有空的时候,添加去除本来上传的视频
 | 
	
		
			
				|  |  | +        download_url = 'https://zx-media-database.oss-cn-hangzhou.aliyuncs.com/' + download_path
 | 
	
		
			
				|  |  | +        logging.info('开始解析视频:{} '.format(download_url))
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        # 3.进行视频解析
 | 
	
		
			
				|  |  | +        cloud_filepath, metadata_title, video_size, duration, bit_rate, width, height, format = video_processing.change_format(
 | 
	
		
			
				|  |  | +            download_url)
 | 
	
		
			
				|  |  | +        # -1.更新media,idea两种表的数据
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        type = 2
 | 
	
		
			
				|  |  | +        signature = metadata_title[3:]
 | 
	
		
			
				|  |  | +        media_size = video_size
 | 
	
		
			
				|  |  | +        media_format = format
 | 
	
		
			
				|  |  | +        video_length = duration
 | 
	
		
			
				|  |  | +        video_bit_rate = bit_rate
 | 
	
		
			
				|  |  | +        max_media_size = media_size
 | 
	
		
			
				|  |  | +        height = height
 | 
	
		
			
				|  |  | +        width = width
 | 
	
		
			
				|  |  | +        aspect_ratio = round(width / (height if height else 1), 1)
 | 
	
		
			
				|  |  | +        media_sql = '''
 | 
	
		
			
				|  |  | +            update t_ads_media 
 | 
	
		
			
				|  |  | +                        set video_mate_data='{video_mate_data}',
 | 
	
		
			
				|  |  | +                        type='{type}' ,signature='{signature}', media_size='{media_size}',
 | 
	
		
			
				|  |  | +                        media_format = '{media_format}', video_length='{video_length}',
 | 
	
		
			
				|  |  | +                        video_bit_rate = '{video_bit_rate}', max_media_size='{max_media_size}',
 | 
	
		
			
				|  |  | +                        height='{height}', width='{width}', aspect_ratio = '{aspect_ratio}'
 | 
	
		
			
				|  |  | +                        where download_path='{download_path}';
 | 
	
		
			
				|  |  | +        '''.format(download_path=download_path, video_mate_data=metadata_title, type=type,
 | 
	
		
			
				|  |  | +                   signature=signature, media_size=media_size,
 | 
	
		
			
				|  |  | +                   media_format=media_format, video_length=video_length,
 | 
	
		
			
				|  |  | +                   video_bit_rate=video_bit_rate, max_media_size=max_media_size,
 | 
	
		
			
				|  |  | +                   height=height, width=width, aspect_ratio=aspect_ratio)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        idea_sql = '''
 | 
	
		
			
				|  |  | +            update t_ads_idea 
 | 
	
		
			
				|  |  | +                set signature='{signature}', video_mate_data='{video_mate_data}',
 | 
	
		
			
				|  |  | +                         media_size='{media_size}',type='{type}' ,
 | 
	
		
			
				|  |  | +                        media_format = '{media_format}',
 | 
	
		
			
				|  |  | +                         video_length='{video_length}',
 | 
	
		
			
				|  |  | +                        video_bit_rate = '{video_bit_rate}', max_media_size='{max_media_size}',
 | 
	
		
			
				|  |  | +                        height='{height}', width='{width}', aspect_ratio = '{aspect_ratio}'
 | 
	
		
			
				|  |  | +                        where download_path='{download_path}'
 | 
	
		
			
				|  |  | +        '''.format(download_path=download_path, video_mate_data=metadata_title, type=type,
 | 
	
		
			
				|  |  | +                   signature=signature, media_size=media_size,
 | 
	
		
			
				|  |  | +                   media_format=media_format, video_length=video_length,
 | 
	
		
			
				|  |  | +                   video_bit_rate=video_bit_rate, max_media_size=max_media_size,
 | 
	
		
			
				|  |  | +                   height=height, width=width, aspect_ratio=aspect_ratio)
 | 
	
		
			
				|  |  | +        zx_db.session.execute(media_sql)
 | 
	
		
			
				|  |  | +        zx_db.session.execute(idea_sql)
 | 
	
		
			
				|  |  | +        zx_db.session.commit()
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +def run():
 | 
	
		
			
				|  |  | +    # 1.检查上一个进程是否已经完结
 | 
	
		
			
				|  |  | +    result = os.popen('ps aux | grep python | grep minuteRun.py | grep -v grep')
 | 
	
		
			
				|  |  | +    res = result.read()
 | 
	
		
			
				|  |  | +    for line in res.splitlines():
 | 
	
		
			
				|  |  | +        print(line)
 | 
	
		
			
				|  |  | +    print(len(res.splitlines()))
 | 
	
		
			
				|  |  | +    if len(res.splitlines()) > 1:
 | 
	
		
			
				|  |  | +        logging.info('前面有视频检查程序在运行')
 | 
	
		
			
				|  |  | +        return
 | 
	
		
			
				|  |  | +    else:
 | 
	
		
			
				|  |  | +        video_check()
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +if __name__ == '__main__':
 | 
	
		
			
				|  |  | +    # 定时检查素材库里的视频数据,是否是有
 | 
	
		
			
				|  |  | +    # 广告创意----也需要检查----图片可能是被动上传
 | 
	
		
			
				|  |  | +    #
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    logging.basicConfig(
 | 
	
		
			
				|  |  | +        handlers=[
 | 
	
		
			
				|  |  | +            logging.handlers.RotatingFileHandler('./log/minute_Run.log',
 | 
	
		
			
				|  |  | +                                                 maxBytes=10 * 1024 * 1024,
 | 
	
		
			
				|  |  | +                                                 backupCount=5,
 | 
	
		
			
				|  |  | +                                                 encoding='utf-8')
 | 
	
		
			
				|  |  | +            , logging.StreamHandler()  # 供输出使用
 | 
	
		
			
				|  |  | +        ],
 | 
	
		
			
				|  |  | +        level=logging.INFO,
 | 
	
		
			
				|  |  | +        format="%(asctime)s - %(levelname)s %(filename)s %(funcName)s %(lineno)s - %(message)s"
 | 
	
		
			
				|  |  | +    )
 | 
	
		
			
				|  |  | +    zx_db = DB(db_config.zx_ads)
 | 
	
		
			
				|  |  | +    logging.info('视频检查,开始')
 | 
	
		
			
				|  |  | +    run()
 | 
	
		
			
				|  |  | +    logging.info('视频检查,结束')
 |