ai_callback_handler.py 4.0 KB

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