瀏覽代碼

修改上新代码逻辑

syh 4 年之前
父節點
當前提交
f301606669
共有 7 個文件被更改,包括 135 次插入19 次删除
  1. 1 1
      .idea/sqldialects.xml
  2. 45 0
      ai_auto_create_creative_job.py
  3. 31 15
      server.py
  4. 22 2
      mysql_search_utils.py
  5. 30 0
      utils/DateUtils.py
  6. 1 1
      HttpUtils.py
  7. 5 0
      utils/ResultDictUtils.py

+ 1 - 1
.idea/sqldialects.xml

@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <project version="4">
   <component name="SqlDialectMappings">
-    <file url="file://$PROJECT_DIR$/mysql_search_utils.py" dialect="GenericSQL" />
+    <file url="file://$PROJECT_DIR$/utils/DataSearchUtils.py" dialect="GenericSQL" />
   </component>
 </project>

+ 45 - 0
ai_auto_create_creative_job.py

@@ -0,0 +1,45 @@
+from utils.DataSearchUtils import MysqlSearchUtils
+from utils import ResultDictUtils
+import datetime
+import time
+from utils.DateUtils import DateUtils
+
+mysqlUtils = MysqlSearchUtils()
+dateUtils = DateUtils()
+
+
+class AiAutoCreateCreativeJob():
+
+    def auto_create_job(self):
+        response_dict = ResultDictUtils.common_success_dict
+        # 查询需要自动创建创意的模板信息
+        templates = mysqlUtils.get_all_ai_account_target_template_by_params(1)
+        if len(templates):
+            for item in templates:
+                self.create_creative(item)
+            return response_dict
+        else:
+            return ResultDictUtils.ai_account_template_not_exist_dict
+
+    def create_creative(self,item):
+        # 查询当前时间段内的视频数据
+        accountId= item["account_id"]
+        timestamp = time.time()
+        enddate= dateUtils.get_datetime_str_add_sencend(timestamp,-5*60)
+        startdate= dateUtils.get_datetime_str_add_sencend(timestamp,-10*60)
+        list = mysqlUtils.get_kuaishou_video_list_by_params(accountId,startdate,enddate)
+        if len(list):
+            return None
+        else:
+            return ResultDictUtils.ai_account_template_not_exist_dict
+
+if __name__ == '__main__':
+    timestamp = time.time()
+    endtimestamp = timestamp - 5*60
+
+    datetime_struct = datetime.datetime.fromtimestamp(timestamp)
+    enddatetime_struct = datetime.datetime.fromtimestamp(endtimestamp)
+    print(timestamp)
+    print(datetime_struct.strftime('%Y-%m-%d %H:%M:%S'))
+    print(endtimestamp)
+    print(enddatetime_struct.strftime('%Y-%m-%d %H:%M:%S'))

+ 31 - 15
server.py

@@ -1,38 +1,54 @@
 from flask import Flask,request
-from mysql_search_utils import mysqlSearchUtils
-from HttpUtils import httpUtils
+from utils.DataSearchUtils import MysqlSearchUtils
+from utils.HttpUtils import HttpUtils
+from ai_auto_create_creative_job import AiAutoCreateCreativeJob
 import json
+from utils import ResultDictUtils
 
 app = Flask(__name__)
+mysql = MysqlSearchUtils()
+httpUtils = HttpUtils()
+aiAutoCreateCreativeJob = AiAutoCreateCreativeJob()
 
-mysql = mysqlSearchUtils()
-httpUtils = httpUtils()
 
 @app.route('/hello')
-def helloWord():
+def hello_word():
     print(request.path)
     print(request.full_path)
     return 'hello world'
 
 
 @app.route('/getOne')
-def getJson():
-    responseDict = {}
+def get_json():
+    response_dict = {}
     data = mysql.get_one()
-    responseDict["data"] = data
-    return responseDict
+    response_dict["data"] = data
+    return response_dict
+
 
 @app.route('/getList')
-def getList():
-    responseDict = {}
+def get_list():
+    response_dict = {}
     list = mysql.get_all()
-    responseDict["list"] = list
-    return responseDict
+    response_dict["list"] = list
+    return response_dict
+
 
-@app.route('/testReport')
-def testReport():
+@app.route('/testReport',methods=["get"])
+def test_report():
     rest = httpUtils.get("http://localhost:8079/getOne",{"accountId":"123"},{"accountId":"123"})
     return json.loads(rest)
 
+
+@app.route('/autoCreateCreativeJob')
+def auto_create_creative_job():
+    response_dict = ResultDictUtils.common_success_dict
+    account_id = request.args.get("accountId")
+    if 0 == account_id or account_id is None:
+        return ResultDictUtils.params_error_dict
+    rest = aiAutoCreateCreativeJob.auto_create_job(account_id)
+    response_dict["data"] = rest
+    return response_dict
+
 if __name__ == '__main__':
     app.run(host='0.0.0.0',port=8079,debug=True)

+ 22 - 2
mysql_search_utils.py

@@ -1,12 +1,15 @@
 import pymysql
 
-class mysqlSearchUtils():
+
+class MysqlSearchUtils():
+
     def __init__(self):
         try:
             self.conn = pymysql.connect(host="139.186.27.96",port=4000,user="data",passwd="hcst@2021",db="jeecg-boot",charset="utf8mb4")
             self.cursor = self.conn.cursor()
         except pymysql.err as e:
             print("error"% e)
+
     def get_one(self):
         sql = "select * from ctop_user_allocation where account_id = %s order by id desc"
         self.cursor.execute(sql, ('9556344',))
@@ -23,11 +26,28 @@ class mysqlSearchUtils():
         self.cursor.close()
         return result
 
+    def get_all_ai_account_target_template_by_params(self,status,account_id):
+        sql = "select * from ctop_ai_kuaishou_auto_creative_account_info where status = %s order by id desc"
+        self.cursor.execute(sql, (status,))
+        rest = self.cursor.fetchall()
+        result = [dict(zip([k[0] for k in self.cursor.description], row)) for row in rest]
+        self.cursor.close()
+        return result
+
     def db_close(self):
         self.conn.close()
 
+    def get_kuaishou_video_list_by_params(self, accountId, startDate, endDate):
+        sql = "select * from ctop_kuaishou_video_get where account_id = %s and stat_date>= %s and stat_date<=%s order by id desc"
+        self.cursor.execute(sql, (accountId,startDate,endDate))
+        rest = self.cursor.fetchall()
+        result = [dict(zip([k[0] for k in self.cursor.description], row)) for row in rest]
+        self.cursor.close()
+        return result
+
+
 if __name__ == '__main__':
-    obj = mysqlSearchUtils()
+    obj = MysqlSearchUtils()
     reslt = obj.get_all()
     for item in reslt:
         print(item)

+ 30 - 0
utils/DateUtils.py

@@ -0,0 +1,30 @@
+import time
+import datetime
+
+class DateUtils:
+
+    def get_now_datetime_str(self):
+        timestamp = time.time()
+        datetime_struct = datetime.datetime.fromtimestamp(timestamp)
+        return datetime_struct.strftime('%Y-%m-%d %H:%M:%S')
+
+    def get_now_date_str(self):
+        timestamp = time.time()
+        datetime_struct = datetime.datetime.fromtimestamp(timestamp)
+        return datetime_struct.strftime('%Y-%m-%d')
+
+    def get_now_date_str_add_sencend(self,seconds):
+        timestamp = time.time()
+        return self.get_date_str_add_sencend(timestamp,seconds)
+
+    def get_date_str_add_sencend(self,timestemp,seconds):
+        datetime_struct = datetime.datetime.fromtimestamp(timestemp+seconds)
+        return datetime_struct.strftime('%Y-%m-%d')
+
+    def get_now_datetime_str_add_sencend(self,seconds):
+        timestamp = time.time()
+        return self.get_datetime_str_add_sencend(timestamp,seconds)
+
+    def get_datetime_str_add_sencend(self,timestemp,seconds):
+        datetime_struct = datetime.datetime.fromtimestamp(timestemp+seconds)
+        return datetime_struct.strftime('%Y-%m-%d %H:%M:%S')

+ 1 - 1
HttpUtils.py

@@ -2,7 +2,7 @@ import requests
 import logging
 
 
-class httpUtils:
+class HttpUtils:
     logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
     logger = logging.getLogger(__name__)
 

+ 5 - 0
utils/ResultDictUtils.py

@@ -0,0 +1,5 @@
+
+common_success_dict = {"code": 0,"success": True,'message': "操作成功"}
+params_error_dict = {"code": -1,"success": False,'message': "参数异常"}
+ai_account_template_not_exist_dict = {"code": -2,"success": False,'message': "账户投放模板不存在"}
+ai_account_new_video_not_exist_dict = {"code": 0,"success": True,'message': "该时段不存在上新视频"}