ai_callback_handler.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/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. class AiCallBackAddCreative(tornado.web.RequestHandler):
  16. def post(self):
  17. res = {'code': 0,
  18. 'message': "SUCCESS"}
  19. data = self.request.body
  20. data = str(data, 'utf8')
  21. data = json.loads(data, encoding='utf8')
  22. logger.info("*************************************** NEW REQUEST ***************************************")
  23. logger.info("raw data from request is %s" % data)
  24. try:
  25. db_con = pymysql.connect(host=host,
  26. port=port,
  27. user=user,
  28. password=passwd,
  29. database=db,
  30. charset=charset)
  31. cursor = db_con.cursor()
  32. for item in data['callbackData']:
  33. if item['code'] == 0:
  34. creative_uuid = item['creative_uuid']
  35. creative_id = item['creative_id']
  36. unit_id = item['unit_id']
  37. logger.info("on single update callback info: %s" % item)
  38. cursor.execute("""UPDATE ctop_ai_kuaishou_creative_level_operation_record
  39. SET unit_id= %s, creative_id=%s
  40. WHERE creative_uuid = %s""", (unit_id, creative_id, creative_uuid))
  41. else:
  42. logger.error("on single update callback info: %s" % item)
  43. db_con.commit()
  44. db_con.close()
  45. except Exception:
  46. res['code'] = -1
  47. res['message'] = 'FAIL'
  48. logger.error(traceback.format_exc())
  49. # 返回接口结果
  50. self.write(json.dumps(res))
  51. self.flush()
  52. class AiCallBackAddGroup(tornado.web.RequestHandler):
  53. def post(self):
  54. res = {'code': 0,
  55. 'message': "SUCCESS"}
  56. data = self.request.body
  57. data = str(data, 'utf8')
  58. data = json.loads(data, encoding='utf8')
  59. logger.info("*************************************** NEW REQUEST ***************************************")
  60. logger.info("raw data from request is %s" % data)
  61. try:
  62. db_con = pymysql.connect(host=host,
  63. port=port,
  64. user=user,
  65. password=passwd,
  66. database=db,
  67. charset=charset)
  68. cursor = db_con.cursor()
  69. for item in data['callbackData']:
  70. if item['code'] == 0:
  71. group_uuid = item['group_uuid']
  72. unit_id = item['unit_id']
  73. group_create_time = item['group_create_time']
  74. logger.info("on single update callback info: %s" % item)
  75. cursor.execute("""UPDATE ctop_ai_kuaishou_unit_level_operation_record
  76. SET unit_id= %s, group_create_time=%s
  77. WHERE group_uuid = %s""", (unit_id, group_create_time, group_uuid))
  78. else:
  79. logger.error("on single update callback info: %s" % item)
  80. db_con.commit()
  81. db_con.close()
  82. except Exception:
  83. res['code'] = -1
  84. res['message'] = 'FAIL'
  85. logger.error(traceback.format_exc())
  86. # 返回接口结果
  87. self.write(json.dumps(res))
  88. self.flush()