浏览代码

second commit

liyuyi@c-top.com.cn 4 年之前
父节点
当前提交
1218d07ac9
共有 4 个文件被更改,包括 258 次插入10 次删除
  1. 6 0
      .idea/vcs.xml
  2. 202 0
      ai_strategy_request_handler.py
  3. 5 0
      db_config.py
  4. 45 10
      main.py

+ 6 - 0
.idea/vcs.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="$PROJECT_DIR$" vcs="Git" />
+  </component>
+</project>

+ 202 - 0
ai_strategy_request_handler.py

@@ -0,0 +1,202 @@
+from typing import Optional, Awaitable
+
+import pandas as pd
+import numpy as np
+import datetime
+import pymysql
+from sqlalchemy import create_engine
+import simplejson
+import requests
+import tornado.web
+import pandas as pd
+import json
+import numpy as np
+import datetime
+from sqlalchemy import create_engine
+from db_config import *
+import traceback
+
+
+class AiStrategyRequestParse(tornado.web.RequestHandler):
+    def initialize(self, logger):
+        self.logger = logger
+
+    def post(self):
+        data = self.request.body
+        data = str(data, 'utf8')
+        self.logger.info("***************************************  NEW REQUEST ***************************************")
+        self.logger.info("raw data from request is %s" % data)
+
+        # 1、新增广告组
+        # addClass 调用新增的操作类
+
+        # 2、修改操作
+        # modifyClass 调用修改的操作类
+
+        # 3、关停操作
+        # offClass 调用关停的操作类
+
+
+class ParseAddRequest(object):
+    def __init__(self):
+        pass
+
+
+class ParseModifyRequest(object):
+    def __init__(self):
+        pass
+
+
+class ParseShutDownRequest(object):
+    def __init__(self):
+        pass
+
+
+
+
+
+# -----------------------------------------------
+# 1、 获取需要创建广告组所需要的视频 --调用于蒙的视频接口
+
+
+def get_video(acc_id):
+    # url = "http://192.168.1.8:8080/jeecg-boot/kuaishou/Ai/getNewVideoList"
+    # request_data = {'account_id': acc_id}
+    # request = requests.post(url, request_data)
+    # res_data = simplejson.loads(request.text)
+
+    video_dict = {
+        "code": 0,
+        "data": [
+            {
+                "video_url": "http://ucmov.a.yximgs.com/upic/2021/01/07/11/BMjAyMTAxMDcxMTI4MTlfMTI1MDY0NTMyOV80MTk5NzI5Njc0Ml8wXzM=_b_B210bce3effacab7744fa87897f2b4662.mp4?tag=1-1609990769-unknown-0-9ercfqrqp4-dd65af98d83fca04&clientCacheKey=3xigm8jk8fqbti6_b.mp4&tt=b&di=811c96e5&bp=13890",
+                "photo_id": "5227271831775447110",
+                "signature": "ed963b1333fe607d563d5cb0d916b65b",
+                "stat_date": "2021-01-12 10:57:26",
+                "channel_type": 0,
+                "imageList": [
+                    "5ce93696dac9a326ff18505f49391297",
+                    "a368dd69d95539f26f5bee9d1580ccf8",
+                    "33b3e1a2cad48a2309d6ce351e6082fd",
+                    "56f6a8ec736dcdb17a2f052ebfcc255e",
+                    "addd5dc29c52ef863a40c6b688ce7951",
+                    "cb6e10ec5258145056aaafad647e1c36",
+                    "763344141ace6707c79f54bba3db5d1a",
+                    "86e061c34bf675690cb57943d8675130",
+                    "5d5258e7ff4af9dbb196e10ec4e878ce",
+                    "59ca244d88e7f23ef47744110e753bac",
+                    "260e0f08aebbf168b5fd80ee864b2e4c",
+                    "49a551a978d7dd182a04bd6af7e0c08d",
+                    "0c5c43f3b2c215e66d646fb9c03e110b",
+                    "44731b7d8b7e93ac12e8dcf3d34cc272",
+                    "2227f6cc89a5c0f242e04762b0444032"
+                ]
+            },
+            {
+                "video_url": "http://sjymov.a.yximgs.com/upic/2020/12/03/15/BMjAyMDEyMDMxNTEyNDZfMTI1MDY0NTMyOV80MDE1Mjg1MDE5OV8wXzM=_b_B7fd27f653e6af4e8cf0979041e31a38b.mp4?tag=1-1606982411-unknown-0-1ksr2io24f-3a57957a462092c7&clientCacheKey=3xmtw52hxui5j7k_b.mp4&tt=b&di=8bba1fd5&bp=13890",
+                "photo_id": "5211227760959967759",
+                "signature": "fc7717ac4f08316e81aa463c720226c2",
+                "stat_date": "2021-01-12 10:57:21",
+                "channel_type": 0,
+                "imageList": [
+                    "11836a6a4042b254a64bd9a6ca99f9b5",
+                    "82349cfa1e26bd17397a303b85dd0fbb",
+                    "04b1dc6793b0d2a65d8f8b45ce37744d",
+                    "4165fa246defbced026558d62ef30ed5",
+                    "064af2a19b313c0c436f6c2e4d2384eb",
+                    "13997aaa0294bcf1ca0c27a43aecceb2",
+                    "fb138ea0251f0a1c243cb287f1f865d8",
+                    "83e9292549f9617ceb5cb468cd5cce0d",
+                    "4bf798acb18747baacec0762d76e2006",
+                    "74d7b71c4e7be44d12cd7af03568053b",
+                    "b74620eeaa6c19620530b7cb553e954d",
+                    "2a1c907e5e3cf0cf2e871e5df16cca8f",
+                    "981660adc052f807ece9155124290ae1",
+                    "1247852f30b51511b510ef3b2d7f0b70",
+                    "b13119ff3fb326adfdcd04bb87324d02"
+                ]
+            }
+        ],
+        "message": "SUCCESS"
+    }
+    if video_dict['code'] != 0:
+        return
+    else:
+        return video_dict
+
+
+# 拼接请求的json
+# example: 整个项目近30天内,消耗top10的视频,创建组
+account_id = 9556344
+request_dict = {}
+# 1、读取 客户策略表中 status = 1 的记录 (ctop_ai_kuaishou_advertiser_strategy)
+get_advertiser_strategy_sql = """
+select * 
+from ctop_ai_kuaishou_advertiser_strategy 
+where account_id = %d 
+and status = 1
+""" % account_id
+advertiser_strategy_df = pd.read_sql(get_advertiser_strategy_sql, engine)
+advertiser_strategy_drop_cols = ['id', 'status', 'create_time', 'effective_time', 'expiry_time']
+advertiser_strategy_df.drop(columns=advertiser_strategy_drop_cols, inplace=True)
+request_dict['advertiser_strategy_info'] = advertiser_strategy_df.T.to_dict()[0]
+
+# 2、拼接请求的json
+
+
+# 获取视频方式一
+days = 30
+num = 10
+sql = """
+select * from ctop_kuaishou_video_get
+        where account_id = %d
+        and signature in
+        (
+        select
+            daily_material.signature
+        from
+        ctop_kuaishou_report_daily_material daily_material
+        inner join
+        ctop_user_allocation allocation
+        on daily_material.account_id = allocation.account_id
+        where  allocation.project_id in (select project_id from ctop_user_allocation where account_id = %d)
+        and  to_days(now()) - to_days(daily_material.stat_date) <= %d
+        and daily_material.signature is not null
+        GROUP BY daily_material.signature
+        order by sum(daily_material.charge) desc
+        limit %d
+        )
+""" % (account_id, account_id, days, num)
+# video_df = pd.read_sql(sql, engine)
+# print(video_df.shape)
+
+# 获取视频方式二
+# 离线分析手动提取 signature,然后创建广告组
+# signature_list = ['f17059ef15d5940a43bd38f9b13dc551', 'c0065cbd882d94c7c0a808db68f73666']
+# sql = """
+# select * from ctop_kuaishou_video_get
+#         where account_id = %d
+#         and signature in %s
+# """ % (account_id, tuple(signature_list))
+# video_df = pd.read_sql(sql, engine)
+# print(video_df)
+
+# 2.1 计划层级的参数
+campaign_name = "户14-上下滑&信息流-跑量视频-剪辑-低价-低价包邮-活动-年龄18岁以上-单出价-" + \
+                datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S")
+# campaign_id 为空表示需要创建广告计划, 不为空则表示在该广告计划下新增广告组
+campaign_dict = {}
+campaign_id = None
+campaign_dict['campaign_name'] = campaign_name
+campaign_dict['campaign_id'] = campaign_id
+campaign_dict['campaign_type'] = 2
+campaign_dict['day_budget'] = None
+campaign_dict['day_budget_schedule'] = None
+campaign_dict['operation_type'] = "add"
+
+request_dict['campaign'] = campaign_dict
+
+# 2.2 组层级的参数
+
+if __name__ == '__main__':
+    print("test")

+ 5 - 0
db_config.py

@@ -0,0 +1,5 @@
+# 线上数据库连接字符串
+# db_con_str = "mysql+pymysql://%s:%s@%s:%d/%s" % ("readonly", "hcst@2021", "139.186.27.96", 4000, "jeecg-boot")
+
+# 测试数据库连接字符串
+db_con_str = "mysql+pymysql://%s:%s@%s:%d/%s" % ("hcst", "hcst@2020", "139.186.151.174", 3306, "jeecg-boot")

+ 45 - 10
main.py

@@ -1,16 +1,51 @@
-# This is a sample Python script.
+import tornado.ioloop
+import tornado.web
+import argparse
+import json
+import pickle
+import tornado.options
+import tornado.log
+import time
+import traceback
+from logging.handlers import TimedRotatingFileHandler
+import logging
 
-# Press Shift+F10 to execute it or replace it with your code.
-# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
+import ai_strategy_request_handler
 
 
-def print_hi(name):
-    # Use a breakpoint in the code line below to debug your script.
-    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.
+def create_application():
+    # 日志的格式
+    log_formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s',
+                                      '%m/%d/%Y %I:%M:%S %p')
 
+    # ai策略请求解析的日志对象
+    ai_strategy_request_log_handler = TimedRotatingFileHandler("logs/ai_strategy_request/ai_strategy_request.log",
+                                                   when="midnight",
+                                                   backupCount=100)
 
-# Press the green button in the gutter to run the script.
-if __name__ == '__main__':
-    print_hi('PyCharm')
+    ai_strategy_request_log_handler.setFormatter(log_formatter)
+    ai_strategy_request_logger = logging.getLogger('ai_strategy_request_logger')
+    ai_strategy_request_logger.addHandler(ai_strategy_request_log_handler)
+    ai_strategy_request_logger.setLevel(logging.DEBUG)
 
-# See PyCharm help at https://www.jetbrains.com/help/pycharm/
+    print("ai_strategy_request_logger's id is {}".format(id(ai_strategy_request_logger)))
+    msg = "ai strategy request service started at {t}".\
+           format(t=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
+    ai_strategy_request_logger.info(msg)
+
+    web_application = tornado.web.Application([
+        (r"/ai_strategy_request",
+         ai_strategy_request_handler.AiStrategyRequestParse,
+         {"logger": ai_strategy_request_logger}),
+    ])
+
+    return web_application
+
+
+if __name__ == "__main__":
+    parser = argparse.ArgumentParser()
+    parser.add_argument('-p', '--port', type=int, default=31012)
+    args = parser.parse_args()
+    application = create_application()
+    application.listen(args.port)
+    tornado.ioloop.IOLoop.instance().start()