ai_callback_handler.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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("*************************************** NEW REQUEST ***************************************")
  26. logger.info("raw data from request is %s" % data)
  27. try:
  28. db_con = pymysql.connect(host=host,
  29. port=port,
  30. user=user,
  31. password=passwd,
  32. database=db,
  33. charset=charset)
  34. cursor = db_con.cursor()
  35. for item in data['callbackData']:
  36. if item['type'] == 1:
  37. # 自定义创意
  38. creative_uuid = item.get('creative_uuid')
  39. creative_id = item.get('creative_id')
  40. unit_id = item.get('unit_id')
  41. status = item.get('code')
  42. message = item.get('message')
  43. cursor.execute("""UPDATE ctop_ai_kuaishou_creative_level_operation_record
  44. SET unit_id= %s, creative_id=%s, status=%s, message=%s
  45. WHERE creative_uuid = %s""",
  46. (unit_id, creative_id, status, message, creative_uuid))
  47. if item['code'] == 0:
  48. logger.info("on single update callback info: %s" % item)
  49. else:
  50. logger.error("on single update callback info: %s" % item)
  51. if item['type'] == 2:
  52. # 程序化创意2.0
  53. creative_uuid = item.get('creative_uuid')
  54. unit_id = item.get('unit_id')
  55. status = item.get('code')
  56. message = item.get('message')
  57. cursor.execute("""UPDATE ctop_ai_kuaishou_program_creative_level_operation_record
  58. SET unit_id= %s, status=%s, message=%s
  59. WHERE creative_uuid = %s""",
  60. (unit_id, status, message, creative_uuid))
  61. if item['code'] == 0:
  62. logger.info("on single update callback info: %s" % item)
  63. else:
  64. logger.error("on single update callback info: %s" % item)
  65. db_con.commit()
  66. db_con.close()
  67. except Exception:
  68. res['code'] = -1
  69. res['message'] = 'FAIL'
  70. logger.error(traceback.format_exc())
  71. # 返回接口结果
  72. self.write(json.dumps(res))
  73. self.flush()
  74. class AiCallBackAddGroup(tornado.web.RequestHandler):
  75. def post(self):
  76. res = {'code': 0,
  77. 'message': "SUCCESS"}
  78. data = self.request.body
  79. data = str(data, 'utf8')
  80. data = json.loads(data, encoding='utf8')
  81. logger.info("*************************************** NEW REQUEST ***************************************")
  82. logger.info("raw data from request is %s" % data)
  83. try:
  84. db_con = pymysql.connect(host=host,
  85. port=port,
  86. user=user,
  87. password=passwd,
  88. database=db,
  89. charset=charset)
  90. cursor = db_con.cursor()
  91. for item in data['callbackData']:
  92. group_uuid = item.get('group_uuid')
  93. unit_id = item.get('unit_id')
  94. group_create_time = item.get('group_create_time')
  95. status = item.get('code')
  96. message = item.get('message')
  97. cursor.execute("""UPDATE ctop_ai_kuaishou_unit_level_operation_record
  98. SET unit_id= %s, group_create_time=%s,status=%s,message=%s
  99. WHERE group_uuid = %s""", (unit_id, group_create_time, status, message, group_uuid))
  100. if item['code'] == 0:
  101. logger.info("on single update callback info: %s" % item)
  102. else:
  103. logger.error("on single update callback info: %s" % item)
  104. db_con.commit()
  105. db_con.close()
  106. except Exception:
  107. res['code'] = -1
  108. res['message'] = 'FAIL'
  109. logger.error(traceback.format_exc())
  110. # 返回接口结果
  111. self.write(json.dumps(res))
  112. self.flush()