routes.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. insert_sql = f"""
  60. INSERT INTO long_articles_video
  61. (trace_id, gh_id, article_title, article_text, account_name, recall_video_id1)
  62. VALUES
  63. ('{trace_id}', '{gh_id}', '{title}', '{contents}', '{account_name}', '{video_id}');"""
  64. await mysql_client.async_insert(insert_sql)
  65. res = {
  66. "status": "success",
  67. "code": 0,
  68. "traceId": trace_id
  69. }
  70. return jsonify(res)
  71. else:
  72. insert_sql = f"""
  73. INSERT INTO long_articles_video
  74. (trace_id, gh_id, article_title, article_text, account_name)
  75. VALUES
  76. ('{trace_id}', '{gh_id}', '{title}', '{contents}', '{account_name}');"""
  77. await mysql_client.async_insert(insert_sql)
  78. try:
  79. asyncio.ensure_future(
  80. search_videos(
  81. params=params,
  82. trace_id=trace_id,
  83. gh_id=gh_id,
  84. mysql_client=mysql_client
  85. )
  86. )
  87. res = {
  88. "status": "success",
  89. "code": 0,
  90. "traceId": trace_id
  91. }
  92. except Exception as e:
  93. res = {
  94. "status": "fail",
  95. "code": 1,
  96. "message": str(e)
  97. }
  98. return jsonify(res)
  99. @my_blueprint.route('/recall_videos', methods=['POST'])
  100. async def find_videos():
  101. """
  102. 请求接口代码
  103. :return:
  104. """
  105. data = await request.get_json()
  106. trace_id = data['traceId']
  107. logging(
  108. code="1001",
  109. info="请求接口成功",
  110. port="recall_videos",
  111. trace_id=trace_id
  112. )
  113. try:
  114. result = await recall_videos(
  115. trace_id=trace_id,
  116. mysql_client=mysql_client
  117. )
  118. print(result)
  119. except Exception as e:
  120. result = {
  121. "traceId": trace_id,
  122. "error": str(e)
  123. }
  124. print(result)
  125. return jsonify(result)
  126. @my_blueprint.route('/re_search_videos', methods=['POST'])
  127. async def re_search_videos():
  128. """
  129. 重新搜索
  130. :return:
  131. """
  132. params = await request.get_json()
  133. gh_id = params['ghId']
  134. trace_id = params['trace_id']
  135. logging(
  136. code="2000",
  137. info="重新搜索视频内容接口请求成功",
  138. port="title_to_search",
  139. function="re_search_videos_from_the_web",
  140. trace_id=trace_id
  141. )
  142. try:
  143. asyncio.ensure_future(
  144. search_videos(
  145. params=params,
  146. trace_id=trace_id,
  147. gh_id=gh_id,
  148. mysql_client=mysql_client
  149. )
  150. )
  151. res = {
  152. "status": "success",
  153. "code": 0,
  154. "traceId": trace_id
  155. }
  156. except Exception as e:
  157. res = {
  158. "status": "fail",
  159. "code": 1,
  160. "message": str(e)
  161. }
  162. return jsonify(res)
  163. return my_blueprint