routes.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. """
  2. @author: luojunhui
  3. """
  4. import json
  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 recall_videos, search_videos
  11. from applications.functions.kimi import KimiServer
  12. my_blueprint = Blueprint('LongArticles', __name__)
  13. def Routes(mysql_client):
  14. """
  15. 路由代码
  16. """
  17. @my_blueprint.route('/healthcheck')
  18. def healthcheck():
  19. """
  20. Hello World Test
  21. :return:
  22. """
  23. logging(
  24. code="1001",
  25. info="请求接口成功",
  26. port="healthcheck"
  27. )
  28. return jsonify({'message': 'Hello, World!'})
  29. @my_blueprint.route('/search_videos', methods=['POST'])
  30. async def search_videos_from_the_web():
  31. """
  32. 从web 搜索视频并且存储到票圈的视频库中
  33. :return:
  34. """
  35. params = await request.get_json()
  36. K = KimiServer()
  37. gh_id = params['ghId']
  38. trace_id = "search-{}-{}".format(str(uuid.uuid4()), str(int(time.time())))
  39. params['trace_id'] = trace_id
  40. title = params['title'].split("@@")[-1].replace("'", "")
  41. contents = params['content'].replace("'", "")
  42. account_name = params['accountName'].replace("'", "")
  43. logging(
  44. code="2000",
  45. info="搜索视频内容接口请求成功",
  46. port="title_to_search",
  47. function="search_videos_from_the_web",
  48. trace_id=trace_id
  49. )
  50. insert_sql = f"""
  51. INSERT INTO long_articles_video_dev
  52. (trace_id, gh_id, article_title, article_text, account_name)
  53. VALUES
  54. ('{trace_id}', '{gh_id}', '{title}', '{contents}', '{account_name}');"""
  55. await mysql_client.async_insert(insert_sql)
  56. try:
  57. kimi_info = await K.search_kimi_schedule(params=params)
  58. print(json.dumps(kimi_info, ensure_ascii=False, indent=4))
  59. kimi_title = kimi_info['k_title']
  60. content_title = kimi_info['content_title']
  61. content_keys = json.dumps(kimi_info['content_keys'], ensure_ascii=False)
  62. update_kimi_sql = f"""
  63. UPDATE long_articles_video_dev SET
  64. kimi_title = '{kimi_title}',
  65. kimi_summary = '{content_title}',
  66. kimi_keys = '{content_keys}'
  67. WHERE trace_id = '{trace_id}';
  68. """
  69. await mysql_client.async_insert(update_kimi_sql)
  70. asyncio.ensure_future(
  71. search_videos(
  72. kimi_info=kimi_info,
  73. trace_id=trace_id,
  74. gh_id=gh_id,
  75. mysql_client=mysql_client
  76. )
  77. )
  78. res = {
  79. "status": "success",
  80. "code": 0,
  81. "traceId": trace_id
  82. }
  83. except Exception as e:
  84. res = {
  85. "status": "fail",
  86. "code": 1,
  87. "message": str(e)
  88. }
  89. return jsonify(res)
  90. @my_blueprint.route('/recall_videos', methods=['POST'])
  91. async def find_videos():
  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="recall_videos",
  102. trace_id=trace_id
  103. )
  104. try:
  105. result = await recall_videos(
  106. trace_id=trace_id,
  107. mysql_client=mysql_client
  108. )
  109. except Exception as e:
  110. result = {
  111. "traceId": trace_id,
  112. "error": e
  113. }
  114. return jsonify(result)
  115. return my_blueprint