liyuyi@c-top.com.cn 4 lat temu
rodzic
commit
9f47bed4a3

+ 37 - 28
ai_target_combine_handler.py

@@ -40,35 +40,43 @@ class AiTargetCombine(tornado.web.RequestHandler):
         try:
             # 1、创建类实例
             ins = GetTargetAndAssemblyParameters(account_id, project_id)
-            # 2、判断当天是否已经存在包含【优质定向】的广告计划,如果有则获取该计划id,否则需要新建计划
-            sql = """select campaign_id, campaign_name from ctop_kuaishou_campaign 
-                    where account_id = %s
-                    and put_create_time >= curdate()
-                    and campaign_name like '%s'""" % (ins.account_id, '%%优质定向%%')
-            df = pd.read_sql(sql, ins.engine)
-            if df.empty:
-                ins.add_campaign()
-            else:
-                ins.campaign_id = df['campaign_id'].values[0]
-
-            # 3、拼装广告组和创意的参数
-            ins.assembly_group_and_creative_params()
-
-            # 4、信息写如到智能策略表中
-            ins.write_intelligence_strategy_table()
-
-            # 5、发送创建组和创意的请求
-            request = requests.post(create_group_and_creative_url,
-                                    headers=headers,
-                                    data=json.dumps(ins.request_data, cls=NpEncoder))
-            response_data = json.loads(request.text)
 
-            # 6、接口的返回信息更新到数据库表
-            ins.update_intelligence_strategy_table(message=response_data)
-
-            logger.info("account_id = %s, 策略uuid = %s 的请求返回信息:%s" % (ins.account_id, ins.ai_strategy_uuid, response_data))
-            self.write(json.dumps({"account_id": account_id, "message": str(response_data), "code": 1}))
-            self.flush()
+            # 2、判断final_target_combine是否为空,为空则不执行后续的操作
+            if not ins.final_target_combine:
+                self.write(json.dumps({"account_id": account_id, "message": "没有合适的定向组合", "code": 1}))
+                self.flush()
+            else:
+                # 2、判断当天是否已经存在包含【优质定向】的广告计划,如果有则获取该计划id,否则需要新建计划
+                sql = """select campaign_id, campaign_name from ctop_kuaishou_campaign 
+                        where account_id = %s
+                        and put_create_time >= curdate()
+                        and campaign_name like '%s'""" % (ins.account_id, '%%优质定向%%')
+                df = pd.read_sql(sql, ins.engine)
+                if df.empty:
+                    ins.add_campaign()
+                else:
+                    ins.campaign_id = df['campaign_id'].values[0]
+
+                # 3、拼装广告组和创意的参数
+                ins.assembly_group_and_creative_params()
+
+                # 4、信息写如到智能策略表中
+                ins.write_intelligence_strategy_table()
+
+                # 5、
+
+                # 5、发送创建组和创意的请求
+                request = requests.post(create_group_and_creative_url,
+                                        headers=headers,
+                                        data=json.dumps(ins.request_data, cls=NpEncoder))
+                response_data = json.loads(request.text)
+
+                # 6、接口的返回信息更新到数据库表
+                ins.update_intelligence_strategy_table(message=response_data)
+
+                logger.info("account_id = %s, 策略uuid = %s 的请求返回信息:%s" % (ins.account_id, ins.ai_strategy_uuid, response_data))
+                self.write(json.dumps({"account_id": account_id, "message": str(response_data), "code": 1}))
+                self.flush()
         except:
             logger.error("account_id = %s, traceback is %s" % (account_id, traceback.format_exc()))
             self.write(json.dumps({"account_id": account_id, "message": str(traceback.format_exc()), "code": -1}))
@@ -137,6 +145,7 @@ class GetTargetAndAssemblyParameters(object):
         # 1、请求创建广告计划的接口
         campaign_name = self.advertiser_strategy['campaign_name']
         campaign_name = campaign_name.replace('{{自定义}}', '')
+        campaign_name = campaign_name.replace('{{日期}}', '')
         campaign_name += '_优质定向_'
         campaign_name += str(datetime.datetime.now().date())
         create_campaign_request_data = {'account_id': self.account_id,

+ 1 - 1
time_task/time_task_create_ad_by_target.py

@@ -59,7 +59,7 @@ account_df.drop_duplicates('account_id', keep='first', inplace=True)
 acc_list = [(item['account_id'], item['project_id']) for index, item in account_df.iterrows()]
 
 # for test
-acc_list = [(9774238, 458)] * 1
+acc_list = [(10456827, 458)] * 1
 
 # 2、 5个账户一组进行发送请求
 batch_num = 5

+ 4 - 1
utils/commonFunc.py

@@ -36,12 +36,15 @@ def is_contains_region(val1, val2):
     if val1 is None:
         return True, {'region': val2}
 
+    val_split = val1.split('|')
+    val_tuple = tuple(val_split*2) if len(val_split) == 1 else tuple(val_split)
     sql = """
                 select t1.city_name, t2.level, t2.region_id, t2.parent from ctop_kuaishou_city_level t1
                 left join ctop_kuaishou_region_list_parent t2
                 on t1.city_name = t2.name
                 where t1.city_level in %s
-                """ % (tuple(val1.split('|')),)
+                """ % (val_tuple,)
+    print(sql)
     with open('config/config.yaml', mode='r', encoding='utf-8') as f:
         config = yaml.load(f.read(), Loader=yaml.FullLoader)