| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 | import jsonimport tracebackimport tornado.webimport pymysqlimport loggingfrom concurrent_log import ConcurrentTimedRotatingFileHandlerimport yamlimport oslog_formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s', '%m/%d/%Y %I:%M:%S %p')log_handler = ConcurrentTimedRotatingFileHandler("/data/pythonProject/ai_target/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('/data/pythonProject/ai_target/config/config.yaml', mode='r', encoding='utf-8') as f:    config = yaml.load(f.read(), Loader=yaml.FullLoader)if os.getenv('LYY_DEV', '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()
 |