|
@@ -0,0 +1,167 @@
|
|
|
+"""
|
|
|
+@author: luojunhui
|
|
|
+"""
|
|
|
+import time
|
|
|
+import asyncio
|
|
|
+
|
|
|
+from uuid import uuid4
|
|
|
+
|
|
|
+from applications.functions.log import logging
|
|
|
+from applications.static.config import db_article
|
|
|
+from applications.schedule import search_videos
|
|
|
+
|
|
|
+
|
|
|
+class SearchDeal(object):
|
|
|
+ """
|
|
|
+ 搜索接口处理逻辑
|
|
|
+ """
|
|
|
+
|
|
|
+ def __init__(self, params, mysql_client):
|
|
|
+ self.content_id = None
|
|
|
+ self.account_name = None
|
|
|
+ self.contents = None
|
|
|
+ self.title = None
|
|
|
+ self.gh_id = None
|
|
|
+ self.params = params
|
|
|
+ self.mysql_client = mysql_client
|
|
|
+ self.trace_id = "search-{}-{}".format(str(uuid4()), str(int(time.time())))
|
|
|
+
|
|
|
+ def check_params(self):
|
|
|
+ """
|
|
|
+ 检查请求params
|
|
|
+ :return:
|
|
|
+ """
|
|
|
+ try:
|
|
|
+ self.gh_id = self.params['ghId']
|
|
|
+ self.title = self.params['title'].split("@@")[-1].replace("'", "")
|
|
|
+ self.contents = self.params['content'].replace("'", "")
|
|
|
+ self.account_name = self.params['accountName'].replace("'", "")
|
|
|
+ self.content_id = self.params['articleId']
|
|
|
+ logging(
|
|
|
+ code="2000",
|
|
|
+ info="搜索视频内容接口请求成功",
|
|
|
+ port="title_to_search",
|
|
|
+ function="search_videos_from_the_web",
|
|
|
+ trace_id=self.trace_id
|
|
|
+ )
|
|
|
+ return None
|
|
|
+ except Exception as e:
|
|
|
+ result = {
|
|
|
+ "status": "fail",
|
|
|
+ "code": 1,
|
|
|
+ "message": str(e),
|
|
|
+ "info": "params check error"
|
|
|
+ }
|
|
|
+ return result
|
|
|
+
|
|
|
+ async def record(self):
|
|
|
+ """
|
|
|
+ 把数据插入
|
|
|
+ :return:
|
|
|
+ """
|
|
|
+ insert_sql = f"""
|
|
|
+ INSERT INTO {db_article}
|
|
|
+ (trace_id, gh_id, article_title, article_text, account_name)
|
|
|
+ VALUES
|
|
|
+ ('{self.trace_id}', '{self.gh_id}', '{self.title}', '{self.contents}', '{self.account_name}');"""
|
|
|
+ await self.mysql_client.async_insert(insert_sql)
|
|
|
+
|
|
|
+ async def process_video_id(self):
|
|
|
+ """
|
|
|
+ 如果video_id在标题中,则做特殊处理
|
|
|
+ :return:
|
|
|
+ """
|
|
|
+ video_id = self.title.split("video_id=")[-1]
|
|
|
+ update_sql = f"""
|
|
|
+ UPDATE
|
|
|
+ {db_article}
|
|
|
+ SET
|
|
|
+ recall_video_id1 = '{video_id}'
|
|
|
+ WHERE
|
|
|
+ trace_id = '{self.trace_id}';"""
|
|
|
+ await self.mysql_client.async_insert(update_sql)
|
|
|
+ res = {
|
|
|
+ "status": "success",
|
|
|
+ "code": 0,
|
|
|
+ "traceId": self.trace_id
|
|
|
+ }
|
|
|
+ return res
|
|
|
+
|
|
|
+ async def insert_history_contents_videos(self, vid1, vid2, vid3):
|
|
|
+ """
|
|
|
+ 插入历史视频id
|
|
|
+ :return:
|
|
|
+ """
|
|
|
+ update_sql = f"""
|
|
|
+ UPDATE {db_article}
|
|
|
+ SET
|
|
|
+ recall_video_id1={vid1}, recall_video_id2={vid2}, recall_video_id3={vid3}
|
|
|
+ WHERE trace_id = {self.trace_id}
|
|
|
+ """
|
|
|
+ self.mysql_client.async_insert(update_sql)
|
|
|
+
|
|
|
+ async def get_history_contents(self):
|
|
|
+ """
|
|
|
+ check whether the content id exists
|
|
|
+ :return:
|
|
|
+ """
|
|
|
+ select_sql = f"""
|
|
|
+ SELECT recall_video_id1, recall_video_id2, recall_video_id3
|
|
|
+ FROM {db_article}
|
|
|
+ WHERE content_id = '{self.content_id}'
|
|
|
+ ORDER BY id DESC;
|
|
|
+ """
|
|
|
+ result = await self.mysql_client.async_select(select_sql)[0]
|
|
|
+ video_1, video_2, video_3 = result
|
|
|
+ if video_1 and video_2 and video_3:
|
|
|
+ return [video_1, video_2, video_3]
|
|
|
+ else:
|
|
|
+ return None
|
|
|
+
|
|
|
+ async def deal(self):
|
|
|
+ """
|
|
|
+ deal
|
|
|
+ :return:
|
|
|
+ """
|
|
|
+ params_error = self.check_params()
|
|
|
+ if params_error:
|
|
|
+ return params_error
|
|
|
+ else:
|
|
|
+ # 记录
|
|
|
+ await self.record()
|
|
|
+ # 处理video_id
|
|
|
+ if "video_id=" in self.title:
|
|
|
+ return await self.process_video_id()
|
|
|
+ else:
|
|
|
+ video_ids = await self.get_history_contents()
|
|
|
+ if video_ids:
|
|
|
+ await self.insert_history_contents_videos(
|
|
|
+ video_ids[0],
|
|
|
+ video_ids[1],
|
|
|
+ video_ids[2]
|
|
|
+ )
|
|
|
+ return {"status": "success", "code": 0, "traceId": self.trace_id}
|
|
|
+ else:
|
|
|
+ # search from the Internet
|
|
|
+ try:
|
|
|
+ asyncio.ensure_future(
|
|
|
+ search_videos(
|
|
|
+ params={"title": self.title, "content": self.contents, "trace_id": self.trace_id},
|
|
|
+ trace_id=self.trace_id,
|
|
|
+ gh_id=self.gh_id,
|
|
|
+ mysql_client=self.mysql_client
|
|
|
+ )
|
|
|
+ )
|
|
|
+ res = {
|
|
|
+ "status": "success",
|
|
|
+ "code": 0,
|
|
|
+ "traceId": self.trace_id
|
|
|
+ }
|
|
|
+ return res
|
|
|
+ except Exception as e:
|
|
|
+ res = {
|
|
|
+ "status": "fail",
|
|
|
+ "code": 1,
|
|
|
+ "message": str(e)
|
|
|
+ }
|
|
|
+ return res
|