ai_callback_handler.py 5.3 KB

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