search_deal.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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={vid2},
  109. recall_video_id3={vid3}
  110. WHERE trace_id = '{self.trace_id}'
  111. """
  112. await self.mysql_client.async_insert(update_sql)
  113. async def get_history_contents(self):
  114. """
  115. check whether the content id exists
  116. :return:
  117. """
  118. select_sql = f"""
  119. SELECT recall_video_id1, recall_video_id2, recall_video_id3, kimi_title
  120. FROM {db_article}
  121. WHERE content_id = '{self.content_id}' and trace_id != '{self.trace_id}'
  122. ORDER BY id DESC;
  123. """
  124. result = await self.mysql_client.async_select(select_sql)
  125. if result:
  126. for item in result:
  127. video_1, video_2, video_3, kimi_title = item
  128. if video_1 and video_2 and video_3 and kimi_title:
  129. return [video_1, video_2, video_3, kimi_title]
  130. else:
  131. continue
  132. return None
  133. else:
  134. return None
  135. async def deal(self):
  136. """
  137. deal
  138. :return:
  139. """
  140. params_error = self.check_params()
  141. if params_error:
  142. return params_error
  143. else:
  144. # 记录
  145. await self.record()
  146. if "video_id=" in self.title:
  147. return await self.process_video_id()
  148. else:
  149. video_ids = await self.get_history_contents()
  150. if video_ids:
  151. logging(
  152. code="1004",
  153. info="获取历史到文章视频",
  154. data=video_ids,
  155. trace_id=self.trace_id
  156. )
  157. await self.insert_history_contents_videos(
  158. video_ids[0],
  159. video_ids[1],
  160. video_ids[2],
  161. video_ids[3]
  162. )
  163. return {"status": "success", "code": 0, "traceId": self.trace_id}
  164. else:
  165. # search from the Internet
  166. try:
  167. asyncio.ensure_future(
  168. search_videos(
  169. params={"title": self.title, "content": self.contents, "trace_id": self.trace_id},
  170. trace_id=self.trace_id,
  171. gh_id=self.gh_id,
  172. mysql_client=self.mysql_client
  173. )
  174. )
  175. res = {
  176. "status": "success",
  177. "code": 0,
  178. "traceId": self.trace_id
  179. }
  180. return res
  181. except Exception as e:
  182. res = {
  183. "status": "fail",
  184. "code": 1,
  185. "message": str(e)
  186. }
  187. return res