فهرست منبع

2025-01-06-oss_rank developing

luojunhui 3 ماه پیش
والد
کامیت
39cfc36717

+ 8 - 0
applications/const/server_const.py

@@ -62,5 +62,13 @@ class ServerConst:
     TASK_FAIL_CODE = 99
     TASK_PROCESSING_CODE = 101
 
+    # oss_rank
+    VIDEO_LIMIT = 3
+
+
+    # response code
+    SUCCESS_CODE = 0
+    FAIL_CODE = -1
+    PARAMS_CHECK_FAILED_CODE = -2
 
 

+ 3 - 1
applications/match_algorithm/__init__.py

@@ -1,4 +1,6 @@
 """
 @author: luojunhui
 匹配算法
-"""
+"""
+
+from .rank_by_fission_on_read import *

+ 60 - 0
applications/match_algorithm/rank_by_fission_on_read.py

@@ -0,0 +1,60 @@
+"""
+@author: luojunhui
+@software: vscode
+@file: rank_by_fission_on_read.py
+@time: 2025-01-04
+"""
+
+from typing import Dict, List
+
+from aiomysql.cursors import DictCursor
+
+
+async def get_content_id_fission_info(content_id_tuple: tuple[str], db_client, video_limit: int) -> List[Dict]:
+    """
+    获取内容id的裂变/阅读信息
+    :param content_id:
+    :return:
+    """
+    select_sql = f"""
+        SELECT 
+            l.oss_name, l.read_total, l.fission_level_0_total, l.fission_0_on_read 
+        FROM 
+            long_articles_videos_fission_info l 
+        LEFT JOIN 
+            article_re_match_record r 
+            ON l.oss_name = r.oss_path
+        WHERE 
+            l.content_id IN {content_id_tuple}
+            AND r.oss_path IS NULL 
+        ORDER BY l.fission_0_on_read DESC 
+        LIMIT {video_limit};
+    """
+    response = await db_client.async_select(select_sql, DictCursor)
+    print("get_content_id_fission_info", response)
+    return response
+
+
+async def get_history_content_ids(content_id: str, db_client) -> tuple[str]:
+    """
+    Retrieve historical content IDs related to the given content ID.
+    :param content_id: The ID of the content for which related content IDs are to be retrieved.
+    :param db_client: The database client used to execute the query.
+    :return: A tuple containing the historical content IDs.
+    """
+    select_sql = f"""
+        SELECT 
+            DISTINCT c2.content_id
+        FROM crawler_produce_id_map c
+        JOIN article_pool_promotion_source ap1 
+            ON c.channel_content_id = ap1.channel_content_id
+        JOIN article_pool_promotion_source ap2
+            ON ap1.root_produce_content_id = ap2.root_produce_content_id
+        JOIN crawler_produce_id_map c2
+            ON ap2.channel_content_id = c2.channel_content_id
+        WHERE c.content_id = '{content_id}';
+    """
+    response = await db_client.async_select(select_sql, DictCursor)
+    print("get_history_content_ids", response) 
+    response_tuple = tuple(item["content_id"] for item in response)
+    return response_tuple

+ 2 - 1
server/api/__init__.py

@@ -4,4 +4,5 @@
 from .get_off_videos import GetOffVideos
 from .minigram import Minigram
 from .response import Response
-from .record import Record
+from .record import Record
+from .oss_rank import OssRank

+ 78 - 0
server/api/oss_rank.py

@@ -0,0 +1,78 @@
+"""
+@author luojunhui
+@description: find top fission / read oss path list
+"""
+
+from applications.const import server_const
+from applications.match_algorithm import get_content_id_fission_info
+from applications.match_algorithm import get_history_content_ids
+
+
+class OssRank:
+    """
+    input: content_id
+    output: find the top fission / read oss path list
+    """
+    def __init__(self, db_client, params):
+        self.db_client = db_client
+        self.params = params
+        self.content_id = None
+
+    async def check_params(self):
+        """
+        check params
+        """
+        try:
+            self.content_id = self.params['contentId']
+            self.video_limit = self.params.get("videoLimit", server_const.VIDEO_LIMIT)
+            return
+        except KeyError as e:
+            result = {
+                "error": str(e),
+                "message": "invalid params",
+                "data": self.params,
+                "code": server_const.PARAMS_CHECK_FAILED_CODE
+            }
+            return result
+        
+    async def deal(self):
+        """
+        entrance of this class
+        """
+        params_error = await self.check_params()
+        if params_error:
+            return params_error
+        else:
+            history_content_tuple = await get_history_content_ids(
+                content_id=self.content_id, 
+                db_client=self.db_client
+                )
+            
+            if history_content_tuple:
+                oss_path_result = await get_content_id_fission_info(
+                    content_id_tuple=history_content_tuple, 
+                    db_client=self.db_client,
+                    video_limit=self.video_limit
+                    )
+                if oss_path_result:
+                    oss_path_list = [
+                        {
+                            "oss_name": i['oss_name'],
+                            "fission_0_on_read": i['fission_0_on_read']
+                        }
+                        for i in oss_path_result
+                    ]
+                    code = server_const.SUCCESS_CODE
+                else:
+                    oss_path_list = []
+                    code = server_const.FAIL_CODE
+                
+                return {
+                    "oss_path_list": oss_path_list,
+                    "code": code
+                }
+            else:
+                return {
+                    "oss_path_list": [],
+                    "code": server_const.FAIL_CODE
+                }

+ 12 - 2
server/routes.py

@@ -3,7 +3,7 @@
 """
 from quart import Blueprint, jsonify, request
 
-from server.api import Response, Record, Minigram, GetOffVideos
+from server.api import Response, Record, Minigram, GetOffVideos, OssRank
 
 my_blueprint = Blueprint('LongArticlesMatchServer', __name__)
 
@@ -68,5 +68,15 @@ def Routes(mysql_client, config):
         get_off_video = GetOffVideos(params=data, mysql_client=mysql_client, config=config)
         result = await get_off_video.deal()
         return jsonify(result)
-
+    
+    @my_blueprint.route("/oss_rank", methods=['POST'])
+    async def get_video_oss_rank():
+        """
+        获取content_id以及溯源id的oss路径信息并且通过 fission / read 排序
+        """
+        data = await request.get_json()
+        oss_rank = OssRank(params=data, db_client=mysql_client)
+        result = await oss_rank.deal()
+        return jsonify(result)
+    
     return my_blueprint