Browse Source

forward.py 新增转发功能

罗俊辉 6 months ago
parent
commit
d3ace07c24

+ 10 - 0
applications/deal/record.py

@@ -6,6 +6,7 @@ import time
 from uuid import uuid4
 
 from applications.functions.log import logging
+from applications.functions.forward import forward_requests
 from static.config import db_article
 
 
@@ -13,6 +14,7 @@ class Record(object):
     """
     搜索接口处理逻辑
     """
+    NEW_STRATEGY = "strategy_v2"
 
     def __init__(self, params, mysql_client):
         self.content_id = None
@@ -20,6 +22,7 @@ class Record(object):
         self.contents = None
         self.title = None
         self.gh_id = None
+        self.strategy = None
         self.params = params
         self.mysql_client = mysql_client
         self.trace_id = "search-{}-{}".format(str(uuid4()), str(int(time.time())))
@@ -35,6 +38,7 @@ class Record(object):
             self.contents = self.params['content'].replace("'", "")
             self.account_name = self.params['accountName'].replace("'", "")
             self.content_id = self.params['articleId']
+            self.strategy = self.params['strategy']
             logging(
                 code="1001",
                 info="搜索视频内容接口请求成功, 参数校验成功",
@@ -90,6 +94,12 @@ class Record(object):
             return params_error
         else:
             # 记录
+            if self.strategy == self.NEW_STRATEGY:
+                response = await forward_requests(
+                    params=self.params,
+                    api="search_videos"
+                )
+                return response
             await self.input_queue()
             res = {
                 "status": "success input to article queue",

+ 14 - 1
applications/deal/response.py

@@ -12,14 +12,16 @@ import urllib.parse
 from applications.functions.log import logging
 from static.config import db_article
 from applications.functions.common import request_for_info
+from applications.functions.forward import forward_requests
 
 
 class Response(object):
     """
     召回逻辑
     """
+    NEW_STRATEGY = "strategy_v2"
 
-    def __init__(self, trace_id, mysql_client, mini_program_type):
+    def __init__(self, trace_id, mysql_client, mini_program_type, strategy="strategy_v1"):
         """
         长文: 25, 29, 31
         投流: 33
@@ -31,6 +33,7 @@ class Response(object):
         self.trace_id = trace_id
         self.mysql_client = mysql_client
         self.mini_program_type = mini_program_type
+        self.strategy = strategy
         self.mini_map = {
             # 25: {
             #     "avatar": "https://rescdn.yishihui.com/0temp/ttmhzfsh.png",
@@ -285,6 +288,16 @@ class Response(object):
         Recall Deal
         :return:
         """
+        if self.strategy == self.NEW_STRATEGY:
+            response = await forward_requests(
+                params={
+                    "traceId": self.trace_id,
+                    "miniprogramUseType": self.mini_program_type,
+                    "strategy": self.strategy
+                },
+                api="recall_videos"
+            )
+            return response
         response = await self.get_result()
         status_code = response.get("content_status")
         process_times = response.get("process_times")

+ 24 - 0
applications/functions/forward.py

@@ -0,0 +1,24 @@
+"""
+@author: luojunhui
+"""
+import requests
+
+NEW_IP = "47.98.154.124"
+NEW_PORT = "8111"
+
+
+async def forward_requests(params, api):
+    """
+    转发请求
+    :return:
+    """
+    new_url = "http://{}:{}/{}".format(NEW_IP, NEW_PORT, api)
+    response = requests.post(
+        url=new_url,
+        headers={
+            "Content-Type": "application/json",
+        },
+        json=params
+    )
+    return response.json()
+

+ 2 - 1
applications/routes.py

@@ -41,7 +41,8 @@ def Routes(mysql_client):
         data = await request.get_json()
         trace_id = data['traceId']
         minigram_type = data['miniprogramUseType']
-        RD = Response(trace_id=trace_id, mini_program_type=minigram_type, mysql_client=mysql_client)
+        strategy = data['strategy']
+        RD = Response(trace_id=trace_id, mini_program_type=minigram_type, mysql_client=mysql_client, strategy=strategy)
         response = await RD.deal()
         return jsonify(response)