import json import traceback from logging.handlers import TimedRotatingFileHandler import tornado.web import pymysql from utils.DataBaseConfig import * import logging from concurrent_log import ConcurrentTimedRotatingFileHandler 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!") 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=host, port=port, user=user, password=passwd, database=db, charset=charset) 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=host, port=port, user=user, password=passwd, database=db, charset=charset) 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()