search_deal.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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="2000",
  36. info="搜索视频内容接口请求成功",
  37. port="title_to_search",
  38. function="search_videos_from_the_web",
  39. trace_id=self.trace_id
  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. return result
  50. async def record(self):
  51. """
  52. 把数据插入
  53. :return:
  54. """
  55. insert_sql = f"""
  56. INSERT INTO {db_article}
  57. (trace_id, gh_id, article_title, article_text, account_name)
  58. VALUES
  59. ('{self.trace_id}', '{self.gh_id}', '{self.title}', '{self.contents}', '{self.account_name}');"""
  60. await self.mysql_client.async_insert(insert_sql)
  61. async def process_video_id(self):
  62. """
  63. 如果video_id在标题中,则做特殊处理
  64. :return:
  65. """
  66. video_id = self.title.split("video_id=")[-1]
  67. update_sql = f"""
  68. UPDATE
  69. {db_article}
  70. SET
  71. recall_video_id1 = '{video_id}'
  72. WHERE
  73. trace_id = '{self.trace_id}';"""
  74. await self.mysql_client.async_insert(update_sql)
  75. res = {
  76. "status": "success",
  77. "code": 0,
  78. "traceId": self.trace_id
  79. }
  80. return res
  81. async def insert_history_contents_videos(self, vid1, vid2, vid3):
  82. """
  83. 插入历史视频id
  84. :return:
  85. """
  86. update_sql = f"""
  87. UPDATE {db_article}
  88. SET
  89. recall_video_id1={vid1}, recall_video_id2={vid2}, recall_video_id3={vid3}
  90. WHERE trace_id = {self.trace_id}
  91. """
  92. self.mysql_client.async_insert(update_sql)
  93. async def get_history_contents(self):
  94. """
  95. check whether the content id exists
  96. :return:
  97. """
  98. select_sql = f"""
  99. SELECT recall_video_id1, recall_video_id2, recall_video_id3
  100. FROM {db_article}
  101. WHERE content_id = '{self.content_id}'
  102. ORDER BY id DESC;
  103. """
  104. result = await self.mysql_client.async_select(select_sql)[0]
  105. video_1, video_2, video_3 = result
  106. if video_1 and video_2 and video_3:
  107. return [video_1, video_2, video_3]
  108. else:
  109. return None
  110. async def deal(self):
  111. """
  112. deal
  113. :return:
  114. """
  115. params_error = self.check_params()
  116. if params_error:
  117. return params_error
  118. else:
  119. # 记录
  120. await self.record()
  121. # 处理video_id
  122. if "video_id=" in self.title:
  123. return await self.process_video_id()
  124. else:
  125. video_ids = await self.get_history_contents()
  126. if video_ids:
  127. await self.insert_history_contents_videos(
  128. video_ids[0],
  129. video_ids[1],
  130. video_ids[2]
  131. )
  132. return {"status": "success", "code": 0, "traceId": self.trace_id}
  133. else:
  134. # search from the Internet
  135. try:
  136. asyncio.ensure_future(
  137. search_videos(
  138. params={"title": self.title, "content": self.contents, "trace_id": self.trace_id},
  139. trace_id=self.trace_id,
  140. gh_id=self.gh_id,
  141. mysql_client=self.mysql_client
  142. )
  143. )
  144. res = {
  145. "status": "success",
  146. "code": 0,
  147. "traceId": self.trace_id
  148. }
  149. return res
  150. except Exception as e:
  151. res = {
  152. "status": "fail",
  153. "code": 1,
  154. "message": str(e)
  155. }
  156. return res