ソースを参照

更新 share_url
更新 logo.png

罗俊辉 1 年間 前
コミット
cb69d98c43
6 ファイル変更206 行追加66 行削除
  1. 12 1
      app.py
  2. 10 1
      applications/functions/calculate.py
  3. 53 0
      applications/log.py
  4. 114 61
      applications/process.py
  5. 16 2
      applications/routes.py
  6. 1 1
      test.py

+ 12 - 1
app.py

@@ -2,13 +2,24 @@
 @author: luojunhui
 """
 from quart import Quart
-from applications.routes.title_routes import my_blueprint
+from applications.log import logging
+from applications.routes import my_blueprint
 
 # 初始化 App
 app = Quart(__name__, static_folder='applications/static')
+logging(
+    code="1000",
+    info="APP Initialization Complete",
+    function="app"
+)
 
 # 注册蓝图
 app.register_blueprint(my_blueprint)
+logging(
+    code="1000",
+    info="Blue Print Initialization Complete",
+    function="app"
+)
 
 
 if __name__ == '__main__':

+ 10 - 1
applications/functions/calculate.py

@@ -4,6 +4,8 @@
 import json
 import os
 
+from applications.log import logging
+
 
 def read_single_file(filename):
     """
@@ -92,7 +94,7 @@ def compute_similarity(file_1, file_2):
         return 0, 0
 
 
-def title_mix(title_p, dt):
+def title_mix(title_p, dt, trace_id):
     """
     执行代码
     :param title_p:
@@ -124,4 +126,11 @@ def title_mix(title_p, dt):
         "s2_score": s2_list[0][0],
         "s2_uid": s2_list[0][2]
     }
+    logging(
+        code="1003",
+        info="计算结果得分",
+        data=obj,
+        function="title_mix",
+        trace_id=trace_id
+    )
     return obj

+ 53 - 0
applications/log.py

@@ -0,0 +1,53 @@
+"""
+@author: luojunhui
+"""
+import time
+import json
+from aliyun.log import LogClient, PutLogsRequest, LogItem
+
+
+def logging(code, trace_id=None, info=None, port=None, alg=None, function=None, data=None):
+    """
+    :param trace_id: 请求唯一 id
+    :param data: 信息
+    :param code: 日志状态码
+    :param info: 日志消息
+    :param port: 请求接口
+    :param alg: 算法主体
+    :param function: 方法信息
+    :return:
+    """
+    if data is None:
+        data = {}
+    accessKeyId = "LTAIP6x1l3DXfSxm"
+    accessKey = "KbTaM9ars4OX3PMS6Xm7rtxGr1FLon"
+    project = "changwen-alg"
+    log_store = "weixin_video_match"
+    endpoint = "cn-hangzhou.log.aliyuncs.com"
+
+    # 创建 LogClient 实例
+    client = LogClient(endpoint, accessKeyId, accessKey)
+    log_group = []
+    log_item = LogItem()
+    contents = [
+        (f"code", str(code)),
+        (f"alg", str(alg)),
+        (f"function", str(function)),
+        (f"info", str(info)),
+        (f"port", str(port)),
+        (f"data", json.dumps(data, ensure_ascii=False) if data else ""),
+        ("timestamp", str(int(time.time()))),
+    ]
+
+    log_item.set_contents(contents)
+    log_group.append(log_item)
+    # 写入日志
+    request = PutLogsRequest(
+        project=project,
+        logstore=log_store,
+        topic="",
+        source="",
+        logitems=log_group,
+        compress=False,
+    )
+    client.put_logs(request)

+ 114 - 61
applications/process.py

@@ -10,18 +10,66 @@ import requests
 import urllib.parse
 from datetime import datetime, timedelta
 
+from applications.log import logging
 from applications.functions.ask_kimi import ask_kimi
 from applications.functions.calculate import title_mix
 from applications.functions.auto_white import auto_white
 
 
+def create_gzh_path(video_id, shared_uid):
+    """
+    :param video_id: 视频 id
+    :param shared_uid: 分享 id
+    """
+    root_share_id = str(uuid.uuid4())
+    url = f"pages/user-videos?id={video_id}&su={shared_uid}&fromGzh=1&rootShareId={root_share_id}&shareId={root_share_id}"
+    # 自动把 root_share_id 加入到白名单
+    auto_white(root_share_id)
+    return root_share_id, f"pages/category?jumpPage={urllib.parse.quote(url)}"
+
+
+def request_for_info(video_id):
+    """
+    请求数据
+    :param video_id:
+    :return:
+    """
+    url = "https://longvideoapi.piaoquantv.com/longvideoapi/openapi/video/batchSelectVideoInfo"
+    data = {
+        "videoIdList": [video_id]
+    }
+    header = {
+        "Content-Type": "application/json",
+    }
+    response = requests.post(url, headers=header, data=json.dumps(data))
+    return response.json()
+
+
+def choose_video(result):
+    """
+    :param result: 计算出来的结果
+    :return: uid, video_id
+    """
+    # 判断 score
+    score1, score2 = result['s1_score'], result['s2_score']
+    if score1 == 0 and score2 == 0:
+        return None, None
+    elif score1 == 0 and score2 > 0:
+        return result['s2_uid'], result['s2_vid']
+    elif score1 > 0 and score2 == 0:
+        return result['s1_uid'], result['s1_vid']
+    elif score1 > 0 and score2 > 0:
+        return result['s1_uid'], result['s1_vid']
+
+
 class ProcessParams(object):
     """
     Params Analysis
     """
+    def __init__(self, t_id):
+        self.trace_id = t_id
 
-    @classmethod
-    def get_params(cls, data):
+    def get_params(self, data):
         """
         "accountName": "公众号名称",
         "content": "文章正文",
@@ -30,10 +78,16 @@ class ProcessParams(object):
         :param data:
         :return: title
         """
-        return data['title']
+        logging(
+            code="1002",
+            info="处理请求参数",
+            function="get_params",
+            trace_id=self.trace_id,
+            data=data
+        )
+        return data
 
-    @classmethod
-    def ask_kimi_and_save_to_local(cls, title):
+    def ask_kimi_and_save_to_local(self, title):
         """
         save file to local
         :param title:
@@ -41,79 +95,71 @@ class ProcessParams(object):
         """
         save_path = os.path.join(os.getcwd(), 'applications', 'static', "titles", "{}.json".format(title))
         if os.path.exists(save_path):
+            logging(
+                code="1002",
+                info="该 video 信息已经挖掘完成---{}".format(title),
+                function="ask_kimi_and_save_to_local",
+                trace_id=self.trace_id,
+            )
             return
         else:
             os.makedirs(os.path.dirname(save_path), exist_ok=True)
             if not title:
-                result = "{}"
+                result = {}
             else:
                 result = ask_kimi(title)
+            logging(
+                code="1002",
+                info="kimi-result",
+                data=result,
+                trace_id=self.trace_id,
+                function="ask_kimi_and_save_to_local"
+            )
             with open(save_path, "w", encoding="utf-8") as f:
                 f.write(json.dumps(result, ensure_ascii=False))
 
-    @classmethod
-    def create_gzh_path(cls, video_id, shared_uid):
-        """
-        :param video_id: 视频 id
-        :param shared_uid: 分享 id
-        """
-        root_share_id = str(uuid.uuid4())
-        url = f"pages/user-videos?id={video_id}&su={shared_uid}&fromGzh=1&rootShareId={root_share_id}&shareId={root_share_id}"
-        # 自动把 root_share_id 加入到白名单
-        auto_white(root_share_id)
-        return root_share_id, f"pages/category?jumpPage={urllib.parse.quote(url)}"
-
-    @classmethod
-    def request_for_info(cls, video_id):
-        """
-        请求数据
-        :param video_id:
-        :return:
-        """
-        url = "https://longvideoapi.piaoquantv.com/longvideoapi/openapi/video/batchSelectVideoInfo"
-        data = {
-            "videoIdList": [video_id]
-        }
-        header = {
-            "Content-Type": "application/json",
-        }
-        response = requests.post(url, headers=header, data=json.dumps(data))
-        return response.json()
-
-    @classmethod
-    def choose_video(cls, result):
-        """
-        :param result: 计算出来的结果
-        :return: uid, video_id
-        """
-        # 判断 score
-        score1, score2 = result['s1_score'], result['s2_score']
-        if score1 == 0 and score2 == 0:
-            return None, None
-        elif score1 == 0 and score2 > 0:
-            return result['s2_uid'], result['s2_vid']
-        elif score1 > 0 and score2 == 0:
-            return result['s1_uid'], result['s1_vid']
-        elif score1 > 0 and score2 > 0:
-            return result['s1_uid'], result['s1_vid']
-
-    @classmethod
-    def process(cls, data):
+    def process(self, data):
         """执行代码"""
         today = datetime.today()
         yesterday = today - timedelta(days=1)
         yesterday_str = yesterday.strftime("%Y%m%d")
-        title = cls.get_params(data)
+        logging(
+            code="1002",
+            info="昨日的时间戳是---{}".format(yesterday),
+            function="process",
+            trace_id=self.trace_id,
+        )
+        params = self.get_params(data)
+        title = params['title']
+        account_name = params['accountName']
         title_p = os.path.join(os.getcwd(), 'applications', 'static', "titles", "{}.json".format(title))
         if os.path.exists(title_p):
-            result = title_mix(title_p=title_p, dt=yesterday_str)
+            logging(
+                code="1002",
+                info="该标题已经被 kimi 处理过,跳过请求 kimi 操作--- {}".format(title),
+                function="process",
+                trace_id=self.trace_id
+            )
+            result = title_mix(title_p=title_p, dt=yesterday_str, trace_id=self.trace_id)
         else:
-            cls.ask_kimi_and_save_to_local(title)
-            result = title_mix(title_p=title_p, dt=yesterday_str)
-        uid, video_id = cls.choose_video(result)
+            self.ask_kimi_and_save_to_local(title)
+            result = title_mix(title_p=title_p, dt=yesterday_str, trace_id=self.trace_id)
+        uid, video_id = choose_video(result)
+        logging(
+            code="1002",
+            info="best video_id --{}".format(video_id),
+            function="process",
+            trace_id=self.trace_id
+        )
         if video_id and uid:
-            root_share_id, productionPath = cls.create_gzh_path(uid, video_id)
-            response = cls.request_for_info(video_id)
+            root_share_id, productionPath = create_gzh_path(video_id=video_id, shared_uid=account_name)
+            logging(
+                code="1002",
+                info="root_share_id --{}, productionPath -- {}".format(root_share_id, productionPath),
+                function="process",
+                trace_id=self.trace_id
+            )
+            response = request_for_info(video_id)
             productionCover = response['data'][0]['shareImgPath']
             productionName = response["data"][0]['title']
             programAvatar = "/static/logo.png"
@@ -141,4 +187,11 @@ class ProcessParams(object):
                 "rootShareId": None,
                 "productionPath": None
             }
+        logging(
+            code="1002",
+            info="返回结果",
+            function="process",
+            data=result,
+            trace_id=self.trace_id
+        )
         return result

+ 16 - 2
applications/routes/title_routes.py → applications/routes.py

@@ -1,11 +1,13 @@
 """
 @author: luojunhui
 """
+import time
+import uuid
 from quart import Blueprint, jsonify, request
 
+from applications.log import logging
 from applications.process import ProcessParams
 
-
 my_blueprint = Blueprint('kimi', __name__)
 
 
@@ -15,6 +17,11 @@ async def hello():
     Hello World Test
     :return:
     """
+    logging(
+        code="1001",
+        info="请求接口成功",
+        port="healthcheck"
+    )
     return jsonify({'message': 'Hello, World!'})
 
 
@@ -24,7 +31,14 @@ async def post_data():
     请求接口代码
     :return:
     """
-    p = ProcessParams()
+    trace_id = str(uuid.uuid4()) + "-" + str(int(time.time()))
+    logging(
+        code="1001",
+        info="请求接口成功",
+        port="title_to_video",
+        trace_id=trace_id
+    )
+    p = ProcessParams(t_id=trace_id)
     data = await request.get_json()
     processed_data = p.process(data)
     return jsonify(processed_data)

+ 1 - 1
test.py

@@ -8,7 +8,6 @@ from concurrent.futures import ThreadPoolExecutor
 
 
 def request_data(url):
-
     # index = _url.split("#")[0]
     # url = _url.split("#")[1]
     body = {
@@ -24,6 +23,7 @@ def request_data(url):
     # print(res.text)
     print(res.json())
 
+
 if __name__ == "__main__":
     # parser = argparse.ArgumentParser()  # 新建参数解释器对象
     # parser.add_argument("--thread")