routes.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. """
  2. @author: luojunhui
  3. """
  4. import time
  5. import uuid
  6. import asyncio
  7. from quart import Blueprint, jsonify, request
  8. from applications.functions.log import logging
  9. from applications.schedule import recall_videos, search_videos, re_search_videos
  10. my_blueprint = Blueprint('LongArticles', __name__)
  11. def Routes(mysql_client):
  12. """
  13. 路由代码
  14. """
  15. @my_blueprint.route('/healthcheck')
  16. def healthcheck():
  17. """
  18. Hello World Test
  19. :return:
  20. """
  21. logging(
  22. code="1001",
  23. info="请求接口成功",
  24. port="healthcheck"
  25. )
  26. return jsonify({'message': 'Hello, World!'})
  27. @my_blueprint.route('/search_videos', methods=['POST'])
  28. async def search_videos_from_the_web():
  29. """
  30. 从web 搜索视频并且存储到票圈的视频库中
  31. :return:
  32. """
  33. params = await request.get_json()
  34. try:
  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. except Exception as e:
  49. result = {
  50. "status": "fail",
  51. "code": 1,
  52. "message": str(e),
  53. "info": "params check error"
  54. }
  55. return jsonify(result)
  56. if "video_id=" in title:
  57. video_id = title.split("video_id=")[-1]
  58. insert_sql = f"""
  59. INSERT INTO long_articles_video
  60. (trace_id, gh_id, article_title, article_text, account_name, recall_video_id1)
  61. VALUES
  62. ('{trace_id}', '{gh_id}', '{title}', '{contents}', '{account_name}', '{video_id}');"""
  63. await mysql_client.async_insert(insert_sql)
  64. res = {
  65. "status": "success",
  66. "code": 0,
  67. "traceId": trace_id
  68. }
  69. return jsonify(res)
  70. else:
  71. insert_sql = f"""
  72. INSERT INTO long_articles_video
  73. (trace_id, gh_id, article_title, article_text, account_name)
  74. VALUES
  75. ('{trace_id}', '{gh_id}', '{title}', '{contents}', '{account_name}');"""
  76. await mysql_client.async_insert(insert_sql)
  77. try:
  78. asyncio.ensure_future(
  79. search_videos(
  80. params=params,
  81. trace_id=trace_id,
  82. gh_id=gh_id,
  83. mysql_client=mysql_client
  84. )
  85. )
  86. res = {
  87. "status": "success",
  88. "code": 0,
  89. "traceId": trace_id
  90. }
  91. except Exception as e:
  92. res = {
  93. "status": "fail",
  94. "code": 1,
  95. "message": str(e)
  96. }
  97. return jsonify(res)
  98. @my_blueprint.route('/recall_videos', methods=['POST'])
  99. async def find_videos():
  100. """
  101. 请求接口代码
  102. :return:
  103. """
  104. data = await request.get_json()
  105. trace_id = data['traceId']
  106. logging(
  107. code="1001",
  108. info="请求接口成功",
  109. port="recall_videos",
  110. trace_id=trace_id
  111. )
  112. try:
  113. result = await recall_videos(
  114. trace_id=trace_id,
  115. mysql_client=mysql_client
  116. )
  117. print(result)
  118. except Exception as e:
  119. result = {
  120. "traceId": trace_id,
  121. "error": str(e)
  122. }
  123. print(result)
  124. return jsonify(result)
  125. @my_blueprint.route('/re_search_videos', methods=['POST'])
  126. async def ree_search_videos():
  127. """
  128. 重新搜索
  129. :return:
  130. """
  131. params = await request.get_json()
  132. gh_id = params['ghId']
  133. trace_id = params['trace_id']
  134. logging(
  135. code="2000",
  136. info="重新搜索视频内容接口请求成功",
  137. port="title_to_search",
  138. function="re_search_videos_from_the_web",
  139. trace_id=trace_id
  140. )
  141. try:
  142. asyncio.ensure_future(
  143. re_search_videos(
  144. params=params,
  145. trace_id=trace_id,
  146. gh_id=gh_id,
  147. mysql_client=mysql_client
  148. )
  149. )
  150. res = {
  151. "status": "success",
  152. "code": 0,
  153. "traceId": trace_id
  154. }
  155. except Exception as e:
  156. res = {
  157. "status": "fail",
  158. "code": 1,
  159. "message": str(e)
  160. }
  161. return jsonify(res)
  162. return my_blueprint