ai_callback_handler.py 4.0 KB

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