routes.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. my_blueprint = Blueprint('LongArticles', __name__)
  12. def Routes(mysql_client):
  13. """
  14. 路由代码
  15. """
  16. @my_blueprint.route('/healthcheck')
  17. def healthcheck():
  18. """
  19. Hello World Test
  20. :return:
  21. """
  22. logging(
  23. code="1001",
  24. info="请求接口成功",
  25. port="healthcheck"
  26. )
  27. return jsonify({'message': 'Hello, World!'})
  28. @my_blueprint.route('/search_videos', methods=['POST'])
  29. async def search_videos_from_the_web():
  30. """
  31. 从web 搜索视频并且存储到票圈的视频库中
  32. :return:
  33. """
  34. params = await request.get_json()
  35. gh_id = params['ghId']
  36. trace_id = "search-{}-{}".format(str(uuid.uuid4()), str(int(time.time())))
  37. params['trace_id'] = trace_id
  38. title = params['title'].split("@@")[-1].replace("'", "")
  39. contents = params['content'].replace("'", "")
  40. account_name = params['accountName'].replace("'", "")
  41. logging(
  42. code="2000",
  43. info="搜索视频内容接口请求成功",
  44. port="title_to_search",
  45. function="search_videos_from_the_web",
  46. trace_id=trace_id
  47. )
  48. insert_sql = f"""
  49. INSERT INTO long_articles_video_dev
  50. (trace_id, gh_id, article_title, article_text, account_name)
  51. VALUES
  52. ('{trace_id}', '{gh_id}', '{title}', '{contents}', '{account_name}');"""
  53. await mysql_client.async_insert(insert_sql)
  54. try:
  55. asyncio.ensure_future(
  56. search_videos(
  57. params=params,
  58. trace_id=trace_id,
  59. gh_id=gh_id,
  60. mysql_client=mysql_client
  61. )
  62. )
  63. res = {
  64. "status": "success",
  65. "code": 0,
  66. "traceId": trace_id
  67. }
  68. except Exception as e:
  69. res = {
  70. "status": "fail",
  71. "code": 1,
  72. "message": str(e)
  73. }
  74. return jsonify(res)
  75. @my_blueprint.route('/recall_videos', methods=['POST'])
  76. async def find_videos():
  77. """
  78. 请求接口代码
  79. :return:
  80. """
  81. data = await request.get_json()
  82. trace_id = data['traceId']
  83. logging(
  84. code="1001",
  85. info="请求接口成功",
  86. port="recall_videos",
  87. trace_id=trace_id
  88. )
  89. try:
  90. result = await recall_videos(
  91. trace_id=trace_id,
  92. mysql_client=mysql_client
  93. )
  94. except Exception as e:
  95. result = {
  96. "traceId": trace_id,
  97. "error": e
  98. }
  99. return jsonify(result)
  100. return my_blueprint