|  | @@ -476,7 +476,198 @@ class AiCheckAndUpTOFullCreative(tornado.web.RequestHandler):
 | 
	
		
			
				|  |  |          logger.info("账户信息=%s, 当前进程=%s, 当前进程的主进程=%s,  raw data from request is %s" %
 | 
	
		
			
				|  |  |                      (account_id, os.getpid(), os.getppid(), data))
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |          try:
 | 
	
		
			
				|  |  | +            # 1-1 从客户策略表中获取该账户下 每个素材搭配的封面个数, 来计算需要补充的素材个数
 | 
	
		
			
				|  |  | +            sql = """select image_cnt from ctop_ai_kuaishou_advertiser_strategy 
 | 
	
		
			
				|  |  | +                        where account_id = %s
 | 
	
		
			
				|  |  | +                        limit 1""" % account_id
 | 
	
		
			
				|  |  | +            df = pd.read_sql(sql, engine)
 | 
	
		
			
				|  |  | +            image_cnt = int(df['image_cnt'].values[0])
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            # 1-2、获取从0点到now()为止,该账户下已有的创意个数 M
 | 
	
		
			
				|  |  | +            sql = """select count(1) creative_cnt from ctop_kuaishou_creative 
 | 
	
		
			
				|  |  | +                     where account_id = %s
 | 
	
		
			
				|  |  | +                     and creative_id is not NULL
 | 
	
		
			
				|  |  | +                     and creative_create_time <= '%s'
 | 
	
		
			
				|  |  | +                     and creative_create_time >= '%s'""" % (account_id,
 | 
	
		
			
				|  |  | +                                                            (datetime.datetime.now()).strftime('%Y-%m-%d %H:%M:%S'),
 | 
	
		
			
				|  |  | +                                                            (datetime.datetime.now()).strftime('%Y-%m-%d'))
 | 
	
		
			
				|  |  | +            df = pd.read_sql(sql, engine)
 | 
	
		
			
				|  |  | +            exist_creative_cnt = int(df['creative_cnt'].values[0])
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            now_hour = datetime.datetime.now().hour
 | 
	
		
			
				|  |  | +            now = datetime.datetime.now()
 | 
	
		
			
				|  |  | +            should_creative_cnt = now_hour * 100
 | 
	
		
			
				|  |  | +            if exist_creative_cnt < should_creative_cnt:
 | 
	
		
			
				|  |  | +                need_video_cnt = (should_creative_cnt - exist_creative_cnt) // image_cnt
 | 
	
		
			
				|  |  | +                logger.info("account_id = %s, 按小时需要补充的视频数量 = %s" % (account_id, need_video_cnt))
 | 
	
		
			
				|  |  | +                if now_hour % 5 == 0:
 | 
	
		
			
				|  |  | +                    video_list = get_top_video_info(account_id=account_id,
 | 
	
		
			
				|  |  | +                                                    url=get_high_quality_video_url,
 | 
	
		
			
				|  |  | +                                                    start_time=(now + datetime.timedelta(days=-2 * high_quality_video_days)).
 | 
	
		
			
				|  |  | +                                                    strftime("%Y-%m-%d %H:%M:%S"),
 | 
	
		
			
				|  |  | +                                                    end_time=(now + datetime.timedelta(days=-high_quality_video_days)).
 | 
	
		
			
				|  |  | +                                                    strftime("%Y-%m-%d %H:%M:%S"),
 | 
	
		
			
				|  |  | +                                                    cnt=need_video_cnt,
 | 
	
		
			
				|  |  | +                                                    app_version=None)
 | 
	
		
			
				|  |  | +                else:
 | 
	
		
			
				|  |  | +                    video_list = get_history_video_info(account_id=account_id,
 | 
	
		
			
				|  |  | +                                                        url=get_history_video_url,
 | 
	
		
			
				|  |  | +                                                        start_time=history_video_start_time,
 | 
	
		
			
				|  |  | +                                                        end_time=history_video_end_time,
 | 
	
		
			
				|  |  | +                                                        video_cnt=need_video_cnt,
 | 
	
		
			
				|  |  | +                                                        related_creative_max_cnt=related_creative_max_cnt)
 | 
	
		
			
				|  |  | +                # 根据获取到的视频进行创建
 | 
	
		
			
				|  |  | +                sql = """select account_id, single_appid, app_id_array, general_track from ctop_ai_kuaishou_advertiser_strategy
 | 
	
		
			
				|  |  | +                         where account_id=%s order by create_time desc limit 1""" % account_id
 | 
	
		
			
				|  |  | +                df = pd.read_sql(sql, engine)
 | 
	
		
			
				|  |  | +                if df['general_track'].values[0] == 0:
 | 
	
		
			
				|  |  | +                    app_id_array = eval(df['app_id_array'].values[0])
 | 
	
		
			
				|  |  | +                    app_list = get_app_list(account_id=account_id, url=get_app_list_url)
 | 
	
		
			
				|  |  | +                    # app_dict key = ("appVersion": "ksjyrlyn-人类一脑涂地大败") , value = 完整的app_id信息
 | 
	
		
			
				|  |  | +                    app_dict = {}
 | 
	
		
			
				|  |  | +                    for app_info in app_list['data']:
 | 
	
		
			
				|  |  | +                        if app_info['appId'] in app_id_array:
 | 
	
		
			
				|  |  | +                            app_dict[app_info['appVersion']] = app_info
 | 
	
		
			
				|  |  | +                    # 单独处理返回的时候中,有通用视频,需要单独处理
 | 
	
		
			
				|  |  | +                    # "photo_name": "all",
 | 
	
		
			
				|  |  | +                    # "photo_name": "ksjytdrca-糖豆人冲啊",
 | 
	
		
			
				|  |  | +                    if video_list['code'] == 0:
 | 
	
		
			
				|  |  | +                        video_df = pd.DataFrame(video_list['data'])
 | 
	
		
			
				|  |  | +                        # 1-特定视频的创建
 | 
	
		
			
				|  |  | +                        special_video_df = video_df[video_df.photo_name != 'all']
 | 
	
		
			
				|  |  | +                        for photo_name in special_video_df['photo_name'].unique():
 | 
	
		
			
				|  |  | +                            if photo_name in app_dict.keys():
 | 
	
		
			
				|  |  | +                                campaign_id = None
 | 
	
		
			
				|  |  | +                                app_id = app_dict[photo_name].get('appId', 'error')
 | 
	
		
			
				|  |  | +                                app_version = app_dict[photo_name].get('appVersion', 'error')
 | 
	
		
			
				|  |  | +                                track_url = app_dict[photo_name].get('trackUrl', 'error')
 | 
	
		
			
				|  |  | +                                sql = """ select campaign_id, campaign_name from ctop_kuaishou_campaign where account_id = %s """ % account_id
 | 
	
		
			
				|  |  | +                                campaign_df = pd.read_sql(sql, engine)
 | 
	
		
			
				|  |  | +                                for row in campaign_df.itertuples():
 | 
	
		
			
				|  |  | +                                    if (app_version + '--优选广告位-' + '高质量素材') == getattr(row, 'campaign_name'):
 | 
	
		
			
				|  |  | +                                        campaign_id = getattr(row, 'campaign_id')
 | 
	
		
			
				|  |  | +                                        break
 | 
	
		
			
				|  |  | +                                app_id_video_df = special_video_df[special_video_df.photo_name == photo_name]
 | 
	
		
			
				|  |  | +                                app_id_video_list = []
 | 
	
		
			
				|  |  | +                                for k, v in app_id_video_df.T.to_dict().items():
 | 
	
		
			
				|  |  | +                                    app_id_video_list.append(v)
 | 
	
		
			
				|  |  | +                                request_data = get_request_data(account_id=account_id,
 | 
	
		
			
				|  |  | +                                                                campaign_id=campaign_id,
 | 
	
		
			
				|  |  | +                                                                video_info=app_id_video_list,
 | 
	
		
			
				|  |  | +                                                                campaign_name=app_version + '--优选广告位-' + '高质量素材',
 | 
	
		
			
				|  |  | +                                                                group_name=app_version + '-优选广告位-不限-自定义-' + '高质量素材',
 | 
	
		
			
				|  |  | +                                                                ai_strategy_remark="按小时补满创意",
 | 
	
		
			
				|  |  | +                                                                unit_type=4,
 | 
	
		
			
				|  |  | +                                                                app_id=app_id,
 | 
	
		
			
				|  |  | +                                                                click_track_url=track_url)
 | 
	
		
			
				|  |  | +                                request = ai_strategy_request_parse(request_data)
 | 
	
		
			
				|  |  | +                                logger.info("account_id=%s, app_id=%s, 用高质量素材补满创意-特定视频,ai策略的返回信息:%s" %
 | 
	
		
			
				|  |  | +                                            (account_id, app_id, request))
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                        # 2-通用视频的创建,random.shuffle 选择 app_id
 | 
	
		
			
				|  |  | +                        general_video_df = video_df[video_df.photo_name == 'all']
 | 
	
		
			
				|  |  | +                        if not general_video_df.empty:
 | 
	
		
			
				|  |  | +                            general_video_cnt = len(general_video_df)
 | 
	
		
			
				|  |  | +                            app_version_list = list(app_dict)
 | 
	
		
			
				|  |  | +                            random.shuffle(app_version_list)
 | 
	
		
			
				|  |  | +                            video_cnt = 0
 | 
	
		
			
				|  |  | +                            for app_version in app_version_list[:general_video_cnt]:
 | 
	
		
			
				|  |  | +                                campaign_id = None
 | 
	
		
			
				|  |  | +                                app_id = app_dict[app_version].get('appId', 'error')
 | 
	
		
			
				|  |  | +                                track_url = app_dict[app_version].get('trackUrl', 'error')
 | 
	
		
			
				|  |  | +                                sql = """ select campaign_id, campaign_name from ctop_kuaishou_campaign where account_id = %s """ % account_id
 | 
	
		
			
				|  |  | +                                campaign_df = pd.read_sql(sql, engine)
 | 
	
		
			
				|  |  | +                                for row in campaign_df.itertuples():
 | 
	
		
			
				|  |  | +                                    if (app_version + '--优选广告位-' + '高质量素材') == getattr(row, 'campaign_name'):
 | 
	
		
			
				|  |  | +                                        campaign_id = getattr(row, 'campaign_id')
 | 
	
		
			
				|  |  | +                                        break
 | 
	
		
			
				|  |  | +                                single_video = [general_video_df.iloc[video_cnt, :].T.to_dict()]
 | 
	
		
			
				|  |  | +                                request_data = get_request_data(account_id=account_id,
 | 
	
		
			
				|  |  | +                                                                campaign_id=campaign_id,
 | 
	
		
			
				|  |  | +                                                                video_info=single_video,
 | 
	
		
			
				|  |  | +                                                                campaign_name=app_version + '--优选广告位-' + '高质量素材',
 | 
	
		
			
				|  |  | +                                                                group_name=app_version + '-优选广告位-不限-自定义-' + '高质量素材',
 | 
	
		
			
				|  |  | +                                                                ai_strategy_remark="用高质量素材补满创意",
 | 
	
		
			
				|  |  | +                                                                unit_type=4,
 | 
	
		
			
				|  |  | +                                                                app_id=app_id,
 | 
	
		
			
				|  |  | +                                                                click_track_url=track_url)
 | 
	
		
			
				|  |  | +                                request = ai_strategy_request_parse(request_data)
 | 
	
		
			
				|  |  | +                                video_cnt += 1
 | 
	
		
			
				|  |  | +                                logger.info("account_id=%s, app_id=%s, 用高质量素材补满创意-通用视频,ai策略的返回信息:%s" % (account_id, app_id, request))
 | 
	
		
			
				|  |  | +                        else:
 | 
	
		
			
				|  |  | +                            logger.info("account_id=%s, 用高质量素材补满创意: 返回的视频中没有通用视频" % account_id)
 | 
	
		
			
				|  |  | +                        self.write(json.dumps({"message": "account_id=%s, 用高质量素材补满创意请求已走完!" % account_id}))
 | 
	
		
			
				|  |  | +                        self.flush()
 | 
	
		
			
				|  |  | +                    else:
 | 
	
		
			
				|  |  | +                        logger.info("account_id=%s, 按小时补满创意: 没有成功获取到视频!" % account_id)
 | 
	
		
			
				|  |  | +                        self.write(json.dumps({"message": "account_id=%s, 按小时补满创意: 没有成功获取到视频!" % account_id}))
 | 
	
		
			
				|  |  | +                        self.flush()
 | 
	
		
			
				|  |  | +                else:
 | 
	
		
			
				|  |  | +                    # 1-1 获取当天计划中包含“高质量素材”计划的,计划id
 | 
	
		
			
				|  |  | +                    sql = """
 | 
	
		
			
				|  |  | +                                                       select campaign_id, campaign_name from ctop_kuaishou_campaign 
 | 
	
		
			
				|  |  | +                                                       where account_id = %s
 | 
	
		
			
				|  |  | +                                                       and put_create_time >= '%s'
 | 
	
		
			
				|  |  | +                                                       and put_create_time <= '%s'
 | 
	
		
			
				|  |  | +                                                       """ % (account_id, start_time, end_time)
 | 
	
		
			
				|  |  | +                    df = pd.read_sql(sql, engine)
 | 
	
		
			
				|  |  | +                    for row in df.itertuples():
 | 
	
		
			
				|  |  | +                        if '高质量素材' in getattr(row, 'campaign_name'):
 | 
	
		
			
				|  |  | +                            campaign_id = getattr(row, 'campaign_id')
 | 
	
		
			
				|  |  | +                            break
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                    video_info = get_top_video_info(account_id=account_id,
 | 
	
		
			
				|  |  | +                                                    cnt=video_cnt,
 | 
	
		
			
				|  |  | +                                                    start_time=(now + datetime.timedelta(days=-2 * high_quality_video_days)).
 | 
	
		
			
				|  |  | +                                                    strftime("%Y-%m-%d %H:%M:%S"),
 | 
	
		
			
				|  |  | +                                                    end_time=(now + datetime.timedelta(days=-high_quality_video_days)).
 | 
	
		
			
				|  |  | +                                                    strftime("%Y-%m-%d %H:%M:%S"),
 | 
	
		
			
				|  |  | +                                                    url=get_high_quality_video_url)
 | 
	
		
			
				|  |  | +                    if video_info['code'] != 0:
 | 
	
		
			
				|  |  | +                        logger.info("account_id = %s, 历史高质量素材填满, 没有获取到视频,不发送ai策略请求!" % account_id)
 | 
	
		
			
				|  |  | +                        self.write(json.dumps({"account_id": account_id,
 | 
	
		
			
				|  |  | +                                               "message": "历史高质量素材填满, 没有获取到视频,不发送ai策略请求!"}))
 | 
	
		
			
				|  |  | +                        self.flush()
 | 
	
		
			
				|  |  | +                    else:
 | 
	
		
			
				|  |  | +                        campaign_name, group_name = get_campaign_and_group_name_rule(account_id)
 | 
	
		
			
				|  |  | +                        if (campaign_name is None) or (group_name is None):
 | 
	
		
			
				|  |  | +                            logger.info("account_id = %s, 没有获取到广告计划命名规则 或 广告组命名规则,不发送ai策略请求!" % account_id)
 | 
	
		
			
				|  |  | +                            self.write(json.dumps({"account_id": account_id,
 | 
	
		
			
				|  |  | +                                                   "message": "没有获取到广告计划命名规则 或 广告组命名规则,不发送ai策略请求!"}))
 | 
	
		
			
				|  |  | +                            self.flush()
 | 
	
		
			
				|  |  | +                        else:
 | 
	
		
			
				|  |  | +                            request_data = get_request_data(account_id=account_id,
 | 
	
		
			
				|  |  | +                                                            campaign_id=campaign_id,
 | 
	
		
			
				|  |  | +                                                            video_info=video_info['data'],
 | 
	
		
			
				|  |  | +                                                            campaign_name=campaign_name,
 | 
	
		
			
				|  |  | +                                                            group_name=group_name,
 | 
	
		
			
				|  |  | +                                                            ai_strategy_remark="用高质量素材补满创意",
 | 
	
		
			
				|  |  | +                                                            name_replace="高质量素材")
 | 
	
		
			
				|  |  | +                            request = ai_strategy_request_parse(request_data)
 | 
	
		
			
				|  |  | +                            logger.info("account_id = %s, 用高质量素材补满创意,ai策略的返回信息:%s" % (account_id, request))
 | 
	
		
			
				|  |  | +                            self.write(json.dumps(request))
 | 
	
		
			
				|  |  | +                            self.flush()
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            else:
 | 
	
		
			
				|  |  | +                logger.info("account_id = %s, 创意已经建满,不需要补充素材!" % account_id)
 | 
	
		
			
				|  |  | +                self.write(json.dumps({"account_id": account_id, "message": "创意已经建满,不需要补充素材!"}))
 | 
	
		
			
				|  |  | +                self.flush()
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            # 2、如果创意个数M 小于 hour()*100, 则使用 (hour()*100 - M)// image_cnt 个视频
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            # 2、datetime.datetime.now().hour % 5, 如果不等于0, 则使用创意关联数少的视频,否则使用历史跑量视频
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |              start_time = (datetime.date.today()).strftime('%Y-%m-%d %H:%M:%S')
 | 
	
		
			
				|  |  |              end_time = (datetime.datetime.now()).strftime('%Y-%m-%d %H:%M:%S')
 | 
	
		
			
				|  |  |              now = datetime.datetime.now()
 |