Explorar o código

1、素材自动上新视频接口调用前调用一个刷新素材的接口 2、所有调用外部接口都加上了try-except来捕获异常的返回信息 3、获取客户策略信息时去掉status的判断,因为一个account_id只有一个配置信息

liyuyi@c-top.com.cn %!s(int64=4) %!d(string=hai) anos
pai
achega
752b92a6d2

+ 1 - 2
ai_strategy_request_func.py

@@ -93,8 +93,7 @@ class ParseAddCampaignOrAddGroupRequest(object):
         sql = """
         select * 
         from ctop_ai_kuaishou_advertiser_strategy 
-        where account_id = %d 
-        and status = 0
+        where account_id = %d
         limit 1
         """ % self.account_id
         advertiser_strategy_df = pd.read_sql(sql, engine)

+ 14 - 15
ai_time_task_creative_handler.py

@@ -24,7 +24,7 @@ logger.info("ai_time_task_creative_server started!")
 
 class AiHistoricalMissingMaterial(tornado.web.RequestHandler):
     """
-    补充历史遗漏素材(关联创意个数为0个素材) -- 测试通过 在AD后台创建成功,检查AD信息和数据库表 没有问题
+    补充历史遗漏素材(关联创意个数为0个素材)
     """
     def post(self):
         data = self.request.body
@@ -38,7 +38,7 @@ class AiHistoricalMissingMaterial(tornado.web.RequestHandler):
         try:
             sql = """
             select account_id, single_appid, app_id_array, general_track from ctop_ai_kuaishou_advertiser_strategy
-            where account_id=%s and status=0 order by create_time desc limit 1
+            where account_id=%s  order by create_time desc limit 1
             """ % account_id
             df = pd.read_sql(sql, engine)
             if df['general_track'].values[0] == 0:
@@ -174,7 +174,7 @@ class AiAutoCreative(tornado.web.RequestHandler):
         try:
             sql = """
                         select account_id, single_appid, app_id_array, general_track from ctop_ai_kuaishou_advertiser_strategy
-                        where account_id=%s and status=0 order by create_time desc limit 1
+                        where account_id=%s order by create_time desc limit 1
                         """ % account_id
             df = pd.read_sql(sql, engine)
             if df['general_track'].values[0] == 0:
@@ -270,15 +270,15 @@ class AiAutoCreative(tornado.web.RequestHandler):
 
                 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.write(json.dumps({"account_id": account_id, "message": "素材自动上新没有获取到视频,不发送ai策略请求!"}))
                     self.flush()
                 else:
                     campaign_name, group_name = get_campaign_and_group_name_rule(account_id)
                     if (campaign_name is None) or (group_name is None):
-                        logger.info("没有获取到广告计划命名规则 或 广告组命名规则,不发送ai策略请求!")
-                        self.write(json.dumps({"message": "没有获取到广告计划命名规则 或 广告组命名规则,不发送ai策略请求!"}))
+                        logger.info("account_id = %s, 素材自动上新 没有获取到广告计划命名规则 或 广告组命名规则,不发送ai策略请求!"
+                                    % account_id)
+                        self.write(json.dumps({"account_id": account_id,
+                                              "message": "素材自动上新 没有获取到广告计划命名规则 或 广告组命名规则,不发送ai策略请求!"}))
                         self.flush()
                     else:
                         request_data = get_request_data(account_id=account_id,
@@ -289,7 +289,7 @@ class AiAutoCreative(tornado.web.RequestHandler):
                                                         ai_strategy_remark="自动上新",
                                                         name_replace="素材自动上新")
                         request = ai_strategy_request_parse(request_data)
-                        logger.info("ai策略的返回信息:%s" % request)
+                        logger.info("account_id = %s, 素材自动上新 ai策略的返回信息:%s" % (account_id, request))
                         self.write(json.dumps(request))
                         self.flush()
         except Exception:
@@ -313,7 +313,7 @@ class AiHighQualityMaterial(tornado.web.RequestHandler):
                     (account_id, os.getpid(), os.getppid(), data))
         try:
             sql = """select account_id, single_appid, app_id_array, general_track from ctop_ai_kuaishou_advertiser_strategy
-                     where account_id=%s and status=0 order by create_time desc limit 1
+                     where account_id=%s  order by create_time desc limit 1
                      """ % account_id
             df = pd.read_sql(sql, engine)
             if df['general_track'].values[0] == 0:
@@ -461,8 +461,7 @@ class AiCheckAndUpTOFullCreative(tornado.web.RequestHandler):
             # 1-1 从客户策略表中获取该账户下 每个素材搭配的封面个数, 来计算需要补充的素材个数
             sql = """
             select image_cnt from ctop_ai_kuaishou_advertiser_strategy 
-            where account_id = %s 
-            and status = 0
+            where account_id = %s
             limit 1
             """ % account_id
             df = pd.read_sql(sql, engine)
@@ -493,7 +492,7 @@ class AiCheckAndUpTOFullCreative(tornado.web.RequestHandler):
                 logger.info("需要补充的视频数量:%s" % video_cnt)
 
                 sql = """select account_id, single_appid, app_id_array, general_track from ctop_ai_kuaishou_advertiser_strategy
-                                 where account_id=%s and status=0 order by create_time desc limit 1
+                                 where account_id=%s order by create_time desc limit 1
                               """ % account_id
                 df = pd.read_sql(sql, engine)
                 if df['general_track'].values[0] == 0:
@@ -643,7 +642,7 @@ class AiProgramCreativeHighQualityMaterial(tornado.web.RequestHandler):
                     (account_id, os.getpid(), os.getppid(), data))
         try:
             sql = """select account_id, single_appid, app_id_array, general_track from ctop_ai_kuaishou_advertiser_strategy
-                     where account_id=%s and status=0 order by create_time desc limit 1
+                     where account_id=%s  order by create_time desc limit 1
                   """ % account_id
             df = pd.read_sql(sql, engine)
             if df['general_track'].values[0] == 0:
@@ -750,7 +749,7 @@ class AiProgramCreativeNewMaterial(tornado.web.RequestHandler):
                     (account_id, os.getpid(), os.getppid(), data))
         try:
             sql = """select account_id, single_appid, app_id_array, general_track from ctop_ai_kuaishou_advertiser_strategy
-                     where account_id=%s and status=0 order by create_time desc limit 1
+                     where account_id=%s  order by create_time desc limit 1
                   """ % account_id
             df = pd.read_sql(sql, engine)
             if df['general_track'].values[0] == 0:

+ 3 - 3
manual_request_json.py

@@ -22,10 +22,10 @@ def main():
     # print(res)
 
     # data = json.dumps({"account_id": 23212})
-    data = json.dumps({"account_id": 9864909})
-    url = 'http://192.168.1.193:31012/ai_programme_new_material'
+    data = json.dumps({"account_id": 9864904})
+    # url = 'http://192.168.1.193:31018/ai_programme_new_material'
     # url = 'http://192.168.1.193:31012/ai_programme_high_quality_material'
-    # url = 'http://192.168.1.193:31012/ai_historical_missing_material'
+    url = 'http://192.168.1.193:31012/ai_historical_missing_material'
     request = requests.post(url, data)
     print(request.text)
     res_data = simplejson.loads(request.text)

+ 72 - 20
utils/CommonFunction.py

@@ -1,9 +1,11 @@
 import requests
-from utils.UrlConfig import headers
+from utils.UrlConfig import headers, refresh_video_url
 import json
 import pandas as pd
 import datetime
 from utils.DataBaseConfig import *
+from ai_time_task_creative_handler import logger
+import traceback
 
 
 def get_lower_case_name(name):
@@ -47,9 +49,16 @@ def get_app_detail(account_id, url, app_id):
     }
     """
     request_data = {"accountId": account_id, "appId": app_id}
-    request = requests.post(url=url, headers=headers, data=json.JSONEncoder().encode(request_data)).text
-    request = json.loads(request)
-    return request
+    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, appId=%s, the res of getAppDetail is %s" % (account_id, app_id, request_json['message']))
+    except:
+        logger.error('account_id=%s, appId=%s, the res of getAppDetail is %s, the traceback is %s ' %
+                     (account_id, app_id, request, traceback.format_exc()))
+        return {"code": -1}
+    return request_json
 
 
 def get_history_video_info(account_id, url, start_time, end_time, video_cnt, related_creative_max_cnt, app_version=None):
@@ -77,26 +86,53 @@ def get_history_video_info(account_id, url, start_time, end_time, video_cnt, rel
                         "num": video_cnt,
                         "createCount": related_creative_max_cnt,
                         "appVersion": app_version}
-    request = requests.post(url, headers=headers, data=json.JSONEncoder().encode(request_data)).text
-    request = json.loads(request)
-    return request
+    try:
+        request = requests.post(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, app_version=%s, the res of getHistoryVideoList is %s" %
+                        (account_id, app_version, request_json['message']))
+    except Exception:
+        logger.error('account_id=%s, app_version=%s, the res of getHistoryVideoList is %s, the traceback is %s ' %
+                     (account_id, app_version, request, traceback.format_exc()))
+        return {"code": -1}
+    return request_json
 
 
 def get_new_video_info(account_id, url, app_version=None):
     """
     url:http://192.168.1.8:8080/jeecg-boot/kuaishou/material/getNewVideoList
     请求方式:POST
-    remark: 返回 upload_time 在上次调用时间到本次调用时间内的素材(第一次调用返回5分钟前到本次调用时间的素材)
+    remark:
+        1、返回 upload_time 在上次调用时间到本次调用时间内的素材(第一次调用返回5分钟前到本次调用时间的素材)
+        2、需要调用刷新快手素材的接口 http://192.168.1.8:8080/jeecg-boot/kuaishou/material/getKuaiShouVideoList
+            (请求方式 POST 入参:accountId 返回结果:成功或失败)
     入参:accountId
     """
+    try:
+        request = requests.post(url=refresh_video_url, headers=headers,
+                                data=json.JSONEncoder().encode({"accountId": account_id})).text
+        _ = json.loads(request)
+    except Exception:
+        logger.error('account_id=%s, app_version=%s, the res of getKuaiShouVideoList is %s, traceback is %s ' %
+                     (account_id, app_version, request, traceback.format_exc()))
+
     if not app_version:
         request_data = {"accountId": account_id}
     else:
         request_data = {"accountId": account_id, 'appVersion': app_version}
 
-    request = requests.post(url, headers=headers, data=json.JSONEncoder().encode(request_data)).text
-    request = json.loads(request)
-    return request
+    try:
+        request = requests.post(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, app_version=%s, the res of getNewVideoList is %s" %
+                        (account_id, app_version, request_json['message']))
+    except Exception:
+        logger.error('account_id=%s, app_version=%s, the res of getNewVideoList is %s, the traceback is %s ' %
+                     (account_id, app_version, request, traceback.format_exc()))
+        return {"code": -1}
+    return request_json
 
 
 def get_missing_video_info(account_id, cnt, url, start_time, end_time, app_version=None):
@@ -116,9 +152,17 @@ def get_missing_video_info(account_id, cnt, url, start_time, end_time, app_versi
         request_data = {"accountId": account_id, "videoCnt": cnt, "startTime": start_time, "endTime": end_time,
                         'appVersion': app_version}
 
-    request = requests.post(url, headers=headers, data=json.JSONEncoder().encode(request_data)).text
-    request = json.loads(request)
-    return request
+    try:
+        request = requests.post(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, app_version=%s, the res of getCreateZeroVideoList is %s" %
+                        (account_id, app_version, request_json['message']))
+    except Exception:
+        logger.error('account_id=%s, app_version=%s, the res of getCreateZeroVideoList is %s, the traceback is %s ' %
+                     (account_id, app_version, request, traceback.format_exc()))
+        return {"code": -1}
+    return request_json
 
 
 def get_top_video_info(account_id, cnt, start_time, end_time, url, app_version=None):
@@ -154,16 +198,24 @@ def get_top_video_info(account_id, cnt, start_time, end_time, url, app_version=N
     else:
         request_data = {"accountId": account_id, "num": cnt, "startTime": start_time, "endTime": end_time,
                         'appVersion': app_version}
-    request = requests.post(url, headers=headers, data=json.JSONEncoder().encode(request_data)).text
-    request = json.loads(request)
-    return request
+    try:
+        request = requests.post(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, app_version=%s, the res of getHistoryTopVideoList is %s" %
+                        (account_id, app_version, request_json['message']))
+    except Exception:
+        logger.error('account_id=%s, app_version=%s, the res of getHistoryTopVideoList is %s, the traceback is %s ' %
+                     (account_id, app_version, request, traceback.format_exc()))
+        return {"code": -1}
+    return request_json
 
 
 def get_campaign_and_group_name_rule(account_id):
     sql = """select campaign_name, group_name 
              from ctop_ai_kuaishou_advertiser_strategy 
              where account_id = %s 
-             and status = 1
+             order by create_time desc
              limit 1
                """ % account_id
     df = pd.read_sql(sql, engine)
@@ -185,10 +237,10 @@ def get_request_data(account_id, campaign_id, video_info, campaign_name, group_n
         "campaign_info":
             {"campaign_id": campaign_id,
              "campaign_name": (campaign_name.replace('自定义', name_replace) + str(datetime.datetime.now()))
-             if not name_replace else (campaign_name + str(datetime.datetime.now()))},
+             if name_replace else (campaign_name + str(datetime.datetime.now()))},
         'group_info':
             {'group_name': (group_name.replace('自定义', name_replace) + str(datetime.datetime.now()))
-                if not name_replace else (group_name + str(datetime.datetime.now())),
+                if name_replace else (group_name + str(datetime.datetime.now())),
              'begin_time': str(datetime.date.today()),
              'unit_type': unit_type,
              'app_id': app_id},

+ 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"
+    # 刷新快手素材列表
+    refresh_video_url = "http://192.168.1.8:8080/jeecg-boot/kuaishou/material/getKuaiShouVideoList"
 else:
     print("生产环境--url!")
     # 创建计划url
@@ -34,3 +36,5 @@ 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"
+    # 刷新快手素材列表
+    refresh_video_url = "http://api.tjyourong.com.cn/jeecg-boot/kuaishou/material/getKuaiShouVideoList"