luojunhui 2 روز پیش
والد
کامیت
bb79b9b1fb
5فایلهای تغییر یافته به همراه94 افزوده شده و 150 حذف شده
  1. 0 96
      README.md
  2. 1 0
      applications/service/__init__.py
  3. 81 0
      applications/service/get_cover.py
  4. 9 0
      applications/service/response.py
  5. 3 54
      routes/blueprint.py

+ 0 - 96
README.md

@@ -1,96 +0,0 @@
-# TouliuServer
-
-投流数据文章相关服务开发
-
-### 0. 启动服务
-
-    hypercorn touliu_app:app --config touliu.toml
-
-    nohup hypercorn touliu_app:app --config touliu.toml
-
-### 1. LogServer
-###### code: 日志码
-    rangs 1001 --> 9999
-    具体细节待设计
-###### env: 日志环境
-    prod: 正式环境
-    dev: 测试环境
-###### message: 日志信息
-    日志基础信息,类似于print
-###### route: 接口信息
-    用于划分某个具体的接口所执行的功能
-###### trace_id: 追踪id
-    touliu + "_" + uuid + "_" + timestamp
-###### data: 日志附加信息
-    一般存储传参,返回结果等json信息
-
-### 2.数据库设计
-###### top_videos_daily
-从pq每日获取单日回流top5000的视频
-
-    CREATE TABLE `top_videos_daily` (
-      `video_id` bigint(16) NOT NULL COMMENT '视频id',
-      `uid` bigint(16) DEFAULT NULL COMMENT '用户id',
-      `title` varchar(255) DEFAULT NULL COMMENT '视频标题',
-      `return_cnt` int(11) DEFAULT NULL COMMENT '回流',
-      `view_cnt` int(11) DEFAULT NULL COMMENT '曝光数量',
-      `share_cnt` int(11) DEFAULT NULL COMMENT '分享数量',
-      `category` varchar(16) DEFAULT NULL COMMENT '视频品类',
-      `rov` double DEFAULT NULL,
-      `ros` double DEFAULT NULL,
-      `status` int(11) DEFAULT NULL,
-      `dt` varchar(16) NOT NULL,
-      PRIMARY KEY (`video_id`,`dt`)
-    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='每日top5000的视频, 为投流业务服务'
-
-###### touliu_published_videos
-投流业务已经发布出去的视频的表现回收
-
-    CREATE TABLE `touliu_published_videos` (
-      `video_id` bigint(16) DEFAULT NULL COMMENT '视频id',
-      `root_source_id` varchar(155) DEFAULT NULL,
-      `business_type` varchar(16) DEFAULT NULL COMMENT '业务类型',
-      `play_first_level` bigint(16) DEFAULT NULL COMMENT '首层播放',
-      `play_fission` bigint(16) DEFAULT NULL COMMENT '裂变层播放',
-      `total_play` bigint(16) DEFAULT NULL COMMENT '总播放',
-      `return_first_level` bigint(16) DEFAULT NULL,
-      `return_fission` bigint(16) DEFAULT NULL,
-      `return_total` bigint(16) DEFAULT NULL,
-      `convert_first_level` bigint(16) DEFAULT NULL,
-      `convert_fission` bigint(16) DEFAULT NULL,
-      `convert_total` bigint(16) DEFAULT NULL,
-      `income_first_level` double DEFAULT NULL,
-      `income_fission` double DEFAULT NULL,
-      `income_total` double DEFAULT NULL,
-      `arpu_first_level` double DEFAULT NULL COMMENT '首层arpu',
-      `arpu_fission` double DEFAULT NULL COMMENT '裂变层arpu',
-      `arpu_total` double DEFAULT NULL COMMENT '总arpu',
-      `publish_dt` datetime DEFAULT NULL COMMENT '视频发布时间',
-      `origin_time` datetime DEFAULT NULL,
-      `recycle_dt` datetime DEFAULT NULL,
-      `uv_first_level` int(16) DEFAULT NULL,
-      `uv_fission` bigint(16) DEFAULT NULL,
-      `uv_total` bigint(16) DEFAULT NULL
-    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='投流已发布视频数据'
-
-###### touliu_source_id
-投流视频文章&&source_id表
-
-    CREATE TABLE `touliu_source_id` (
-      `video_id` bigint(16) DEFAULT NULL,
-      `video_cover` text COMMENT '视频封面url',
-      `video_title` varchar(255) DEFAULT NULL COMMENT '视频标题',
-      `uid` bigint(16) DEFAULT NULL,
-      `gh_id` varchar(32) DEFAULT NULL,
-      `account_name` varchar(255) DEFAULT NULL COMMENT '公众号名称',
-      `article_title` varchar(255) DEFAULT NULL COMMENT '文章标题',
-      `business_type` int(1) DEFAULT NULL COMMENT '1: 公众号投流,2:小程序投流, 3: 企微投流',
-      `publish_type` int(1) DEFAULT NULL COMMENT '1表示菜单栏, 2表示自动回复小程序卡片, 3表示自动回复文章',
-      `cooperation` varchar(255) DEFAULT NULL COMMENT '合作商',
-      `position` int(1) DEFAULT NULL COMMENT '位置',
-      `source_id` varchar(128) DEFAULT NULL,
-      `share_path` varchar(255) DEFAULT NULL COMMENT '分享路径',
-      `create_time` datetime DEFAULT NULL COMMENT '创建时间',
-      `minigram_name` varchar(255) DEFAULT NULL COMMENT '小程序名字',
-      `minigram_id` varchar(255) DEFAULT NULL COMMENT '小程序id'
-    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='投流专用source_id表'

+ 1 - 0
applications/service/__init__.py

@@ -0,0 +1 @@
+from .get_cover import GetCoverService

+ 81 - 0
applications/service/get_cover.py

@@ -0,0 +1,81 @@
+from applications.service.response import Response
+from applications.utils import fetch_channel_info
+from applications.utils import fetch_aigc_cover
+from applications.utils import fetch_long_video_cover
+
+
+class GetCoverService(Response):
+    def __init__(self, pool, params):
+        self.pool = pool
+        self.params = params
+        self.aigc_prefix = "http://res.cybertogether.net/"
+        self.pq_prefix = "https://rescdn.yishihui.com/"
+        self.suffix = "?x-oss-process=image/resize,m_fill,w_450,h_360/format,jpg"
+
+    async def montage_cover(self, oss_key, pool_name):
+        match pool_name:
+            case "aigc_db_pool":
+                return self.aigc_prefix + oss_key + self.suffix
+            case "long_video_db_pool":
+                return self.pq_prefix + oss_key + self.suffix
+            case _:
+                return None
+
+    async def get_cover(self, content_id: str):
+        channel_info = await fetch_channel_info(self.pool, content_id)
+        channel_content_id = channel_info[0]["channel_content_id"]
+        channel_type = channel_info[0]["channel"]
+        match channel_type:
+            case 5:
+                fetch_response = await fetch_aigc_cover(self.pool, channel_content_id)
+                if fetch_response:
+                    image_oss = fetch_response[0]["oss_object_key"]
+                    cover = await self.montage_cover(image_oss, "aigc_db_pool")
+                else:
+                    return self.error_response(
+                        error_code="402",
+                        error_message="can't find cover in aigc system",
+                    )
+            case 6:
+                fetch_response = await fetch_aigc_cover(self.pool, channel_content_id)
+                if fetch_response:
+                    image_oss = fetch_response[0]["oss_object_key"]
+                    cover = await self.montage_cover(image_oss, "aigc_db_pool")
+                else:
+                    return self.error_response(
+                        error_code="402",
+                        error_message="can't find cover in aigc system",
+                    )
+            case 10:
+                fetch_response = await fetch_long_video_cover(
+                    self.pool, channel_content_id
+                )
+                if fetch_response:
+                    image_oss = fetch_response[1]["image_path"]
+                    cover = await self.montage_cover(image_oss, "long_video_db_pool")
+                else:
+                    return self.error_response(
+                        error_code="402",
+                        error_message="can't find cover in pq",
+                    )
+            case _:
+                return self.error_response(
+                    error_code="403",
+                    error_message="channel_type is not supported",
+                )
+
+        return self.success_response(
+            data={
+                "cover": cover,
+            }
+        )
+
+    async def deal(self):
+        content_id = self.params.get("content_id")
+        if not content_id:
+            return self.error_response(
+                error_code="401",
+                error_message="content_id is required",
+            )
+        else:
+            return await self.get_cover(content_id)

+ 9 - 0
applications/service/response.py

@@ -0,0 +1,9 @@
+class Response:
+
+    @classmethod
+    def success_response(cls, data):
+        return {"code": 0, "status": "success", "data": data}
+
+    @classmethod
+    def error_response(cls, error_code, error_message):
+        return {"code": error_code, "status": "error", "message": error_message}

+ 3 - 54
routes/blueprint.py

@@ -1,10 +1,5 @@
 from quart import Blueprint, jsonify, request
-
-from applications.utils import (
-    fetch_aigc_cover,
-    fetch_channel_info,
-    fetch_long_video_cover,
-)
+from applications.service import GetCoverService
 
 server_blueprint = Blueprint("api", __name__, url_prefix="/api")
 
@@ -14,53 +9,7 @@ def server_routes(pools):
     @server_blueprint.route("/get_cover", methods=["POST"])
     async def get_cover():
         params = await request.get_json()
-        content_id = params.get("content_id")
-        if not content_id:
-            return jsonify({"code": 400, "msg": "content_id is required", "data": None})
-
-        # fetch content channel info
-        channel_info = await fetch_channel_info(pools, content_id)
-        channel_content_id, channel_type = channel_info[0]['channel_content_id'], channel_info[0]['channel']
-        match channel_type:
-            case 5:
-                # channel is gzh article
-                fetch_response = await fetch_aigc_cover(pools, channel_content_id)
-                cover_obj = fetch_response[0]
-                if cover_obj:
-                    image_oss = cover_obj['oss_object_key']
-                    cover = "http://res.cybertogether.net/" +  image_oss + "?x-oss-process=image/resize,m_fill,w_450,h_360/format,jpg"
-                else:
-                    return jsonify(
-                        {"code": 401, "msg": "can't find cover in aigc system", "data": None}
-                    )
-            case 6:
-                # channel is toutiao article
-                fetch_response = await fetch_aigc_cover(pools, channel_content_id)
-                cover_obj = fetch_response[0]
-                if cover_obj:
-                    image_oss = cover_obj['oss_object_key']
-                    cover = "http://res.cybertogether.net/" + image_oss + "?x-oss-process=image/resize,m_fill,w_450,h_360/format,jpg"
-                else:
-                    return jsonify(
-                        {"code": 401, "msg": "can't find cover in aigc system", "data": None}
-                    )
-            case 10:
-                # channel is long video
-                fetch_response = await fetch_long_video_cover(pools, channel_content_id)
-                cover_obj = fetch_response[1]
-                if cover_obj:
-                    image_oss = cover_obj['image_path']
-                    cover = "https://rescdn.yishihui.com/" + image_oss + "?x-oss-process=image/resize,m_fill,w_450,h_360/format,jpg"
-                else:
-                    return jsonify(
-                        {"code": 401, "msg": "can't find cover in pq", "data": None}
-                    )
-
-            case _:
-                return jsonify(
-                    {"code": 400, "msg": "channel_type is not supported", "data": None}
-                )
-
-        return jsonify({"code": 200, "msg": "success", "data": {"cover": cover}})
+        task = GetCoverService(pools, params)
+        return jsonify(await task.deal())
 
     return server_blueprint