routes.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. """
  2. @author: luojunhui
  3. """
  4. import os
  5. import time
  6. import uuid
  7. import asyncio
  8. from quart import Blueprint, jsonify, request
  9. from applications.log import logging
  10. from applications.process import ProcessParams
  11. from applications.search import search_videos
  12. from applications.functions.common import find_videos_in_mysql, ask_kimi_and_save_to_local
  13. my_blueprint = Blueprint('kimi', __name__)
  14. @my_blueprint.route('/healthcheck')
  15. def hello():
  16. """
  17. Hello World Test
  18. :return:
  19. """
  20. logging(
  21. code="1001",
  22. info="请求接口成功",
  23. port="healthcheck"
  24. )
  25. return jsonify({'message': 'Hello, World!'})
  26. @my_blueprint.route('/title_to_search', methods=['POST'])
  27. async def search_videos_from_the_web():
  28. """
  29. 从web 搜索视频并且存储到票圈的视频库中
  30. :return:
  31. """
  32. params = await request.get_json()
  33. title = params['title']
  34. gh_id = params['ghId']
  35. trace_id = "search-{}-{}".format(str(uuid.uuid4()), str(int(time.time())))
  36. params['trace_id'] = trace_id
  37. logging(
  38. code="2000",
  39. info="搜索视频内容接口请求成功",
  40. port="title_to_search",
  41. function="search_videos_from_the_web",
  42. trace_id=trace_id
  43. )
  44. try:
  45. title_p = os.path.join(os.getcwd(), 'applications', 'static', "titles", "{}.json".format(title))
  46. if os.path.exists(title_p):
  47. logging(
  48. code="2001",
  49. info="该标题已经被 kimi 处理过,跳过请求 kimi 操作--- {}".format(title),
  50. function="search_videos_from_the_web",
  51. trace_id=trace_id
  52. )
  53. else:
  54. ask_kimi_and_save_to_local((title, trace_id, title_p))
  55. await asyncio.sleep(2)
  56. search_videos(
  57. title=title,
  58. video_path=title_p,
  59. trace_id=trace_id,
  60. gh_id=gh_id,
  61. )
  62. res = {
  63. "trace_id": trace_id,
  64. "code": 0
  65. }
  66. except Exception as e:
  67. res = {
  68. "trace_id": trace_id,
  69. "code": 1,
  70. "message": str(e)
  71. }
  72. return jsonify(res)
  73. @my_blueprint.route('/out_videos', methods=['POST'])
  74. async def find_in_mysql():
  75. """
  76. 搜索是否存在外站视频 video_list, 如果存在,则返回成功
  77. :return:
  78. """
  79. data = await request.get_json()
  80. trace_id = data['traceId']
  81. logging(
  82. code="2000",
  83. info="请求接口成功",
  84. port="title_to_video",
  85. trace_id=trace_id,
  86. function="find_in_mysql"
  87. )
  88. res = find_videos_in_mysql(trace_id=trace_id)
  89. return jsonify(res)
  90. @my_blueprint.route('/title_to_video', methods=['POST'])
  91. async def post_data():
  92. """
  93. 请求接口代码
  94. :return:
  95. """
  96. data = await request.get_json()
  97. trace_id = data['traceId']
  98. logging(
  99. code="1001",
  100. info="请求接口成功",
  101. port="title_to_video",
  102. trace_id=trace_id
  103. )
  104. p = ProcessParams(t_id=trace_id)
  105. processed_data = await p.deal(data)
  106. return jsonify(processed_data)