ai_callback_handler.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. from concurrent_log import ConcurrentTimedRotatingFileHandler
  9. log_formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s', '%m/%d/%Y %I:%M:%S %p')
  10. log_handler = ConcurrentTimedRotatingFileHandler("logs/ai_callback.log",
  11. when="midnight", backupCount=100)
  12. log_handler.setFormatter(log_formatter)
  13. logger = logging.getLogger('ai_callback_logger')
  14. logger.addHandler(log_handler)
  15. logger.setLevel(logging.DEBUG)
  16. print('id of ai_callback_logger %s' % id(logger))
  17. logger.info("ai_callback_server started!")
  18. class AiCallBackAddCreative(tornado.web.RequestHandler):
  19. def post(self):
  20. res = {'code': 0,
  21. 'message': "SUCCESS"}
  22. data = self.request.body
  23. data = str(data, 'utf8')
  24. data = json.loads(data, encoding='utf8')
  25. logger.info("call back of add creative, the 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("call back of add group, the 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()