ai_callback_handler.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import json
  2. import os
  3. import traceback
  4. import pymysql
  5. import tornado.web
  6. import yaml
  7. from loguru import logger
  8. logger.remove() # 删去 import logger之后自动产生的handler,不删除的话会出现重复输出的现象
  9. logger.add("/data/pythonProject/ai_target/logs/ai_callback.{time:YYYY-MM-DD}.log",
  10. rotation="00:00",
  11. format="{time:YYYY-MM-DD HH:mm:ss,SSS} [{process}] [{thread}] {level} {file} {line} - {message}",
  12. level="INFO")
  13. logger.info("ai_callback_server started!")
  14. with open('/data/pythonProject/ai_target/config/config.yaml', mode='r', encoding='utf-8') as f:
  15. config = yaml.load(f.read(), Loader=yaml.FullLoader)
  16. if os.getenv('LYY_DEV', 'unknown') == 'dev':
  17. db_info = config['jeecg_dev_db']
  18. else:
  19. db_info = config['jeecg_product_db']
  20. class AiCallBackAddCreative(tornado.web.RequestHandler):
  21. def post(self):
  22. res = {'code': 0,
  23. 'message': "SUCCESS"}
  24. data = self.request.body
  25. data = str(data, 'utf8')
  26. data = json.loads(data, encoding='utf8')
  27. logger.info("call back of add creative, the raw data from request is %s" % data)
  28. try:
  29. db_con = pymysql.connect(host=db_info['host'],
  30. port=db_info['port'],
  31. user=db_info['username'],
  32. password=db_info['password'],
  33. database=db_info['database'],
  34. charset='utf8')
  35. cursor = db_con.cursor()
  36. for item in data['callbackData']:
  37. if item['type'] == 1:
  38. # 自定义创意
  39. creative_uuid = item.get('creative_uuid')
  40. creative_id = item.get('creative_id')
  41. unit_id = item.get('unit_id')
  42. status = item.get('code')
  43. message = item.get('message')
  44. cursor.execute("""UPDATE ctop_ai_kuaishou_creative_level_operation_record
  45. SET unit_id= %s, creative_id=%s, status=%s, message=%s
  46. WHERE creative_uuid = %s""",
  47. (unit_id, creative_id, status, message, creative_uuid))
  48. if item['code'] == 0:
  49. logger.info("on single update callback info: %s" % item)
  50. else:
  51. logger.error("on single update callback info: %s" % item)
  52. if item['type'] == 2:
  53. # 程序化创意2.0
  54. creative_uuid = item.get('creative_uuid')
  55. unit_id = item.get('unit_id')
  56. status = item.get('code')
  57. message = item.get('message')
  58. cursor.execute("""UPDATE ctop_ai_kuaishou_program_creative_level_operation_record
  59. SET unit_id= %s, status=%s, message=%s
  60. WHERE creative_uuid = %s""",
  61. (unit_id, status, message, creative_uuid))
  62. if item['code'] == 0:
  63. logger.info("on single update callback info: %s" % item)
  64. else:
  65. logger.error("on single update callback info: %s" % item)
  66. db_con.commit()
  67. db_con.close()
  68. except Exception:
  69. res['code'] = -1
  70. res['message'] = 'FAIL'
  71. logger.error(traceback.format_exc())
  72. # 返回接口结果
  73. self.write(json.dumps(res))
  74. self.flush()
  75. class AiCallBackAddGroup(tornado.web.RequestHandler):
  76. def post(self):
  77. res = {'code': 0,
  78. 'message': "SUCCESS"}
  79. data = self.request.body
  80. data = str(data, 'utf8')
  81. data = json.loads(data, encoding='utf8')
  82. logger.info("call back of add group, the raw data from request is %s" % data)
  83. try:
  84. db_con = pymysql.connect(host=db_info['host'],
  85. port=db_info['port'],
  86. user=db_info['username'],
  87. password=db_info['password'],
  88. database=db_info['database'],
  89. charset='utf8')
  90. cursor = db_con.cursor()
  91. for item in data['callbackData']:
  92. group_uuid = item.get('group_uuid')
  93. unit_id = item.get('unit_id')
  94. group_create_time = item.get('group_create_time')
  95. status = item.get('code')
  96. message = item.get('message')
  97. cursor.execute("""UPDATE ctop_ai_kuaishou_unit_level_operation_record
  98. SET unit_id= %s, group_create_time=%s,status=%s,message=%s
  99. WHERE group_uuid = %s""", (unit_id, group_create_time, status, message, group_uuid))
  100. if item['code'] == 0:
  101. logger.info("on single update callback info: %s" % item)
  102. else:
  103. logger.error("on single update callback info: %s" % item)
  104. db_con.commit()
  105. db_con.close()
  106. except Exception:
  107. res['code'] = -1
  108. res['message'] = 'FAIL'
  109. logger.error(traceback.format_exc())
  110. # 返回接口结果
  111. self.write(json.dumps(res))
  112. self.flush()