import pandas as pd from utils.UrlConfig import * import json import logging import traceback import tornado.web from concurrent_log import ConcurrentTimedRotatingFileHandler # from logging.handlers import TimedRotatingFileHandler 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 from utils.ConstantConfig import * from ai_strategy_request_func import ai_strategy_request_parse from utils.DataBaseConfig import * log_formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s', '%m/%d/%Y %I:%M:%S %p') log_handler = ConcurrentTimedRotatingFileHandler("logs/ai_auto_create.log", when="midnight", backupCount=100) log_handler.setFormatter(log_formatter) logger = logging.getLogger('ai_time_task_creative_logger') logger.addHandler(log_handler) logger.setLevel(logging.DEBUG) print('id of ai_time_task_creative_logger %s' % id(logger)) logger.info("ai_time_task_creative_server started!") class AiHistoricalMissingMaterial(tornado.web.RequestHandler): """ 补充历史遗漏素材(关联创意个数为0个素材) -- 测试通过 在AD后台创建成功,检查AD信息和数据库表 没有问题 """ def post(self): data = self.request.body data = str(data, 'utf8') data = json.loads(data, encoding='utf8') logger.info("*************************************** NEW REQUEST ***************************************") account_id = data.get('account_id') campaign_id = data.get('campaign_id') logger.info("账户信息=%s, 当前进程=%s, 当前进程的主进程=%s, raw data from request is %s" % (account_id, os.getpid(), os.getppid(), data)) try: video_info = get_missing_video_info(account_id, missing_video_cnt, get_missing_video_url, missing_video_start_time, missing_video_end_time) if video_info['code'] != 0: logger.info("the res of video is %s" % video_info['message']) logger.info("没有获取到视频,不发送ai策略请求!") self.write(json.dumps({"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("没有获取到广告计划命名规则 或 广告组命名规则,不发送ai策略请求!") self.write(json.dumps({"message": "没有获取到广告计划命名规则 或 广告组命名规则,不发送ai策略请求!"})) self.flush() else: request_data = get_request_data(account_id=account_id, campaign_id=campaign_id, video_info=video_info, campaign_name=campaign_name, group_name=group_name, ai_strategy_remark="补充遗漏素材", name_replace="补充遗漏素材") request = ai_strategy_request_parse(request_data) logger.info("ai策略的返回信息:%s" % request) self.write(json.dumps(request)) self.flush() except Exception: logger.error(traceback.format_exc()) self.write(json.dumps(traceback.format_exc())) self.flush() class AiAutoCreative(tornado.web.RequestHandler): """ 每5分钟,检查一次是否有上新素材,有的话,就自动创建 -- 需要上定时任务测试 """ def post(self): data = self.request.body data = str(data, 'utf8') data = json.loads(data, encoding='utf8') logger.info("*************************************** NEW REQUEST ***************************************") account_id = data.get('account_id') campaign_id = data.get('campaign_id') logger.info("账户信息=%s, 当前进程=%s, 当前进程的主进程=%s, raw data from request is %s" % (account_id, os.getpid(), os.getppid(), data)) try: start_time = (datetime.date.today()).strftime('%Y-%m-%d %H:%M:%S') end_time = (datetime.datetime.now()).strftime('%Y-%m-%d %H:%M:%S') # 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_new_video_info(account_id, get_new_video_url) if video_info['code'] != 0: logger.info("the res of video is %s" % video_info['message']) logger.info("没有获取到视频,不发送ai策略请求!") self.write(json.dumps({"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("没有获取到广告计划命名规则 或 广告组命名规则,不发送ai策略请求!") self.write(json.dumps({"message": "没有获取到广告计划命名规则 或 广告组命名规则,不发送ai策略请求!"})) self.flush() else: request_data = get_request_data(account_id=account_id, campaign_id=campaign_id, video_info=video_info, campaign_name=campaign_name, group_name=group_name, ai_strategy_remark="自动上新", name_replace="素材自动上新") request = ai_strategy_request_parse(request_data) logger.info("ai策略的返回信息:%s" % request) self.write(json.dumps(request)) self.flush() except Exception: logger.error(traceback.format_exc()) self.write(json.dumps(traceback.format_exc())) self.flush() class AiHighQualityMaterial(tornado.web.RequestHandler): """ 补充历史高质量素材 -- 测试通过 在AD后台创建成功,检查AD信息和数据库表 没有问题 """ def post(self): data = self.request.body data = str(data, 'utf8') data = json.loads(data, encoding='utf8') logger.info("*************************************** NEW REQUEST ***************************************") account_id = data.get('account_id') campaign_id = data.get('campaign_id') logger.info("账户信息=%s, 当前进程=%s, 当前进程的主进程=%s, raw data from request is %s" % (account_id, os.getpid(), os.getppid(), data)) try: video_info = get_top_video_info(account_id, high_quality_video_cnt, (datetime.datetime.now() + datetime.timedelta(days=-high_quality_video_days)).strftime("%Y-%m-%d"), str(datetime.date.today()), get_high_quality_video_url) if video_info['code'] != 0: logger.info("the res of video is %s" % video_info['message']) logger.info("没有获取到视频,不发送ai策略请求!") self.write(json.dumps({"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("没有获取到广告计划命名规则 或 广告组命名规则,不发送ai策略请求!") self.write(json.dumps({"message": "没有获取到广告计划命名规则 或 广告组命名规则,不发送ai策略请求!"})) self.flush() else: request_data = get_request_data(account_id=account_id, campaign_id=campaign_id, video_info=video_info, campaign_name=campaign_name, group_name=group_name, ai_strategy_remark="高质量素材复建", name_replace="高质量素材") request = ai_strategy_request_parse(request_data) logger.info("ai策略的返回信息:%s" % request) self.write(json.dumps(request)) self.flush() except Exception: logger.error(traceback.format_exc()) self.write(json.dumps(traceback.format_exc())) self.flush() class AiCheckAndUpTOFullCreative(tornado.web.RequestHandler): """ 每天22:00 调用该任务,检查当天创意是否创建达到上限,否则使用历史高质量素材填满 -- 测试通过 在AD后台创建成功,检查AD信息和数据库表 没有问题 """ def post(self): data = self.request.body data = str(data, 'utf8') data = json.loads(data, encoding='utf8') logger.info("*************************************** NEW REQUEST ***************************************") account_id = data.get('account_id') campaign_id = data.get('campaign_id') logger.info("账户信息=%s, 当前进程=%s, 当前进程的主进程=%s, raw data from request is %s" % (account_id, os.getpid(), os.getppid(), data)) try: start_time = (datetime.date.today()).strftime('%Y-%m-%d %H:%M:%S') end_time = (datetime.datetime.now()).strftime('%Y-%m-%d %H:%M:%S') # 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 # 2-1 从客户策略表中获取该账户下 每个素材搭配的封面个数, 来计算需要补充的素材个数 sql = """ select image_cnt from ctop_ai_kuaishou_advertiser_strategy where account_id = %s and status = 1 limit 1 """ % account_id df = pd.read_sql(sql, engine) image_cnt = int(df['image_cnt'].values[0]) # 3-1 从 ctop_kuaishou_creative 获取当天该账户到此刻 已经创建的创意个数 video_cnt = 0 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, end_time, start_time) df = pd.read_sql(sql, engine) creative_cnt = int(df['creative_cnt'].values[0]) if creative_cnt >= 2000: video_cnt = 0 else: video_cnt = (2000 - creative_cnt) // image_cnt # 4-1 获取视频信息 if video_cnt == 0: logger.info("创意已经建满,不需要补充素材!") self.write(json.dumps("message: 创意已经建满,不需要补充素材!")) self.flush() else: logger.info("需要补充的视频数量:%s" % video_cnt) now = datetime.datetime.now() video_info = get_top_video_info(account_id, video_cnt, (now + datetime.timedelta(days=-2*high_quality_video_days)).strftime("%Y-%m-%d"), (now + datetime.timedelta(days=-high_quality_video_days)).strftime("%Y-%m-%d"), get_high_quality_video_url) if video_info['code'] != 0: logger.info("the res of video is %s" % video_info['message']) logger.info("没有获取到视频,不发送ai策略请求!") self.write(json.dumps({"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("没有获取到广告计划命名规则 或 广告组命名规则,不发送ai策略请求!") self.write(json.dumps({"message": "没有获取到广告计划命名规则 或 广告组命名规则,不发送ai策略请求!"})) self.flush() else: request_data = get_request_data(account_id=account_id, campaign_id=campaign_id, video_info=video_info, campaign_name=campaign_name, group_name=group_name, ai_strategy_remark="用高质量素材补满创意", name_replace="高质量素材") request = ai_strategy_request_parse(request_data) logger.info("ai策略的返回信息:%s" % request) self.write(json.dumps(request)) self.flush() except Exception: logger.error(traceback.format_exc()) self.write(json.dumps(traceback.format_exc())) self.flush() class AiProgramCreativeHighQualityMaterial(tornado.web.RequestHandler): """ 使用历史跑量素材创建程序化创意 跑量素材150个(近14天的素材) 计划名称--跑量素材程序化 timeTask: 每天上午10点执行 --(0 0 10 * * ?) ---------------------------------------- 拼装参数和写入数据库时,需要修改名称(程序化与非程序化不一致): creative_name --> package_name(程序化创意名称) action_bar_text --> action_bar(行动号召按钮) description --> captions(作品广告语) click_track_url --> click_url 写入数据库时: image_md5s --> cover_image_tokens 程序化与非程序化的逻辑不一样: 需要5个视频4张封面一组,拼装程序化创意参数需要单独抽取一个方法出来 所有操作记录表中,添加status信息(已提交,成功,失败),和回调时的 message信息 """ def post(self): data = self.request.body data = str(data, 'utf8') data = json.loads(data, encoding='utf8') logger.info("*************************************** NEW REQUEST ***************************************") account_id = data.get('account_id') campaign_id = data.get('campaign_id') logger.info("账户信息=%s, 当前进程=%s, 当前进程的主进程=%s, raw data from request is %s" % (account_id, os.getpid(), os.getppid(), data)) try: video_info = get_top_video_info(account_id, programme_high_quality_video_cnt, (datetime.datetime.now() + datetime.timedelta( days=-high_quality_video_days)).strftime("%Y-%m-%d"), str(datetime.date.today()), get_high_quality_video_url) if video_info['code'] != 0: logger.info("the res of video is %s" % video_info['message']) logger.info("没有获取到视频,不发送ai策略请求!") self.write(json.dumps({"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("没有获取到广告计划命名规则 或 广告组命名规则,不发送ai策略请求!") self.write(json.dumps({"message": "没有获取到广告计划命名规则 或 广告组命名规则,不发送ai策略请求!"})) self.flush() else: request_data = get_request_data(account_id=account_id, campaign_id=campaign_id, video_info=video_info, campaign_name=campaign_name, group_name=group_name, ai_strategy_remark="程序化高质量素材", name_replace="程序化高质量素材", unit_type=7) request = ai_strategy_request_parse(request_data) logger.info("ai策略的返回信息:%s" % request) self.write(json.dumps(request)) self.flush() except Exception: logger.error(traceback.format_exc()) self.write(json.dumps(traceback.format_exc())) self.flush() class AiProgramCreativeNewMaterial(tornado.web.RequestHandler): """ 使用近期创意数关联小于10(当天创意关联数小于10的,150个素材)的素材创建程序化创意 计划名称--新素材程序化 timeTask: 每天上午10点执行 --(0 0 10 * * ?) """ def post(self): data = self.request.body data = str(data, 'utf8') data = json.loads(data, encoding='utf8') logger.info("*************************************** NEW REQUEST ***************************************") account_id = data.get('account_id') campaign_id = data.get('campaign_id') logger.info("账户信息=%s, 当前进程=%s, 当前进程的主进程=%s, raw data from request is %s" % (account_id, os.getpid(), os.getppid(), data)) try: video_info = get_history_video_info(account_id=account_id, url=get_history_video_url, start_time=(datetime.datetime.now() + datetime.timedelta(days=-2)). strftime("%Y-%m-%d"), end_time=str(datetime.date.today()), video_cnt=programme_new_video_cnt, related_creative_max_cnt=related_creative_max_cnt) if video_info['code'] != 0: logger.info("the res of video is %s" % video_info['message']) logger.info("没有获取到视频,不发送ai策略请求!") self.write(json.dumps({"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("没有获取到广告计划命名规则 或 广告组命名规则,不发送ai策略请求!") self.write(json.dumps({"message": "没有获取到广告计划命名规则 或 广告组命名规则,不发送ai策略请求!"})) self.flush() else: request_data = get_request_data(account_id=account_id, campaign_id=campaign_id, video_info=video_info, campaign_name=campaign_name, group_name=group_name, ai_strategy_remark="程序化上新素材", name_replace="程序化上新素材", unit_type=7) request = ai_strategy_request_parse(request_data) logger.info("ai策略的返回信息:%s" % request) self.write(json.dumps(request)) self.flush() except Exception: logger.error(traceback.format_exc()) self.write(json.dumps(traceback.format_exc())) self.flush() def get_request_data(account_id, campaign_id, video_info, campaign_name, group_name, ai_strategy_remark, name_replace, unit_type=4): res = { "video": video_info, "operation_type": 1, "account_id": account_id, "ai_strategy_remark": ai_strategy_remark, "campaign_info": {"campaign_id": campaign_id, "campaign_name": campaign_name.replace('自定义', name_replace) + str(datetime.datetime.now())}, 'group_info': {'group_name': group_name.replace('自定义', name_replace) + str(datetime.datetime.now()), 'begin_time': str(datetime.date.today()), 'unit_type': unit_type}, 'creative_info': {'is_sticky': 0} } return res