ai_callback_handler.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import json
  2. import traceback
  3. from logging.handlers import TimedRotatingFileHandler
  4. import pymysql
  5. from utils.BaseClass import *
  6. from utils.DataBaseConfig import *
  7. from utils.LogConfig import *
  8. log_handler = TimedRotatingFileHandler("logs/ai_callback.log",
  9. when="midnight", backupCount=100)
  10. log_handler.setFormatter(log_formatter)
  11. logger = logging.getLogger('ai_callback_logger')
  12. logger.addHandler(log_handler)
  13. logger.setLevel(logging.DEBUG)
  14. print('id of ai_callback_logger %s' % id(logger))
  15. logger.info("ai_callback_server started!")
  16. class AiCallBackAddCreative(tornado.web.RequestHandler):
  17. def post(self):
  18. res = {'code': 0,
  19. 'message': "SUCCESS"}
  20. data = self.request.body
  21. data = str(data, 'utf8')
  22. data = json.loads(data, encoding='utf8')
  23. logger.info("*************************************** NEW REQUEST ***************************************")
  24. logger.info("raw data from request is %s" % data)
  25. try:
  26. db_con = pymysql.connect(host=host,
  27. port=port,
  28. user=user,
  29. password=passwd,
  30. database=db,
  31. charset=charset)
  32. cursor = db_con.cursor()
  33. for item in data['callbackData']:
  34. if item['type'] == 1:
  35. # 自定义创意
  36. creative_uuid = item.get('creative_uuid')
  37. creative_id = item.get('creative_id')
  38. unit_id = item.get('unit_id')
  39. status = item.get('code')
  40. message = item.get('message')
  41. cursor.execute("""UPDATE ctop_ai_kuaishou_creative_level_operation_record
  42. SET unit_id= %s, creative_id=%s, status=%s, message=%s
  43. WHERE creative_uuid = %s""",
  44. (unit_id, creative_id, status, message, creative_uuid))
  45. if item['code'] == 0:
  46. logger.info("on single update callback info: %s" % item)
  47. else:
  48. logger.error("on single update callback info: %s" % item)
  49. if item['type'] == 2:
  50. # 程序化创意2.0
  51. creative_uuid = item.get('creative_uuid')
  52. unit_id = item.get('unit_id')
  53. status = item.get('code')
  54. message = item.get('message')
  55. cursor.execute("""UPDATE ctop_ai_kuaishou_program_creative_level_operation_record
  56. SET unit_id= %s, status=%s, message=%s
  57. WHERE creative_uuid = %s""",
  58. (unit_id, status, message, creative_uuid))
  59. if item['code'] == 0:
  60. logger.info("on single update callback info: %s" % item)
  61. else:
  62. logger.error("on single update callback info: %s" % item)
  63. db_con.commit()
  64. db_con.close()
  65. except Exception:
  66. res['code'] = -1
  67. res['message'] = 'FAIL'
  68. logger.error(traceback.format_exc())
  69. # 返回接口结果
  70. self.write(json.dumps(res))
  71. self.flush()
  72. class AiCallBackAddGroup(tornado.web.RequestHandler):
  73. def post(self):
  74. res = {'code': 0,
  75. 'message': "SUCCESS"}
  76. data = self.request.body
  77. data = str(data, 'utf8')
  78. data = json.loads(data, encoding='utf8')
  79. logger.info("*************************************** NEW REQUEST ***************************************")
  80. logger.info("raw data from request is %s" % data)
  81. try:
  82. db_con = pymysql.connect(host=host,
  83. port=port,
  84. user=user,
  85. password=passwd,
  86. database=db,
  87. charset=charset)
  88. cursor = db_con.cursor()
  89. for item in data['callbackData']:
  90. group_uuid = item.get('group_uuid')
  91. unit_id = item.get('unit_id')
  92. group_create_time = item.get('group_create_time')
  93. status = item.get('code')
  94. message = item.get('message')
  95. cursor.execute("""UPDATE ctop_ai_kuaishou_unit_level_operation_record
  96. SET unit_id= %s, group_create_time=%s,status=%s,message=%s
  97. WHERE group_uuid = %s""", (unit_id, group_create_time, status, message, group_uuid))
  98. if item['code'] == 0:
  99. logger.info("on single update callback info: %s" % item)
  100. else:
  101. logger.error("on single update callback info: %s" % item)
  102. db_con.commit()
  103. db_con.close()
  104. except Exception:
  105. res['code'] = -1
  106. res['message'] = 'FAIL'
  107. logger.error(traceback.format_exc())
  108. # 返回接口结果
  109. self.write(json.dumps(res))
  110. self.flush()