ai_callback_handler.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.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. # TODO 1、更新message 和 status 到表中 2、需要判断是否为 程序化,来更新不同的表
  35. if item['code'] == 0:
  36. creative_uuid = item['creative_uuid']
  37. creative_id = item['creative_id']
  38. unit_id = item['unit_id']
  39. logger.info("on single update callback info: %s" % item)
  40. cursor.execute("""UPDATE ctop_ai_kuaishou_creative_level_operation_record
  41. SET unit_id= %s, creative_id=%s
  42. WHERE creative_uuid = %s""", (unit_id, creative_id, creative_uuid))
  43. else:
  44. logger.error("on single update callback info: %s" % item)
  45. db_con.commit()
  46. db_con.close()
  47. except Exception:
  48. res['code'] = -1
  49. res['message'] = 'FAIL'
  50. logger.error(traceback.format_exc())
  51. # 返回接口结果
  52. self.write(json.dumps(res))
  53. self.flush()
  54. class AiCallBackAddGroup(tornado.web.RequestHandler):
  55. def post(self):
  56. res = {'code': 0,
  57. 'message': "SUCCESS"}
  58. data = self.request.body
  59. data = str(data, 'utf8')
  60. data = json.loads(data, encoding='utf8')
  61. logger.info("*************************************** NEW REQUEST ***************************************")
  62. logger.info("raw data from request is %s" % data)
  63. try:
  64. db_con = pymysql.connect(host=host,
  65. port=port,
  66. user=user,
  67. password=passwd,
  68. database=db,
  69. charset=charset)
  70. cursor = db_con.cursor()
  71. for item in data['callbackData']:
  72. # TODO 更新message 和 status 到表中
  73. if item['code'] == 0:
  74. group_uuid = item['group_uuid']
  75. unit_id = item['unit_id']
  76. group_create_time = item['group_create_time']
  77. logger.info("on single update callback info: %s" % item)
  78. cursor.execute("""UPDATE ctop_ai_kuaishou_unit_level_operation_record
  79. SET unit_id= %s, group_create_time=%s
  80. WHERE group_uuid = %s""", (unit_id, group_create_time, group_uuid))
  81. else:
  82. logger.error("on single update callback info: %s" % item)
  83. db_con.commit()
  84. db_con.close()
  85. except Exception:
  86. res['code'] = -1
  87. res['message'] = 'FAIL'
  88. logger.error(traceback.format_exc())
  89. # 返回接口结果
  90. self.write(json.dumps(res))
  91. self.flush()