routes.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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, return_info_v2
  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. try:
  36. gh_id = params['ghId']
  37. trace_id = "search-{}-{}".format(str(uuid.uuid4()), str(int(time.time())))
  38. params['trace_id'] = trace_id
  39. title = params['title'].split("@@")[-1].replace("'", "")
  40. contents = params['content'].replace("'", "")
  41. account_name = params['accountName'].replace("'", "")
  42. logging(
  43. code="2000",
  44. info="搜索视频内容接口请求成功",
  45. port="title_to_search",
  46. function="search_videos_from_the_web",
  47. trace_id=trace_id
  48. )
  49. except Exception as e:
  50. result = {
  51. "status": "fail",
  52. "code": 1,
  53. "message": str(e),
  54. "info": "params check error"
  55. }
  56. return jsonify(result)
  57. if "video_id=" in title:
  58. video_id = title.split("video_id=")[-1]
  59. result = await return_info_v2(video_id=video_id, trace_id=trace_id)
  60. return jsonify(result)
  61. else:
  62. insert_sql = f"""
  63. INSERT INTO long_articles_video
  64. (trace_id, gh_id, article_title, article_text, account_name)
  65. VALUES
  66. ('{trace_id}', '{gh_id}', '{title}', '{contents}', '{account_name}');"""
  67. await mysql_client.async_insert(insert_sql)
  68. try:
  69. asyncio.ensure_future(
  70. search_videos(
  71. params=params,
  72. trace_id=trace_id,
  73. gh_id=gh_id,
  74. mysql_client=mysql_client
  75. )
  76. )
  77. res = {
  78. "status": "success",
  79. "code": 0,
  80. "traceId": trace_id
  81. }
  82. except Exception as e:
  83. res = {
  84. "status": "fail",
  85. "code": 1,
  86. "message": str(e)
  87. }
  88. return jsonify(res)
  89. @my_blueprint.route('/recall_videos', methods=['POST'])
  90. async def find_videos():
  91. """
  92. 请求接口代码
  93. :return:
  94. """
  95. data = await request.get_json()
  96. trace_id = data['traceId']
  97. logging(
  98. code="1001",
  99. info="请求接口成功",
  100. port="recall_videos",
  101. trace_id=trace_id
  102. )
  103. try:
  104. result = await recall_videos(
  105. trace_id=trace_id,
  106. mysql_client=mysql_client
  107. )
  108. print(result)
  109. except Exception as e:
  110. result = {
  111. "traceId": trace_id,
  112. "error": str(e)
  113. }
  114. print(result)
  115. return jsonify(result)
  116. @my_blueprint.route('/re_search_videos', methods=['POST'])
  117. async def re_search_videos():
  118. """
  119. 重新搜索
  120. :return:
  121. """
  122. params = await request.get_json()
  123. gh_id = params['ghId']
  124. trace_id = params['trace_id']
  125. logging(
  126. code="2000",
  127. info="重新搜索视频内容接口请求成功",
  128. port="title_to_search",
  129. function="re_search_videos_from_the_web",
  130. trace_id=trace_id
  131. )
  132. try:
  133. asyncio.ensure_future(
  134. search_videos(
  135. params=params,
  136. trace_id=trace_id,
  137. gh_id=gh_id,
  138. mysql_client=mysql_client
  139. )
  140. )
  141. res = {
  142. "status": "success",
  143. "code": 0,
  144. "traceId": trace_id
  145. }
  146. except Exception as e:
  147. res = {
  148. "status": "fail",
  149. "code": 1,
  150. "message": str(e)
  151. }
  152. return jsonify(res)
  153. return my_blueprint