ai_callback_handler.py 5.5 KB

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