ai_callback_handler.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import json
  2. import traceback
  3. import tornado.web
  4. import pymysql
  5. import logging
  6. from concurrent_log import ConcurrentTimedRotatingFileHandler
  7. import yaml
  8. import os
  9. log_formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s', '%m/%d/%Y %I:%M:%S %p')
  10. log_handler = ConcurrentTimedRotatingFileHandler("/data/pythonProject/ai_target/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. with open('/data/pythonProject/ai_target/config/config.yaml', mode='r', encoding='utf-8') as f:
  19. config = yaml.load(f.read(), Loader=yaml.FullLoader)
  20. if os.getenv('LYY_DEV', 'unknown') == 'dev':
  21. db_info = config['devDB']
  22. else:
  23. db_info = config['productDB']
  24. class AiCallBackAddCreative(tornado.web.RequestHandler):
  25. def post(self):
  26. res = {'code': 0,
  27. 'message': "SUCCESS"}
  28. data = self.request.body
  29. data = str(data, 'utf8')
  30. data = json.loads(data, encoding='utf8')
  31. logger.info("call back of add creative, the raw data from request is %s" % data)
  32. try:
  33. db_con = pymysql.connect(host=db_info['host'],
  34. port=db_info['port'],
  35. user=db_info['username'],
  36. password=db_info['password'],
  37. database=db_info['database'],
  38. charset='utf8')
  39. cursor = db_con.cursor()
  40. for item in data['callbackData']:
  41. if item['type'] == 1:
  42. # 自定义创意
  43. creative_uuid = item.get('creative_uuid')
  44. creative_id = item.get('creative_id')
  45. unit_id = item.get('unit_id')
  46. status = item.get('code')
  47. message = item.get('message')
  48. cursor.execute("""UPDATE ctop_ai_kuaishou_creative_level_operation_record
  49. SET unit_id= %s, creative_id=%s, status=%s, message=%s
  50. WHERE creative_uuid = %s""",
  51. (unit_id, creative_id, status, message, creative_uuid))
  52. if item['code'] == 0:
  53. logger.info("on single update callback info: %s" % item)
  54. else:
  55. logger.error("on single update callback info: %s" % item)
  56. if item['type'] == 2:
  57. # 程序化创意2.0
  58. creative_uuid = item.get('creative_uuid')
  59. unit_id = item.get('unit_id')
  60. status = item.get('code')
  61. message = item.get('message')
  62. cursor.execute("""UPDATE ctop_ai_kuaishou_program_creative_level_operation_record
  63. SET unit_id= %s, status=%s, message=%s
  64. WHERE creative_uuid = %s""",
  65. (unit_id, status, message, creative_uuid))
  66. if item['code'] == 0:
  67. logger.info("on single update callback info: %s" % item)
  68. else:
  69. logger.error("on single update callback info: %s" % item)
  70. db_con.commit()
  71. db_con.close()
  72. except Exception:
  73. res['code'] = -1
  74. res['message'] = 'FAIL'
  75. logger.error(traceback.format_exc())
  76. # 返回接口结果
  77. self.write(json.dumps(res))
  78. self.flush()
  79. class AiCallBackAddGroup(tornado.web.RequestHandler):
  80. def post(self):
  81. res = {'code': 0,
  82. 'message': "SUCCESS"}
  83. data = self.request.body
  84. data = str(data, 'utf8')
  85. data = json.loads(data, encoding='utf8')
  86. logger.info("call back of add group, the raw data from request is %s" % data)
  87. try:
  88. db_con = pymysql.connect(host=db_info['host'],
  89. port=db_info['port'],
  90. user=db_info['username'],
  91. password=db_info['password'],
  92. database=db_info['database'],
  93. charset='utf8')
  94. cursor = db_con.cursor()
  95. for item in data['callbackData']:
  96. group_uuid = item.get('group_uuid')
  97. unit_id = item.get('unit_id')
  98. group_create_time = item.get('group_create_time')
  99. status = item.get('code')
  100. message = item.get('message')
  101. cursor.execute("""UPDATE ctop_ai_kuaishou_unit_level_operation_record
  102. SET unit_id= %s, group_create_time=%s,status=%s,message=%s
  103. WHERE group_uuid = %s""", (unit_id, group_create_time, status, message, group_uuid))
  104. if item['code'] == 0:
  105. logger.info("on single update callback info: %s" % item)
  106. else:
  107. logger.error("on single update callback info: %s" % item)
  108. db_con.commit()
  109. db_con.close()
  110. except Exception:
  111. res['code'] = -1
  112. res['message'] = 'FAIL'
  113. logger.error(traceback.format_exc())
  114. # 返回接口结果
  115. self.write(json.dumps(res))
  116. self.flush()