liyuyi@c-top.com.cn 4 years ago
parent
commit
ee8c970da3
1 changed files with 128 additions and 0 deletions
  1. 128 0
      ai_callback_handler.py

+ 128 - 0
ai_callback_handler.py

@@ -0,0 +1,128 @@
+import json
+import traceback
+import tornado.web
+import pymysql
+import logging
+from concurrent_log import ConcurrentTimedRotatingFileHandler
+import yaml
+import os
+
+log_formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s', '%m/%d/%Y %I:%M:%S %p')
+log_handler = ConcurrentTimedRotatingFileHandler("logs/ai_callback.log",
+                                       when="midnight", backupCount=100)
+log_handler.setFormatter(log_formatter)
+logger = logging.getLogger('ai_callback_logger')
+logger.addHandler(log_handler)
+logger.setLevel(logging.DEBUG)
+print('id of ai_callback_logger %s' % id(logger))
+logger.info("ai_callback_server started!")
+
+with open('config/config.yaml', mode='r', encoding='utf-8') as f:
+    config = yaml.load(f.read(), Loader=yaml.FullLoader)
+if os.getenv('environment', 'unknown') == 'dev':
+    db_info = config['devDB']
+else:
+    db_info = config['productDB']
+
+
+class AiCallBackAddCreative(tornado.web.RequestHandler):
+    def post(self):
+        res = {'code': 0,
+               'message': "SUCCESS"}
+
+        data = self.request.body
+        data = str(data, 'utf8')
+        data = json.loads(data, encoding='utf8')
+        logger.info("call back of add creative, the raw data from request is %s" % data)
+
+        try:
+            db_con = pymysql.connect(host=db_info['host'],
+                                     port=db_info['port'],
+                                     user=db_info['username'],
+                                     password=db_info['password'],
+                                     database=db_info['database'],
+                                     charset='utf8')
+            cursor = db_con.cursor()
+            for item in data['callbackData']:
+                if item['type'] == 1:
+                    # 自定义创意
+                    creative_uuid = item.get('creative_uuid')
+                    creative_id = item.get('creative_id')
+                    unit_id = item.get('unit_id')
+                    status = item.get('code')
+                    message = item.get('message')
+                    cursor.execute("""UPDATE ctop_ai_kuaishou_creative_level_operation_record
+                                       SET unit_id= %s, creative_id=%s, status=%s, message=%s
+                                       WHERE creative_uuid = %s""",
+                                   (unit_id, creative_id, status, message, creative_uuid))
+                    if item['code'] == 0:
+                        logger.info("on single update callback info: %s" % item)
+                    else:
+                        logger.error("on single update callback info: %s" % item)
+                if item['type'] == 2:
+                    # 程序化创意2.0
+                    creative_uuid = item.get('creative_uuid')
+                    unit_id = item.get('unit_id')
+                    status = item.get('code')
+                    message = item.get('message')
+                    cursor.execute("""UPDATE ctop_ai_kuaishou_program_creative_level_operation_record
+                                      SET unit_id= %s,  status=%s, message=%s
+                                      WHERE creative_uuid = %s""",
+                                   (unit_id, status, message, creative_uuid))
+                    if item['code'] == 0:
+                        logger.info("on single update callback info: %s" % item)
+                    else:
+                        logger.error("on single update callback info: %s" % item)
+            db_con.commit()
+            db_con.close()
+        except Exception:
+            res['code'] = -1
+            res['message'] = 'FAIL'
+            logger.error(traceback.format_exc())
+
+        # 返回接口结果
+        self.write(json.dumps(res))
+        self.flush()
+
+
+class AiCallBackAddGroup(tornado.web.RequestHandler):
+    def post(self):
+        res = {'code': 0,
+               'message': "SUCCESS"}
+
+        data = self.request.body
+        data = str(data, 'utf8')
+        data = json.loads(data, encoding='utf8')
+        logger.info("call back of add group, the raw data from request is %s" % data)
+
+        try:
+            db_con = pymysql.connect(host=db_info['host'],
+                                     port=db_info['port'],
+                                     user=db_info['username'],
+                                     password=db_info['password'],
+                                     database=db_info['database'],
+                                     charset='utf8')
+            cursor = db_con.cursor()
+            for item in data['callbackData']:
+                group_uuid = item.get('group_uuid')
+                unit_id = item.get('unit_id')
+                group_create_time = item.get('group_create_time')
+                status = item.get('code')
+                message = item.get('message')
+                cursor.execute("""UPDATE ctop_ai_kuaishou_unit_level_operation_record
+                                  SET unit_id= %s, group_create_time=%s,status=%s,message=%s
+                                  WHERE group_uuid = %s""", (unit_id, group_create_time, status, message, group_uuid))
+                if item['code'] == 0:
+                    logger.info("on single update callback info: %s" % item)
+                else:
+                    logger.error("on single update callback info: %s" % item)
+            db_con.commit()
+            db_con.close()
+        except Exception:
+            res['code'] = -1
+            res['message'] = 'FAIL'
+            logger.error(traceback.format_exc())
+
+        # 返回接口结果
+        self.write(json.dumps(res))
+        self.flush()