import json import os import sys import traceback import pymysql import tornado.web import yaml from loguru import logger curr_path = os.path.abspath(os.path.dirname(__file__)) project_root_path = curr_path[:curr_path.find("ai_target") + len("ai_target")] sys.path.append(project_root_path) from config.url_and_db import db_config_info logger.remove() # 删去 import logger之后自动产生的handler,不删除的话会出现重复输出的现象 logger.add("/data/pythonProject/ai_target/logs/ai_callback.{time:YYYY-MM-DD}.log", rotation="00:00", format="{time:YYYY-MM-DD HH:mm:ss,SSS} [{process}] [{thread}] {level} {file} {line} - {message}", level="INFO") logger.info("ai_callback_server started!") with open('/data/pythonProject/ai_target/config/config.yaml', mode='r', encoding='utf-8') as f: config = yaml.load(f.read(), Loader=yaml.FullLoader) 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_config_info['host'], port=db_config_info['port'], user=db_config_info['username'], password=db_config_info['password'], database=db_config_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_config_info['host'], port=db_config_info['port'], user=db_config_info['username'], password=db_config_info['password'], database=db_config_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()