Explorar o código

2024-06-12
第一版缓存搜索接口

罗俊辉 hai 10 meses
pai
achega
2368e3a214

+ 5 - 0
applications/deal/__init__.py

@@ -0,0 +1,5 @@
+"""
+@author: luojunhui
+"""
+from .search_deal import SearchDeal
+from .re_search_deal import ReSearchDeal

+ 78 - 0
applications/deal/re_search_deal.py

@@ -0,0 +1,78 @@
+"""
+@author: luojunhui
+"""
+import asyncio
+
+from applications.schedule import re_search_videos
+
+
+class ReSearchDeal(object):
+    """
+    重新搜索逻辑
+    obj = {
+        "ori_title": params['title'],
+        "content_title": params['kimi_summary'],
+        "content_keys": params['kimi_keys'],
+        "trace_id": params['trace_id']
+    }
+    """
+
+    def __init__(self, params):
+        self.gh_id = None
+        self.trace_id = None
+        self.kimi_keys = None
+        self.kimi_summary = None
+        self.title = None
+        self.params = params
+
+    def check_params(self):
+        """
+        check params
+        :return:
+        """
+        try:
+            self.title = self.params['title']
+            self.kimi_summary = self.params['kimi_summary']
+            self.kimi_keys = self.params['kimi_keys']
+            self.trace_id = self.params['trace_id']
+            self.gh_id = self.params['gh_id']
+            return None
+        except AttributeError as e:
+            result = {
+                "status": "fail",
+                "code": 1,
+                "message": str(e),
+                "info": "params check error"
+            }
+            return result
+        
+    async def deal(self):
+        """
+        research function deal
+        :return: 
+        """
+        params_error = self.check_params()
+        if params_error:
+            return params_error
+        else:
+            try:
+                asyncio.ensure_future(
+                    re_search_videos(
+                        self.params,
+                        self.trace_id,
+                        self.gh_id)
+                )
+                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
+            

+ 167 - 0
applications/deal/search_deal.py

@@ -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

+ 7 - 97
applications/routes.py

@@ -1,14 +1,12 @@
 """
 @author: luojunhui
 """
-import time
-import uuid
 import asyncio
 from quart import Blueprint, jsonify, request
 
 from applications.functions.log import logging
-from applications.schedule import recall_videos, search_videos, re_search_videos
-from applications.static.config import db_article
+from applications.schedule import recall_videos
+from applications.deal import SearchDeal, ReSearchDeal
 
 my_blueprint = Blueprint('LongArticles', __name__)
 
@@ -24,11 +22,6 @@ def Routes(mysql_client):
         Hello World Test
         :return:
         """
-        logging(
-            code="1001",
-            info="请求接口成功",
-            port="healthcheck"
-        )
         return jsonify({'message': 'Hello, World!'})
 
     @my_blueprint.route('/search_videos', methods=['POST'])
@@ -38,70 +31,9 @@ def Routes(mysql_client):
         :return:
         """
         params = await request.get_json()
-        try:
-            gh_id = params['ghId']
-            trace_id = "search-{}-{}".format(str(uuid.uuid4()), str(int(time.time())))
-            params['trace_id'] = trace_id
-            title = params['title'].split("@@")[-1].replace("'", "")
-            contents = params['content'].replace("'", "")
-            account_name = params['accountName'].replace("'", "")
-            logging(
-                code="2000",
-                info="搜索视频内容接口请求成功",
-                port="title_to_search",
-                function="search_videos_from_the_web",
-                trace_id=trace_id
-            )
-        except Exception as e:
-            result = {
-                "status": "fail",
-                "code": 1,
-                "message": str(e),
-                "info": "params check error"
-            }
-            return jsonify(result)
-        if "video_id=" in title:
-            video_id = title.split("video_id=")[-1]
-            insert_sql = f"""
-                            INSERT INTO {db_article}
-                                (trace_id, gh_id, article_title, article_text, account_name, recall_video_id1)
-                            VALUES 
-                                ('{trace_id}', '{gh_id}', '{title}', '{contents}', '{account_name}', '{video_id}');"""
-            await mysql_client.async_insert(insert_sql)
-            res = {
-                "status": "success",
-                "code": 0,
-                "traceId": trace_id
-            }
-            return jsonify(res)
-        else:
-            insert_sql = f"""
-                INSERT INTO {db_article}
-                    (trace_id, gh_id, article_title, article_text, account_name)
-                VALUES 
-                    ('{trace_id}', '{gh_id}', '{title}', '{contents}', '{account_name}');"""
-            await mysql_client.async_insert(insert_sql)
-            try:
-                asyncio.ensure_future(
-                    search_videos(
-                        params=params,
-                        trace_id=trace_id,
-                        gh_id=gh_id,
-                        mysql_client=mysql_client
-                    )
-                )
-                res = {
-                    "status": "success",
-                    "code": 0,
-                    "traceId": trace_id
-                }
-            except Exception as e:
-                res = {
-                    "status": "fail",
-                    "code": 1,
-                    "message": str(e)
-                }
-            return jsonify(res)
+        SD = SearchDeal(params=params, mysql_client=mysql_client)
+        result = await SD.deal()
+        return jsonify(result)
 
     @my_blueprint.route('/recall_videos', methods=['POST'])
     async def find_videos():
@@ -138,30 +70,8 @@ def Routes(mysql_client):
         :return:
         """
         params = await request.get_json()
-        gh_id = params['ghId']
-        trace_id = params['trace_id']
-        logging(
-            code="2000",
-            info="重新搜索视频内容接口请求成功",
-            port="title_to_search",
-            function="re_search_videos_from_the_web",
-            trace_id=trace_id
-        )
-
-        try:
-            asyncio.ensure_future(re_search_videos(params, trace_id, gh_id))
-            res = {
-                "status": "success",
-                "code": 0,
-                "traceId": trace_id
-            }
-            return jsonify(res)
-        except Exception as e:
-            res = {
-                "status": "fail",
-                "code": 1,
-                "message": str(e)
-            }
+        RSD = ReSearchDeal(params=params)
+        res = await RSD.deal()
         return jsonify(res)
 
     return my_blueprint

+ 2 - 2
applications/static/config.py

@@ -365,7 +365,7 @@ ab_test_config = {
 }
 
 # prod
-db_article = "long_articles_video"
+# db_article = "long_articles_video"
 
 # dev
-# db_article = "long_articles_video_dev"
+db_article = "long_articles_video_dev"