Explorar el Código

账号排序- v0.1

罗俊辉 hace 10 meses
padre
commit
2ad6a26c5a

+ 6 - 0
alg.toml

@@ -0,0 +1,6 @@
+reload = true
+bind = "0.0.0.0:6060"
+workers = 4
+keep_alive_timeout = 120  # 保持连接的最大秒数,根据需要调整
+graceful_timeout = 30    # 重启或停止之前等待当前工作完成的时间
+loglevel = "debug"  # 日志级别

+ 33 - 0
alg_app.py

@@ -0,0 +1,33 @@
+"""
+@author: luojunhui
+"""
+from quart import Quart
+from routes import AlgRoutes
+from applications import AsyncMySQLClient
+
+app = Quart(__name__)
+AsyncMySQL = AsyncMySQLClient(app)
+app_routes = AlgRoutes(AsyncMySQL)
+app.register_blueprint(app_routes)
+
+
+@app.before_serving
+async def init_db():
+    """
+    初始化
+    :return:
+    """
+    await AsyncMySQL.init_pool()
+
+
+@app.after_serving
+async def close_db():
+    """
+    关闭连接
+    :return:
+    """
+    await AsyncMySQL.close_pool()
+
+
+if __name__ == '__main__':
+    app.run(debug=True)

+ 4 - 0
applications/__init__.py

@@ -0,0 +1,4 @@
+"""
+@author: luojunhui
+"""
+from .asyncMySQL import AsyncMySQLClient

+ 60 - 0
applications/asyncMySQL.py

@@ -0,0 +1,60 @@
+"""
+@author: luojunhui
+"""
+import aiomysql
+
+
+class AsyncMySQLClient(object):
+    """
+    Async MySQL
+    """
+
+    def __init__(self, app):
+        self.app = app
+
+    async def init_pool(self):
+        """
+        初始化连接
+        :return:
+        """
+        self.app.mysql_pool = await aiomysql.create_pool(
+            host='rm-bp1159bu17li9hi94.mysql.rds.aliyuncs.com',
+            port=3306,
+            user='crawler',
+            password='crawler123456@',
+            db='piaoquan-crawler',
+            charset='utf8mb4',
+            connect_timeout=120,
+        )
+        print("mysql init successfully")
+
+    async def close_pool(self):
+        """
+        关闭 mysql 连接
+        :return:
+        """
+        self.app.mysql_pool.close()
+        await self.app.mysql_pool.wait_closed()
+
+    async def async_select(self, sql):
+        """
+        select method
+        :param sql:
+        :return:
+        """
+        async with self.app.mysql_pool.acquire() as conn:
+            async with conn.cursor() as cursor:
+                await cursor.execute(sql)
+                result = await cursor.fetchall()
+                return result
+
+    async def async_insert(self, sql):
+        """
+        insert and update method
+        :param sql:
+        :return:
+        """
+        async with self.app.mysql_pool.acquire() as coon:
+            async with coon.cursor() as cursor:
+                await cursor.execute(sql)
+                await coon.commit()

+ 5 - 0
applications/config.py

@@ -0,0 +1,5 @@
+"""
+@author: luojunhui
+"""
+# 默认数据库表
+db_config = ""

+ 4 - 0
applications/functions/__init__.py

@@ -0,0 +1,4 @@
+"""
+@author: luojunhui
+"""
+from .article_account import ArticleRank

+ 33 - 0
applications/functions/article_account.py

@@ -0,0 +1,33 @@
+"""
+@author: luojunhui
+"""
+import requests
+
+
+class ArticleRank(object):
+    """
+    账号排序
+    """
+    url = "http://192.168.100.31:8179/score_list"
+
+    @classmethod
+    def rank(cls, account_list, text_list):
+        """
+        Rank
+        :param account_list:
+        :param text_list:
+        :return:
+        """
+        body = {
+            "account_nickname_list": account_list,
+            "text_list": text_list,
+            "max_time": None,
+            "min_time": None,
+            "interest_type": "by_avg",
+            "sim_type": "mean",
+            "rate": 0.1
+        }
+        response = requests.post(url=cls.url, headers={}, json=body).json()
+        return response
+
+

+ 128 - 0
routes/AccountArticleRank.py

@@ -0,0 +1,128 @@
+"""
+@author: luojunhui
+"""
+from applications.functions import ArticleRank
+
+
+class AccountArticleRank(object):
+    """
+    文章排序
+    """
+
+    def __init__(self, params, mysql_client):
+        """
+        :param params: 请求参数
+        :param mysql_client: 数据库链接池
+        """
+        self.publishArticleList = None
+        self.publishNum = None
+        self.strategy = None
+        self.ghId = None
+        self.accountName = None
+        self.accountId = None
+        self.params = params
+        self.mysql_client = mysql_client
+
+    async def check_params(self):
+        """
+        校验参数
+        :return:
+        """
+        try:
+            self.accountId = self.params['accountId']
+            self.accountName = self.params['accountName']
+            self.ghId = self.params['ghId']
+            self.strategy = self.params['strategy']
+            self.publishNum = self.params['publishNum']
+            self.publishArticleList = self.params['publishArticleList']
+            self.title_list = [i['title'] for i in self.publishArticleList]
+            self.content_list = [i['content'] for i in self.publishArticleList]
+            return None
+        except AttributeError as e:
+            response = {
+                "msg": "params error",
+                "info": "params check failed, params : {} is not correct".format(e),
+                "code": 0
+            }
+            return response
+
+    async def rank_v1(self):
+        """
+        Rank Version 1
+        :return:
+        """
+        rank_info = ArticleRank().rank(account_list=[self.accountName], text_list=self.title_list)
+        score_list = rank_info[self.accountName]['score_list']
+        for index, item in enumerate(self.publishArticleList):
+            item['score'] = score_list[index]
+        sorted_list = sorted(self.publishArticleList, key=lambda x: x['score'], reverse=True)
+        result = {
+            "accountId": self.accountId,
+            "accountName": self.accountName,
+            "ghId": self.ghId,
+            "strategy": self.strategy,
+            "publishNum": self.publishNum,
+            "rank_list": sorted_list[:self.publishNum]
+        }
+        response = {
+            "status": "Rank Success",
+            "data": result
+        }
+        return response
+
+
+    async def rank_v2(self):
+        """
+        Rank Version 2
+        :return:
+        """
+        return
+
+    async def rank_v3(self):
+        """
+        Rank Version 3
+        :return:
+        """
+        return
+
+    async def rank_v4(self):
+        """
+        Rank Version 4
+        :return:
+        """
+        return
+
+    async def rank_v5(self):
+        """
+        Rank Version 5
+        :return:
+        """
+        return
+
+    async def choose_strategy(self):
+        """
+        选择排序策略
+        :return:
+        """
+        match self.strategy:
+            case "ArticleRankV1":
+                return await self.rank_v1()
+            case "ArticleRankV2":
+                return await self.rank_v2()
+            case "ArticleRankV3":
+                return await self.rank_v3()
+            case "ArticleRankV4":
+                return await self.rank_v4()
+            case "ArticleRankV5":
+                return await self.rank_v5()
+
+    async def deal(self):
+        """
+        Deal Function
+        :return:
+        """
+        error_params = await self.check_params()
+        if error_params:
+            return error_params
+        else:
+            return await self.choose_strategy()

+ 41 - 0
routes/__init__.py

@@ -0,0 +1,41 @@
+"""
+@author: luojunhui
+"""
+
+from quart import Blueprint, jsonify, request
+
+from .AccountArticleRank import AccountArticleRank
+
+blueprint = Blueprint('LongArticlesAlgServer', __name__)
+
+
+def AlgRoutes(mysql_client):
+    """
+    ALG ROUTES
+    :return:
+    """
+
+    @blueprint.route("/healthCheck")
+    def helloFuture():
+        """
+        测试服务连通性
+        :return:
+        """
+        response = {
+            "msg": "Hello, World! Hello, Future"
+        }
+        return jsonify(response)
+
+    @blueprint.route("/articleRank", methods=["POST"])
+    async def articleRankRoute():
+        """
+        文章排序接口
+        :return:
+        """
+        params = await request.get_json()
+        AAR = AccountArticleRank(params, mysql_client=mysql_client)
+        response = await AAR.deal()
+        print(response)
+        return jsonify(response)
+
+    return blueprint

La diferencia del archivo ha sido suprimido porque es demasiado grande
+ 16 - 0
test/rank_dev.py


Algunos archivos no se mostraron porque demasiados archivos cambiaron en este cambio