search_deal.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. """
  2. @author: luojunhui
  3. """
  4. import time
  5. import asyncio
  6. from uuid import uuid4
  7. from applications.functions.log import logging
  8. from applications.static.config import db_article
  9. from applications.schedule import search_videos
  10. class SearchDeal(object):
  11. """
  12. 搜索接口处理逻辑
  13. """
  14. def __init__(self, params, mysql_client):
  15. self.content_id = None
  16. self.account_name = None
  17. self.contents = None
  18. self.title = None
  19. self.gh_id = None
  20. self.params = params
  21. self.mysql_client = mysql_client
  22. self.trace_id = "search-{}-{}".format(str(uuid4()), str(int(time.time())))
  23. def check_params(self):
  24. """
  25. 检查请求params
  26. :return:
  27. """
  28. try:
  29. self.gh_id = self.params['ghId']
  30. self.title = self.params['title'].split("@@")[-1].replace("'", "")
  31. self.contents = self.params['content'].replace("'", "")
  32. self.account_name = self.params['accountName'].replace("'", "")
  33. self.content_id = self.params['articleId']
  34. logging(
  35. code="1001",
  36. info="搜索视频内容接口请求成功, 参数校验成功",
  37. port="title_to_search",
  38. trace_id=self.trace_id,
  39. data=self.params
  40. )
  41. return None
  42. except Exception as e:
  43. result = {
  44. "status": "fail",
  45. "code": 1,
  46. "message": str(e),
  47. "info": "params check error"
  48. }
  49. logging(
  50. code="4001",
  51. info="搜索视频内容接口请求成功, 参数校验失败",
  52. port="title_to_search",
  53. trace_id=self.trace_id,
  54. data=self.params
  55. )
  56. return result
  57. async def record(self):
  58. """
  59. 把数据插入
  60. :return:
  61. """
  62. insert_sql = f"""
  63. INSERT INTO {db_article}
  64. (trace_id, gh_id, article_title, article_text, account_name, content_id)
  65. VALUES
  66. ('{self.trace_id}', '{self.gh_id}', '{self.title}', '{self.contents}', '{self.account_name}', '{self.content_id}');"""
  67. await self.mysql_client.async_insert(insert_sql)
  68. logging(
  69. code="1002",
  70. info="成功记录请求数据到mysql中",
  71. trace_id=self.trace_id
  72. )
  73. async def process_video_id(self):
  74. """
  75. 如果video_id在标题中,则做特殊处理
  76. :return:
  77. """
  78. video_id = self.title.split("video_id=")[-1]
  79. update_sql = f"""
  80. UPDATE
  81. {db_article}
  82. SET
  83. recall_video_id1 = '{video_id}'
  84. WHERE
  85. trace_id = '{self.trace_id}';"""
  86. await self.mysql_client.async_insert(update_sql)
  87. res = {
  88. "status": "success",
  89. "code": 0,
  90. "traceId": self.trace_id
  91. }
  92. logging(
  93. code="1003",
  94. info="视频生成文本服务请求,video_id = {}".format(video_id),
  95. trace_id=self.trace_id
  96. )
  97. return res
  98. async def insert_history_contents_videos(self, vid1, vid2, vid3, kimi_title):
  99. """
  100. 插入历史视频id
  101. :return:
  102. """
  103. update_sql = f"""
  104. UPDATE {db_article}
  105. SET
  106. kimi_title='{kimi_title}',
  107. recall_video_id1={vid1},
  108. recall_video_id2={"NULL" if vid2 is None else vid2},
  109. recall_video_id3={"NULL" if vid3 is None else vid3}
  110. WHERE trace_id = '{self.trace_id}'
  111. """
  112. print(update_sql)
  113. await self.mysql_client.async_insert(update_sql)
  114. async def get_history_contents(self):
  115. """
  116. check whether the content id exists
  117. :return:
  118. """
  119. select_sql = f"""
  120. SELECT recall_video_id1, recall_video_id2, recall_video_id3, kimi_title
  121. FROM {db_article}
  122. WHERE content_id = '{self.content_id}' and trace_id != '{self.trace_id}'
  123. ORDER BY id DESC;
  124. """
  125. result = await self.mysql_client.async_select(select_sql)
  126. if result:
  127. for item in result:
  128. video_1, video_2, video_3, kimi_title = item
  129. if video_1 and kimi_title:
  130. return [video_1, video_2, video_3, kimi_title]
  131. else:
  132. continue
  133. return None
  134. else:
  135. return None
  136. async def deal(self):
  137. """
  138. deal
  139. :return:
  140. """
  141. params_error = self.check_params()
  142. if params_error:
  143. return params_error
  144. else:
  145. # 记录
  146. await self.record()
  147. if "video_id=" in self.title:
  148. return await self.process_video_id()
  149. else:
  150. video_ids = await self.get_history_contents()
  151. if video_ids:
  152. logging(
  153. code="1004",
  154. info="获取历史到文章视频",
  155. data=video_ids,
  156. trace_id=self.trace_id
  157. )
  158. await self.insert_history_contents_videos(
  159. video_ids[0],
  160. video_ids[1],
  161. video_ids[2],
  162. video_ids[3]
  163. )
  164. return {"status": "success", "code": 0, "traceId": self.trace_id}
  165. else:
  166. # search from the Internet
  167. try:
  168. asyncio.ensure_future(
  169. search_videos(
  170. params={"title": self.title, "content": self.contents, "trace_id": self.trace_id},
  171. trace_id=self.trace_id,
  172. gh_id=self.gh_id,
  173. mysql_client=self.mysql_client
  174. )
  175. )
  176. res = {
  177. "status": "success",
  178. "code": 0,
  179. "traceId": self.trace_id
  180. }
  181. return res
  182. except Exception as e:
  183. res = {
  184. "status": "fail",
  185. "code": 1,
  186. "message": str(e)
  187. }
  188. return res