routes.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.functions.log import logging
  10. from applications.schedule import ProcessParams, search_videos
  11. from applications.functions.common import KimiServer, MySQLServer
  12. from applications.schedule.main_schedule import AskForInfo
  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'].replace("【非头次】", "").replace("【头次】", "")
  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. KimiServer().ask_kimi_and_save_to_local((title, trace_id, title_p))
  55. await asyncio.sleep(1)
  56. kimi_title = KimiServer().kimi_title(title)
  57. search_videos(
  58. title=title,
  59. video_path=title_p,
  60. trace_id=trace_id,
  61. gh_id=gh_id,
  62. )
  63. res = {
  64. "trace_id": trace_id,
  65. "code": 0,
  66. "kimi_title": kimi_title
  67. }
  68. except Exception as e:
  69. res = {
  70. "trace_id": trace_id,
  71. "code": 1,
  72. "message": str(e)
  73. }
  74. return jsonify(res)
  75. @my_blueprint.route('/out_videos', methods=['POST'])
  76. async def find_in_mysql():
  77. """
  78. 搜索是否存在外站视频 video_list, 如果存在,则返回成功
  79. :return:
  80. """
  81. data = await request.get_json()
  82. trace_id = data['traceId']
  83. logging(
  84. code="2000",
  85. info="请求接口成功",
  86. port="title_to_video",
  87. trace_id=trace_id,
  88. function="find_in_mysql"
  89. )
  90. res = MySQLServer().select_download_videos(trace_id=trace_id)
  91. return jsonify(res)
  92. @my_blueprint.route('/find_video', methods=['POST'])
  93. async def post_data():
  94. """
  95. 请求接口代码
  96. :return:
  97. """
  98. data = await request.get_json()
  99. trace_id = data['traceId']
  100. logging(
  101. code="1001",
  102. info="请求接口成功",
  103. port="title_to_video",
  104. trace_id=trace_id
  105. )
  106. p = ProcessParams(t_id=trace_id)
  107. processed_data = await p.deal(data)
  108. return jsonify(processed_data)
  109. @my_blueprint.route('/title_to_video', methods=['POST'])
  110. async def delay_response():
  111. """
  112. main
  113. :return:
  114. """
  115. # 从请求体中解析 JSON 数据
  116. data = await request.get_json()
  117. A = AskForInfo(data)
  118. res = A.schedule()
  119. return jsonify(res)