Bladeren bron

233多应用获取素材优化

liyuyi@c-top.com.cn 4 jaren geleden
bovenliggende
commit
f4bf7cd646

+ 554 - 349
ai_time_task_creative_handler.py

@@ -10,7 +10,7 @@ from utils.ConstantConfig import *
 from utils.DataBaseConfig import *
 from utils.CommonFunction import get_history_video_info, get_new_video_info, get_missing_video_info, \
                                  get_top_video_info, get_campaign_and_group_name_rule, get_app_detail, get_request_data,\
-                                 refresh_video
+                                 refresh_video, get_app_list
 
 from ai_strategy_request_func import ai_strategy_request_parse
 
@@ -45,69 +45,83 @@ class AiHistoricalMissingMaterial(tornado.web.RequestHandler):
             df = pd.read_sql(sql, engine)
             if df['general_track'].values[0] == 0:
                 app_id_array = eval(df['app_id_array'].values[0])
-                random.shuffle(app_id_array)
-                app_id_cnt = 0
-                for app_id in app_id_array:
-                    campaign_id = None
-                    # 1、获取该 app_id 的应该详情
-                    app_id_info = get_app_detail(account_id=account_id,
-                                                 url=get_app_detail_url,
-                                                 app_id=app_id)
-                    if app_id_info['code'] != 0:
-                        continue
-
-                    # 2、依据 appVersion 的值,来判断是否已经有对应的计划,如果有则获取 campaign_id,没有则设置 campaign_name
-                    app_version = app_id_info['data'].get('appVersion', 'error')
-                    track_url = app_id_info['data'].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
-
-                    # 3、获取通用视频1个
-                    general_video_info = get_missing_video_info(account_id=account_id,
-                                                                cnt=1,
-                                                                url=get_missing_video_url,
-                                                                start_time=missing_video_start_time,
-                                                                end_time=missing_video_end_time,
-                                                                app_version='all')
-                    # 4、获取app_id对应的视频1个
-                    special_video_info = get_missing_video_info(account_id=account_id,
-                                                                cnt=1,
-                                                                url=get_missing_video_url,
-                                                                start_time=missing_video_start_time,
-                                                                end_time=missing_video_end_time,
-                                                                app_version=app_version)
-                    # 5、video_info 合并
-                    full_video_info = []
-                    if general_video_info['code'] == 0:
-                        full_video_info.extend(general_video_info['data'])
-                    if special_video_info['code'] == 0:
-                        full_video_info.extend(special_video_info['data'])
-
-                    # 6、拼装request_data,调用ai_strategy_request_parse方法
-                    # 广告计划名称:渠道包 - 游戏名称 - 版位 - 日期
-                    # 广告组名称:渠道包 - 游戏名称 - 优选 - 不限(定向)-自定义 / 程序化 - 日期
-                    if full_video_info:
-                        request_data = get_request_data(account_id=account_id,
-                                                        campaign_id=campaign_id,
-                                                        video_info=full_video_info,
-                                                        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))
-                        app_id_cnt += 1
-                    if app_id_cnt >= missing_video_app_id:
-                        break
+                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
+                video_list = get_missing_video_info(account_id=account_id,
+                                                    cnt=missing_video_cnt,
+                                                    url=get_missing_video_url,
+                                                    start_time=missing_video_start_time,
+                                                    end_time=missing_video_end_time)
+                # 返回视频中有通用视频和特定视频,需要分开处理
+                # "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:
@@ -147,7 +161,7 @@ class AiHistoricalMissingMaterial(tornado.web.RequestHandler):
 
 class AiAutoCreative(tornado.web.RequestHandler):
     """
-    每5分钟,检查一次是否有上新素材,有的话,就自动创建 -- 需要上定时任务测试
+    每5分钟,检查一次是否有上新素材,有的话,就自动创建
     """
     def post(self):
         data = self.request.body
@@ -170,62 +184,85 @@ class AiAutoCreative(tornado.web.RequestHandler):
             df = pd.read_sql(sql, engine)
             if df['general_track'].values[0] == 0:
                 app_id_array = eval(df['app_id_array'].values[0])
-                random.shuffle(app_id_array)
-                for app_id in app_id_array:
-                    campaign_id = None
-                    # 1、获取该 app_id 的应该详情
-                    app_id_info = get_app_detail(account_id=account_id,
-                                                 url=get_app_detail_url,
-                                                 app_id=app_id)
-                    if app_id_info['code'] != 0:
-                        continue
-
-                    # 2、依据 appVersion 的值,来判断是否已经有对应的计划,如果有则获取 campaign_id,没有则设置 campaign_name
-                    app_version = app_id_info['data'].get('appVersion', 'error')
-                    track_url = app_id_info['data'].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_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
+                video_list = get_new_video_info(account_id=account_id,
+                                                url=get_new_video_url)
 
-                    # 3、获取通用视频
-                    general_video_info = get_new_video_info(account_id=account_id,
-                                                            url=get_new_video_url,
-                                                            app_version='all')
-                    # 4、获取app_id对应的特定视频
-                    special_video_info = get_new_video_info(account_id=account_id,
-                                                            url=get_new_video_url,
-                                                            app_version=app_version)
-                    # 5、video_info 合并
-                    full_video_info = []
-                    if general_video_info['code'] == 0:
-                        full_video_info.extend(general_video_info['data'])
-                    if special_video_info['code'] == 0:
-                        full_video_info.extend(special_video_info['data'])
-
-                    # 6、拼装request_data,调用ai_strategy_request_parse方法
-                    # 广告计划名称:渠道包 - 游戏名称 - 版位 - 日期
-                    # 广告组名称:渠道包 - 游戏名称 - 优选 - 不限(定向)-自定义 / 程序化 - 日期
-                    if full_video_info:
-                        request_data = get_request_data(account_id=account_id,
-                                                        campaign_id=campaign_id,
-                                                        video_info=full_video_info,
-                                                        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))
+                # 返回的视频中有通用视频和特定视频,需要分开处理
+                # "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, app_id=%s, 素材自动上新, 没有获取到视频,不发送ai策略请求!" % (account_id, app_id))
-
-                self.write(json.dumps({"message": "account_id=%s, 多应用的素材自动上新请求已走完!" % account_id}))
+                        logger.info("account_id=%s, 素材自动上新: 返回的视频中没有通用视频" % account_id)
+                else:
+                    logger.info("account_id=%s, 素材自动上新: 没有获取到视频" % account_id)
+                self.write(json.dumps({"message": "account_id=%s, 多应用的素材自动上新已走完!" % account_id}))
                 self.flush()
             else:
                 start_time = (datetime.date.today()).strftime('%Y-%m-%d %H:%M:%S')
@@ -295,73 +332,89 @@ class AiHighQualityMaterial(tornado.web.RequestHandler):
             df = pd.read_sql(sql, engine)
             if df['general_track'].values[0] == 0:
                 app_id_array = eval(df['app_id_array'].values[0])
-                random.shuffle(app_id_array)
-                app_id_cnt = 0
-                for app_id in app_id_array:
-                    campaign_id = None
-                    # 1、获取该 app_id 的应该详情
-                    app_id_info = get_app_detail(account_id=account_id,
-                                                 url=get_app_detail_url,
-                                                 app_id=app_id)
-                    if app_id_info['code'] != 0:
-                        continue
-
-                    # 2、依据 appVersion 的值,来判断是否已经有对应的计划,如果有则获取 campaign_id,没有则设置 campaign_name
-                    app_version = app_id_info['data'].get('appVersion', 'error')
-                    track_url = app_id_info['data'].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_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
+                video_list = get_top_video_info(account_id=account_id,
+                                                cnt=high_quality_video_cnt,
+                                                start_time=(datetime.datetime.now() +
+                                                            datetime.timedelta(days=-high_quality_video_days)).
+                                                strftime("%Y-%m-%d %H:%M:%S"),
+                                                end_time=datetime.date.today().strftime("%Y-%m-%d %H:%M:%S"),
+                                                url=get_high_quality_video_url)
 
-                    # 3、获取1个通用视频
-                    general_video_info = get_top_video_info(account_id=account_id,
-                                                            url=get_high_quality_video_url,
-                                                            start_time=(datetime.datetime.now() +
-                                                                        datetime.timedelta(days=-high_quality_video_days)).
-                                                            strftime("%Y-%m-%d %H:%M:%S"),
-                                                            end_time=datetime.date.today().strftime("%Y-%m-%d %H:%M:%S"),
-                                                            cnt=1,
-                                                            app_version='all')
-                    # 4、获取app_id对应的1个视频
-                    special_video_info = get_top_video_info(account_id=account_id,
-                                                            url=get_high_quality_video_url,
-                                                            start_time=(datetime.datetime.now() +
-                                                                        datetime.timedelta(days=-high_quality_video_days)).
-                                                            strftime("%Y-%m-%d %H:%M:%S"),
-                                                            end_time=datetime.date.today().strftime("%Y-%m-%d %H:%M:%S"),
-                                                            cnt=1,
-                                                            app_version=app_version)
-
-                    # 5、video_info 合并
-                    full_video_info = []
-                    if general_video_info['code'] == 0:
-                        full_video_info.extend(general_video_info['data'])
-                    if special_video_info['code'] == 0:
-                        full_video_info.extend(special_video_info['data'])
-
-                    # 6、拼装request_data,调用ai_strategy_request_parse方法
-                    # 广告计划名称:渠道包 - 游戏名称 - 版位 - 日期
-                    # 广告组名称:渠道包 - 游戏名称 - 优选 - 不限(定向)-自定义 / 程序化 - 日期
-                    if full_video_info:
-                        request_data = get_request_data(account_id=account_id,
-                                                        campaign_id=campaign_id,
-                                                        video_info=full_video_info,
-                                                        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))
-                        app_id_cnt += 1
-                    if app_id_cnt >= high_quality_video_app_id:
-                        break
+                # 单独处理返回的时候中,有通用视频,需要单独处理
+                # "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)
+                else:
+                    logger.info("account_id=%s, 高质量素材复建: 没有获取到视频" % account_id)
                 self.write(json.dumps({"message": "account_id=%s, 多应用的高质量素材复建请求已走完!" % account_id}))
                 self.flush()
             else:
@@ -462,60 +515,92 @@ class AiCheckAndUpTOFullCreative(tornado.web.RequestHandler):
                               """ % account_id
                 df = pd.read_sql(sql, engine)
                 if df['general_track'].values[0] == 0:
-                    needed_app_id_cnt = video_cnt
                     app_id_array = eval(df['app_id_array'].values[0])
-                    random.shuffle(app_id_array)
-                    app_id_cnt = 0
-                    for app_id in app_id_array:
-                        campaign_id = None
-                        # 1、获取该 app_id 的应该详情
-                        app_id_info = get_app_detail(account_id=account_id,
-                                                     url=get_app_detail_url,
-                                                     app_id=app_id)
-                        if app_id_info['code'] != 0:
-                            continue
-
-                        # 2、依据 appVersion 的值,来判断当天是否已经有对应的计划,如果有则获取 campaign_id, 没有则设置 campaign_name
-                        app_version = app_id_info['data'].get('appVersion', 'error')
-                        track_url = app_id_info['data'].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
-
-                        # 3、获取app_id对应的1个视频
-                        special_video_info = 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=1,
-                                                                app_version=app_version)
-                        if special_video_info['code'] != 0:
-                            continue
-
-                        # 6、拼装request_data,调用ai_strategy_request_parse方法
-                        # 广告计划名称:渠道包 - 游戏名称 - 版位 - 日期
-                        # 广告组名称:渠道包 - 游戏名称 - 优选 - 不限(定向)-自定义 / 程序化 - 日期
-                        request_data = get_request_data(account_id=account_id,
-                                                        campaign_id=campaign_id,
-                                                        video_info=special_video_info['data'],
-                                                        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))
-                        app_id_cnt += 1
-                        if app_id_cnt >= needed_app_id_cnt:
-                            break
-                    self.write(json.dumps({"message": "account_id=%s, 多应用的高质量素材补满创意的请求已走完!" % account_id}))
-                    self.flush()
+                    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
+                    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=video_cnt,
+                                                    app_version=None)
+
+                    # 单独处理返回的时候中,有通用视频,需要单独处理
+                    # "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:
                     # 1-1 获取当天计划中包含“高质量素材”计划的,计划id
                     sql = """
@@ -532,8 +617,10 @@ class AiCheckAndUpTOFullCreative(tornado.web.RequestHandler):
 
                     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"),
+                                                    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)
@@ -603,61 +690,121 @@ class AiProgramCreativeHighQualityMaterial(tornado.web.RequestHandler):
             df = pd.read_sql(sql, engine)
             if df['general_track'].values[0] == 0:
                 app_id_array = eval(df['app_id_array'].values[0])
-                random.shuffle(app_id_array)
-                app_id_cnt = 0
-                for app_id in app_id_array:
-                    campaign_id = None
-                    # 1、获取该 app_id 的应该详情
-                    app_id_info = get_app_detail(account_id=account_id,
-                                                 url=get_app_detail_url,
-                                                 app_id=app_id)
-                    if app_id_info['code'] != 0:
-                        continue
-
-                    # 2、依据 appVersion 的值,来判断是否已经有对应的计划,如果有则获取 campaign_id,没有则设置 campaign_name
-                    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_version = app_id_info['data'].get('appVersion', 'error')
-                    track_url = app_id_info['data'].get('trackUrl', 'error')
-
-                    # 2、获取特定视频
-                    special_video_info = get_top_video_info(account_id=account_id,
-                                                            url=get_high_quality_video_url,
-                                                            start_time=(datetime.datetime.now() +
-                                                                        datetime.timedelta(days=-high_quality_video_days)).
-                                                            strftime("%Y-%m-%d %H:%M:%S"),
-                                                            end_time=datetime.date.today().strftime("%Y-%m-%d %H:%M:%S"),
-                                                            cnt=5,
-                                                            app_version=app_version)
-                    if special_video_info['code'] != 0:
-                        continue
-
-                    # 3、拼装request_data,调用ai_strategy_request_parse方法
-                    # 广告计划名称:渠道包 - 游戏名称 - 版位 - 日期
-                    # 广告组名称:渠道包 - 游戏名称 - 优选 - 不限(定向)-自定义 / 程序化 - 日期
-                    request_data = get_request_data(account_id=account_id,
-                                                    campaign_id=campaign_id,
-                                                    video_info=special_video_info['data'],
-                                                    campaign_name=app_version + '--优选广告位-' + '程序化高质量素材',
-                                                    group_name=app_version + '-优选广告位-不限-程序化-' + '高质量素材',
-                                                    ai_strategy_remark="程序化高质量素材",
-                                                    unit_type=7,
-                                                    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))
-                    app_id_cnt += 1
-                    if app_id_cnt >= 30:
-                        break
+                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
+                video_list = get_top_video_info(account_id=account_id,
+                                                cnt=multi_app_id_programme_high_quality_video_cnt,
+                                                start_time=(datetime.datetime.now() + datetime.timedelta(
+                                                 days=-high_quality_video_days)).strftime("%Y-%m-%d %H:%M:%S"),
+                                                end_time=datetime.date.today().strftime("%Y-%m-%d %H:%M:%S"),
+                                                url=get_high_quality_video_url)
 
-                self.write(json.dumps({"message": "account_id=%s, 多应用的高质量素材程序化创意的请求已走完!" % account_id}))
-                self.flush()
+                # 单独处理返回的时候中,有通用视频,需要单独处理
+                # "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=7,
+                                                            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']
+                    general_video_list = []
+                    for k, v in general_video_df.T.to_dict().items():
+                        general_video_list.append(v)
+                    if general_video_list:
+                        app_version_list = list(app_dict)
+                        random.shuffle(app_version_list)
+                        # 每5个通用视频创建一个程序化广告组
+                        batch_num = 5
+                        app_id_cnt = 0
+                        for i in range(0, len(general_video_list), 5):
+                            if i + 5 < len(general_video_list):
+                                batch_general_video = general_video_list[i: i+batch_num]
+                                app_version = app_version_list[app_id_cnt]
+                                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
+
+                                request_data = get_request_data(account_id=account_id,
+                                                                campaign_id=campaign_id,
+                                                                video_info=batch_general_video,
+                                                                campaign_name=app_version + '--优选广告位-' + '程序化高质量素材',
+                                                                group_name=app_version + '-优选广告位-不限-程序化-' + '高质量素材',
+                                                                ai_strategy_remark="程序化高质量素材",
+                                                                unit_type=7,
+                                                                app_id=app_id,
+                                                                click_track_url=track_url)
+                                request = ai_strategy_request_parse(request_data)
+                                app_id_cnt += 1
+                                logger.info("account_id=%s, app_id=%s, 程序化高质量素材-通用视频,ai策略的返回信息:%s" % (account_id, app_id, request))
+                            else:
+                                batch_general_video = general_video_list[i: len(general_video_list)]
+                                app_version = app_version_list[app_id_cnt]
+                                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
+
+                                request_data = get_request_data(account_id=account_id,
+                                                                campaign_id=campaign_id,
+                                                                video_info=batch_general_video,
+                                                                campaign_name=app_version + '--优选广告位-' + '程序化高质量素材',
+                                                                group_name=app_version + '-优选广告位-不限-程序化-' + '高质量素材',
+                                                                ai_strategy_remark="程序化高质量素材",
+                                                                unit_type=7,
+                                                                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))
+                    else:
+                        logger.info("account_id=%s, 程序化高质量素材: 返回的视频中没有通用视频" % account_id)
+                    self.write(json.dumps({"message": "account_id=%s, 程序化高质量素材请求已走完!" % account_id}))
+                    self.flush()
             else:
                 video_info = get_top_video_info(account_id=account_id,
                                                 cnt=programme_high_quality_video_cnt,
@@ -718,62 +865,120 @@ class AiProgramCreativeNewMaterial(tornado.web.RequestHandler):
             df = pd.read_sql(sql, engine)
             if df['general_track'].values[0] == 0:
                 app_id_array = eval(df['app_id_array'].values[0])
-                random.shuffle(app_id_array)
-                app_id_cnt = 0
-                for app_id in app_id_array:
-                    campaign_id = None
-                    # 1、获取该 app_id 的应该详情
-                    app_id_info = get_app_detail(account_id=account_id,
-                                                 url=get_app_detail_url,
-                                                 app_id=app_id)
-                    if app_id_info['code'] != 0:
-                        continue
-
-                    # 2、依据 appVersion 的值,来判断是否已经有对应的计划,如果有则获取 campaign_id,没有则设置 campaign_name
-                    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_version = app_id_info['data'].get('appVersion', 'error')
-                    track_url = app_id_info['data'].get('trackUrl', 'error')
-
-                    # 2、获取特定视频
-                    special_video_info = get_history_video_info(account_id=account_id,
-                                                                url=get_history_video_url,
-                                                                start_time=(datetime.datetime.now() + datetime.timedelta(days=-60)).
-                                                                strftime("%Y-%m-%d %H:%M:%S"),
-                                                                end_time=datetime.date.today().strftime("%Y-%m-%d %H:%M:%S"),
-                                                                video_cnt=5,
-                                                                related_creative_max_cnt=related_creative_max_cnt,
-                                                                app_version=app_version
-                                                                )
-                    if special_video_info['code'] != 0:
-                        continue
-
-                    # 3、拼装request_data,调用ai_strategy_request_parse方法
-                    # 广告计划名称:渠道包 - 游戏名称 - 版位 - 日期
-                    # 广告组名称:渠道包 - 游戏名称 - 优选 - 不限(定向)-自定义 / 程序化 - 日期
-                    request_data = get_request_data(account_id=account_id,
-                                                    campaign_id=campaign_id,
-                                                    video_info=special_video_info['data'],
-                                                    campaign_name=app_version + '--优选广告位-' + '程序化上新素材',
-                                                    group_name=app_version + '-优选广告位-不限-程序化-' + '上新素材',
-                                                    ai_strategy_remark="程序化上新素材",
-                                                    unit_type=7,
-                                                    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))
-                    app_id_cnt += 1
-                    if app_id_cnt >= 30:
-                        break
+                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
+                video_list = get_history_video_info(account_id=account_id,
+                                                    url=get_history_video_url,
+                                                    start_time=(datetime.datetime.now() + datetime.timedelta(days=-7)).
+                                                    strftime("%Y-%m-%d %H:%M:%S"),
+                                                    end_time=datetime.date.today().strftime("%Y-%m-%d %H:%M:%S"),
+                                                    video_cnt=multi_app_id_programme_new_video_cnt,
+                                                    related_creative_max_cnt=related_creative_max_cnt)
 
-                self.write(json.dumps({"message": "account_id=%s, 多应用的上新素材程序化创意的请求已走完!" % account_id}))
-                self.flush()
+                # 单独处理返回的时候中,有通用视频,需要单独处理
+                # "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=7,
+                                                            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']
+                    general_video_list = []
+                    for k, v in general_video_df.T.to_dict().items():
+                        general_video_list.append(v)
+                    if general_video_list:
+                        app_version_list = list(app_dict)
+                        random.shuffle(app_version_list)
+                        # 每5个通用视频创建一个程序化广告组
+                        batch_num = 5
+                        app_id_cnt = 0
+                        for i in range(0, len(general_video_list), 5):
+                            if i + 5 < len(general_video_list):
+                                batch_general_video = general_video_list[i: i + batch_num]
+                                app_version = app_version_list[app_id_cnt]
+                                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
+
+                                request_data = get_request_data(account_id=account_id,
+                                                                campaign_id=campaign_id,
+                                                                video_info=batch_general_video,
+                                                                campaign_name=app_version + '--优选广告位-' + '程序化上新素材',
+                                                                group_name=app_version + '-优选广告位-不限-程序化-' + '上新素材',
+                                                                ai_strategy_remark="程序化上新素材",
+                                                                unit_type=7,
+                                                                app_id=app_id,
+                                                                click_track_url=track_url)
+                                request = ai_strategy_request_parse(request_data)
+                                app_id_cnt += 1
+                                logger.info("account_id=%s, app_id=%s, 程序化上新素材-通用视频,ai策略的返回信息:%s" % (account_id, app_id, request))
+                            else:
+                                batch_general_video = general_video_list[i: len(general_video_list)]
+                                app_version = app_version_list[app_id_cnt]
+                                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
+
+                                request_data = get_request_data(account_id=account_id,
+                                                                campaign_id=campaign_id,
+                                                                video_info=batch_general_video,
+                                                                campaign_name=app_version + '--优选广告位-' + '程序化上新素材',
+                                                                group_name=app_version + '-优选广告位-不限-程序化-' + '上新素材',
+                                                                ai_strategy_remark="程序化上新素材",
+                                                                unit_type=7,
+                                                                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))
+                    else:
+                        logger.info("account_id=%s, 程序化上新素材: 返回的视频中没有通用视频" % account_id)
+                    self.write(json.dumps({"message": "account_id=%s, 程序化上新素材请求已走完!" % account_id}))
+                    self.flush()
             else:
                 video_info = get_history_video_info(account_id=account_id,
                                                     url=get_history_video_url,

+ 2 - 2
time_task/time_task_ai_high_quality_material_test.py

@@ -32,8 +32,8 @@ print(acc_list)
 # 233账号: 9864909 登陆账号: jy20201254@163.com 密码:jysz12345
 # 消消消账号: 6020747 登陆账号: 15715158532 密码:xxx@112233
 # 斗地主账号:8067888  登陆账号: 13699377642 密码:yg@12345
-# acc_list = [9792538, 9864909, 6020747, 8067888]
-acc_list = [9881494,9881495,9881496,9881497,9881498,9881499,  9881500,9881502,9881504,9881505,9881506,9881507]
+acc_list = [9792538, 9864909, 6020747, 8067888]
+# acc_list = [9881494,9881495,9881496,9881497,9881498,9881499,  9881500,9881502,9881504,9881505,9881506,9881507]
 print(acc_list)
 
 # 2、 5个账户一组进行发送请求

+ 115 - 8
utils/CommonFunction.py

@@ -27,6 +27,49 @@ def get_lower_case_name(name):
     return "".join(lst).lower()
 
 
+def get_app_list(account_id,url):
+    """
+    获取应用详情
+    url:http://192.168.1.8:8080/jeecg-boot/ai/aiKuaiShouAppInfo/list
+    请求方式:post
+    入参:accountId 账户id
+         appId     应用id
+    返回参数:
+     {
+        "code": 0,
+        "data": [{
+        "id": 413,
+        "accountId": 9864909,
+        "appVersion": "ksjyrlyn-人类一脑涂地大败",
+        "appName": "233乐园",
+        "packageName": "com.meta.box",
+        "platform": 1,
+        "url": "https://www.233leyuan.com/apiserv/api/deliveryTest/ABTest?id=ksjyrlyn",
+        "useSdk": 0,
+        "appPrivacyUrl": "https://webcdn.233leyuan.com/app/useragreement/metax/privacyagreement.html",
+        "trackUrl": "https://lnk0.com/kYZxds?chn=451&imei_md5=__IMEI2__&androidid_md5_1=__ANDROIDID2__&oaid=__OAID__&adcampaign=__DNAME__&adcreative=__CID__&adgroup=__AID__&did=__DID__&accountid=__ACCOUNTID__&platform=__OS__&clicktime=__TS__&ip=__IP__&useragent=__UA__&channelapp=__CSITE__&callback_url=__CALLBACK__&action=none",
+        "appMd5": "0b2314c738956dc159ed38dc584ef214",
+        "imageToken": "marketb3e98f5f68a74870b8d8d814499d6189.jpg",
+        "status": 2,
+        "remark": "创建应用成功",
+        "appId": 3543665,
+        "createTime": "2021-02-03 16:40:53",
+        "updateTime": "2021-02-03 16:42:52"}],
+    "message": "success"
+    }
+    """
+    request_data = {"accountId": account_id}
+    try:
+        request = requests.post(url=url, headers=headers, data=json.JSONEncoder().encode(request_data)).text
+        request_json = json.loads(request)
+        if request_json["code"] != 0:
+            logger.info("account_id=%s, the res of list is %s" % (account_id, request_json['message']))
+    except:
+        logger.error('account_id=%s, the res of list is %s, the traceback is %s ' %
+                     (account_id, request, traceback.format_exc()))
+        return {"code": -1}
+    return request_json
+
 def get_app_detail(account_id, url, app_id):
     """
     获取应用详情
@@ -193,21 +236,85 @@ def get_top_video_info(account_id, cnt, start_time, end_time, url, app_version=N
         startTime: 开始时间
         endTime:   结束时间
         num: 查询数量
+    remark: 返回结果中 "photo_name" == "all",表示为通用视频,反之为app_id对应的特定视频("photo_name": "ksjytdrca-糖豆人冲啊")
     返回:
-    {
+   {
     "code": 0,
     "data": [
         {
-            "charge": 40871.123,
-            "video_url": "&di=811c96e5&bp=13890",
-            "photo_id": "5254011957394409468",
-            "signature": "4af22090cddc32773c7707de3c652f77",
+            "material_type": 1,
+            "charge": 154229.670,
+            "video_url": "http://alimov2.a.yximgs.com/upic/2021/02/03/18/BMjAyMTAyMDMxODU3MjlfMjI0NTY2OTI5MF80MzUxMjc4MTgzNV8wXzM=_b_B142e1ddc44705f978fe2a7fa4f4e9eb1.mp4?tag=1-1613958443-unknown-0-nbzw9am7cu-465420c6c3fe0f27&clientCacheKey=3xeh2gxd84qg462_b.mp4&tt=b&di=8bba1b60&bp=13890",
+            "photo_id": "5249789835088957477",
+            "photo_name": "ksjytdrca-糖豆人冲啊",
+            "signature": "e160cc10d73031a48895a32834b54c46",
             "channel_type": 0,
-            "material_type":1, --1:竖版 2:横版
-            "imageList": ["09f61ca7174160c12d9a68306049ca82","46996bea84287692b3884686a52903ed"]
+            "imageList": [
+                "3267a05ef40ca8180b87428bcc324628",
+                "1e5cb59e588a6dd489a9c0c36b7e52cd",
+                "c0a199395614c5204a2f9a5aa9457fdf",
+                "659c1865be2e9b561475d204ebf363b4",
+                "7b85a1262a59e22ff2e82a3374d8beb0",
+                "2e5542b2b39191219c33e278ccae0bdf",
+                "45d2d11e052aca20c2a5e546e2ffda30",
+                "8902152d887dfc119a2ba32d970683d9",
+                "7becab0d75b2cbec66ea18fbabd33b59",
+                "e1ba0f57d3abe2822ab3fc02d9ea7ba3",
+                "9676c587837df85df4aba76a5ce3b8c1",
+                "1b42543b0b31613740dfaa8fd861136c",
+                "8a1c95ecf4af672d2f7a1acef36bce15",
+                "5e6d8d8f0339ab7d2b3e1de533111606",
+                "8b5b6c6d63e62e04a92d84fe175c05fd"
+            ]
+        },
+        {
+            "material_type": 1,
+            "charge": 92135.125,
+            "video_url": "http://txmov2.a.yximgs.com/upic/2021/02/03/18/BMjAyMTAyMDMxODU3MzhfMjI0NTY2OTI5MF80MzUxMjc5MzU0MV8wXzM=_b_Bd0dab20fe03c02e76080340c66341573.mp4?tag=1-1613958443-unknown-0-9romi5xmpo-d5161ea63e200cde&clientCacheKey=3xu33qupdka3h5y_b.mp4&tt=b&di=8bba1b60&bp=13890",
+            "photo_id": "5192368941898812785",
+            "photo_name": "all",
+            "signature": "fc4004dc82f44f765aa7a0150e3aad8a",
+            "channel_type": 0,
+            "imageList": [
+                "8cc3de1612ced3a6dbdf8e9e054c6ec1",
+                "7a2e7c070c25e6d06460e9fe62f139c7",
+                "43dcb78211d4d6d8bdb5a398a57d9d1d",
+                "14118f0ecd4a86e35165e6130ba69c6b",
+                "eec697ea33e3ddaaf0e77e65749a6f23",
+                "da6e9648da85204c717e8beb87b19328",
+                "0839fffc60203f1ba13bc89c26d437c4",
+                "feabdeaab2e264e521be06c5fc4c0457"
+            ]
+        },
+        {
+            "material_type": 1,
+            "charge": 70956.941,
+            "video_url": "http://txmov2.a.yximgs.com/upic/2021/02/03/18/BMjAyMTAyMDMxODIwMzdfMjI0NTY2OTI5MF80MzUxMDIxMDg2OF8wXzM=_b_Bfea5219f9c40bdf34f8420429c59434c.mp4?tag=1-1613958443-unknown-0-rk05fpbcb3-7dbb4cb30a941643&clientCacheKey=3x3xi88nniq6m6i_b.mp4&tt=b&di=8bba1b60&bp=13890",
+            "photo_id": "5219390536383350388",
+            "photo_name": "all",
+            "signature": "0ec9b0bac40463d18f151be605682dc8",
+            "channel_type": 0,
+            "imageList": [
+                "9b71f206b6ca9d2f89712b79ebd0d699",
+                "c802325b325cf67c5c7c13872572642d",
+                "e79ca1b5dde8709e794b53582e40dbdd",
+                "37b529460cbebcba259a412dce40bd9c",
+                "b7b37d27e33d21923c7ade475e7d0ffb",
+                "b4ebac5da9e624aae208f2d8d9ab6b0e",
+                "0675ef5a12e9ed01954e40309ff226c1",
+                "04ed3651b233aad5a1f7e76b1ead48e4",
+                "6a954e0ecfce38ad854c715c4e0d3a26",
+                "516a957c2c80a4a74a24d1073412647f",
+                "95f1a42d06657c4b8b8554b81583db5f",
+                "30ba9f8a90539f3afe43f3046c54f488",
+                "e48236f0509398412efac2f9ee258c6d",
+                "a9f21f3731b653784fcf2dca6d4ca427",
+                "014383e5b7c33cb9e0a515bf11e9a786"
+            ]
         }
-        ],
+    ],
     "message": "SUCCESS"
+    }
     """
 
     if not app_version:

+ 12 - 2
utils/ConstantConfig.py

@@ -8,7 +8,7 @@ if os_env_code_status == 'dev':
     print("测试环境的 常量信息!")
     # 近多少天的爆量视频
     high_quality_video_days = 140
-    high_quality_video_cnt = 2
+    high_quality_video_cnt = 10
 
     # 对于多应用使用多少个app_id对应高质量视频
     high_quality_video_app_id = 2
@@ -23,9 +23,13 @@ if os_env_code_status == 'dev':
     missing_video_app_id = 2
 
     # 程序化创意跑量视频个数
-    programme_high_quality_video_cnt = 13
+    programme_high_quality_video_cnt = 30
     # 程序化创意上新视频个数
     programme_new_video_cnt = 13
+    # 多应用的程序化跑量视频个数-- 程序化组个数的上限是60个,跑量的分30个组
+    multi_app_id_programme_high_quality_video_cnt = 30
+    # 多应用的上新视频个数-- 程序化组个数的上限是60个,上新的分30个组
+    multi_app_id_programme_new_video_cnt = 30
     # 调用获取历史视频接口时,关联的最大创意个数
     related_creative_max_cnt = 10
 
@@ -51,7 +55,13 @@ else:
     programme_high_quality_video_cnt = 150
     # 程序化创意上新视频个数
     programme_new_video_cnt = 150
+
+    # 多应用的程序化跑量视频个数-- 程序化组个数的上限是60个,跑量的分30个组
+    multi_app_id_programme_high_quality_video_cnt = 30
+    # 多应用的上新视频个数-- 程序化组个数的上限是60个,上新的分30个组
+    multi_app_id_programme_new_video_cnt = 30
     # 调用获取历史视频接口时,关联的最大创意个数
+
     related_creative_max_cnt = 10
 
 

+ 4 - 0
utils/UrlConfig.py

@@ -18,6 +18,8 @@ if os_env_code_status == 'dev':
     get_high_quality_video_url = "http://192.168.1.8:8080/jeecg-boot/kuaishou/material/getHistoryTopVideoList"
     # 获取应用详情
     get_app_detail_url = "http://192.168.1.8:8080/jeecg-boot/ai/aiKuaiShouAppInfo/getAppDetail"
+    # 获取应用列表
+    get_app_list_url = "http://192.168.1.8:8080/jeecg-boot/ai/aiKuaiShouAppInfo/list"
     # 刷新快手素材列表
     refresh_video_url = "http://192.168.1.8:8080/jeecg-boot/kuaishou/material/getKuaiShouVideoList"
 else:
@@ -36,5 +38,7 @@ else:
     get_high_quality_video_url = "http://api.tjyourong.com.cn/jeecg-boot/kuaishou/material/getHistoryTopVideoList"
     # 获取应用详情
     get_app_detail_url = "http://api.tjyourong.com.cn/jeecg-boot/ai/aiKuaiShouAppInfo/getAppDetail"
+    # 获取应用列表
+    get_app_list_url = "http://api.tjyourong.com.cn/jeecg-boot/ai/aiKuaiShouAppInfo/list"
     # 刷新快手素材列表
     refresh_video_url = "http://api.tjyourong.com.cn/jeecg-boot/kuaishou/material/getKuaiShouVideoList"