ソースを参照

2024-07-04
两个账号返回新小程序

罗俊辉 1 年間 前
コミット
52c132a1e1
2 ファイル変更249 行追加4 行削除
  1. 191 3
      applications/deal/recall_deal.py
  2. 58 1
      applications/static/config.py

+ 191 - 3
applications/deal/recall_deal.py

@@ -1,21 +1,209 @@
 """
 @author: luojunhui
 """
+import json
+import uuid
+import time
+import random
+import hashlib
+import urllib.parse
+
+from applications.functions.log import logging
+from applications.static.config import db_article
+from applications.functions.common import request_for_info
 
 
 class RecallDeal(object):
     """
     召回逻辑
     """
-    def __init__(self, trace_id, mysql_client):
+
+    def __init__(self, trace_id, mysql_client, mini_program_type):
         self.trace_id = trace_id
         self.mysql_client = mysql_client
+        self.mini_program_type = mini_program_type
 
-    def deal(self):
+    async def get_result(self):
         """
-        Recall Deal
+        获取结果
         :return:
         """
+        select_sql = f"""
+                SELECT gh_id, recall_video_id1, recall_video_id2, recall_video_id3, kimi_title, content_status, process_times
+                FROM {db_article}
+                WHERE trace_id = '{self.trace_id}';
+        """
+        info_tuple = await self.mysql_client.async_select(select_sql)
+        gh_id, vid1, vid2, vid3, kimi_title, content_status, process_times = info_tuple[0]
+        response = {
+            "gh_id": gh_id,
+            "vid_list": [vid1, vid2, vid3],
+            "kimi_title": kimi_title,
+            "content_status": content_status,
+            "process_times": process_times
+        }
+        return response
 
+    def create_gzh_path(self, video_id, shared_uid):
+        """
+        :param video_id: 视频 id
+        :param shared_uid: 分享 id
+        """
+
+        def generate_source_id():
+            """
+            generate_source_id
+            :return:
+            """
+            timestamp = str(int(time.time() * 1000))
+            random_str = str(random.randint(1000, 9999))
+            hash_input = f"{timestamp}-{random_str}"
+            return hashlib.md5(hash_input.encode()).hexdigest()
+
+        root_share_id = str(uuid.uuid4())
+        if self.mini_program_type == 1:
+            source_id = "touliu_tencentGzhArticle_" + generate_source_id()
+        elif self.mini_program_type == 2:
+            source_id = "longArticles_" + generate_source_id()
+        else:
+            source_id = "Error mini_program_type {}".format(self.mini_program_type)
+        url = f"pages/user-videos?id={video_id}&su={shared_uid}&fromGzh=1&rootShareId={root_share_id}&shareId={root_share_id}&rootSourceId={source_id}"
+        # 自动把 root_share_id 加入到白名单
+        # auto_white(root_share_id)
+        return root_share_id, source_id, f"pages/category?jumpPage={urllib.parse.quote(url, safe='')}"
+
+    def choose_mini_program(self):
+        """
+        获取小程序分享卡片
+        :return:
+        """
+        if self.mini_program_type == 1:
+            # 正常长文业务
+            programAvatar = "https://rescdn.yishihui.com/0temp/ssyqsh.png"
+            programId = "wx59d9e2c05f00f880"
+            programName = "刷刷有趣生活"
+        elif self.mini_program_type == 2:
+            # 投流业务
+            programAvatar = "https://rescdn.yishihui.com/0temp/zfyfyc.jpeg"
+            programId = "wxcddf231abd0dabdc"
+            programName = "祝福有福有财"
+        else:
+            programAvatar = "https://rescdn.yishihui.com/0temp/ssyqsh.png"
+            programId = "wx59d9e2c05f00f880"
+            programName = "刷刷有趣生活"
+        return programAvatar, programId, programName
+
+    async def generate_card(self, video_id, kimi_title, index):
+        """
+        生成分享卡片
+        :return:
+        """
+        response = request_for_info(video_id)
+        productionCover = response['data'][0]['shareImgPath']
+        productionName = kimi_title
+        videoUrl = response['data'][0]['videoPath']
+        user_id = response['data'][0]['user']['uid']
+        programAvatar, programId, programName = self.choose_mini_program()
+        root_share_id, source_id, productionPath = self.create_gzh_path(video_id, user_id)
+        source = "Web"
+        logging(
+            code="1002",
+            info="root_share_id --{}, productionPath -- {}".format(root_share_id, productionPath),
+            function="process",
+            trace_id=self.trace_id
+        )
+        result = {
+            "productionCover": productionCover,
+            "productionName": productionName,
+            "programAvatar": programAvatar,
+            "programId": programId,
+            "programName": programName,
+            "source": source,
+            "rootShareId": root_share_id,
+            "productionPath": productionPath,
+            "videoUrl": videoUrl,
+            "paragraphPosition": index * 0.25
+        }
+        update_result_sql = f"""
+                            UPDATE {db_article}
+                            SET
+                                result{index} = %s,
+                                success = %s
+                            WHERE
+                                trace_id = %s;
+                        """
+        await self.mysql_client.async_insert(
+            sql=update_result_sql,
+            params=(json.dumps(result, ensure_ascii=False), 1, self.trace_id)
+        )
+        logging(
+            code="2000",
+            info="统计 root_share_id && video_id",
+            function="process",
+            trace_id=self.trace_id,
+            data={
+                "rootShareId": root_share_id,
+                "videoId": video_id,
+                "sourceId": source_id
+            }
+        )
+        return result
+
+    async def deal(self):
+        """
+        Recall Deal
+        :return:
+        """
+        response = await self.get_result()
+        status_code = response.get("content_status")
+        process_times = response.get("process_times")
+        if status_code == 0:
+            if process_times > 5:
+                result = {
+                    "traceId": self.trace_id,
+                    "code": 0,
+                    "error": "匹配失败,处理超过五次,文章敏感"
+                }
+            else:
+                result = {
+                    "traceId": self.trace_id,
+                    "code": 0,
+                    "Message": "该请求还没处理"
+                }
+        elif status_code == 1:
+            result = {
+                "traceId": self.trace_id,
+                "code": 1,
+                "Message": "该请求正在处理中"
+            }
+        elif status_code == 2:
+            L = []
+            unEmptyList = [i for i in response['vid_list'] if i]
+            for index, best_video_id in enumerate(unEmptyList, 1):
+                card = await self.generate_card(best_video_id, response.get("kimi_title"), index)
+                L.append(card)
+            result = {
+                "traceId": self.trace_id,
+                "miniprogramList": L
+            }
+        elif status_code == 3:
+            result = {
+                "traceId": self.trace_id,
+                "code": 0,
+                "error": "匹配失败,处理超过五次,文章敏感"
+            }
+        else:
+            result = {
+                "traceId": self.trace_id,
+                "Message": "UnKnow Error"
+            }
+        logging(
+            code="1002",
+            info="返回结果",
+            function="process",
+            data=result,
+            trace_id=self.trace_id
+        )
+        return result
 
 

+ 58 - 1
applications/static/config.py

@@ -27,6 +27,10 @@ gh_id_dict = {
         "uid": 69637508,
         "nick_name": "粟米"
     },
+    "gh_0d8a65926cdf": {
+        "uid": 71353811,
+        "nick_name": "寻歡猫"
+    },
     "gh_171cec079b2a": {
         "uid": 69637501,
         "nick_name": "海上"
@@ -39,14 +43,26 @@ gh_id_dict = {
         "uid": 70731102,
         "nick_name": "久亿"
     },
+    "gh_19daa341c8f9": {
+        "uid": 71353816,
+        "nick_name": "美"
+    },
     "gh_1b27dd1beeca": {
         "uid": 71021080,
         "nick_name": "聚散常迷"
     },
+    "gh_1d10403eb554": {
+        "uid": 71353815,
+        "nick_name": "听闻那年"
+    },
     "gh_1d887d61088c": {
         "uid": 71021081,
         "nick_name": "明月不知沟渠心"
     },
+    "gh_1e83780cc5a8": {
+        "uid": 71353810,
+        "nick_name": "眉眼"
+    },
     "gh_1ee2e1b39ccf": {
         "uid": 69637473,
         "nick_name": "纵有疾风起"
@@ -59,6 +75,10 @@ gh_id_dict = {
         "uid": 71051365,
         "nick_name": "吹长笛"
     },
+    "gh_24aa1e4bf177": {
+        "uid": 71353820,
+        "nick_name": "翁思"
+    },
     "gh_26a307578776": {
         "uid": 69637490,
         "nick_name": "最宝贝的宝贝"
@@ -135,6 +155,10 @@ gh_id_dict = {
         "uid": 69637538,
         "nick_name": "留下太多回忆"
     },
+    "gh_5887a9154605": {
+        "uid": 71353814,
+        "nick_name": "寄许"
+    },
     "gh_59b9f8ef99a4": {
         "uid": 71051360,
         "nick_name": "小編最可愛"
@@ -183,6 +207,10 @@ gh_id_dict = {
         "uid": 69637505,
         "nick_name": "反駁"
     },
+    "gh_759ace9d4567": {
+        "uid": 71353812,
+        "nick_name": "青丝与白猫"
+    },
     "gh_7b4a5f86d68c": {
         "uid": 69637477,
         "nick_name": "我很想你"
@@ -203,6 +231,10 @@ gh_id_dict = {
         "uid": 69637533,
         "nick_name": "彼岸花"
     },
+    "gh_8eaa863bc40e": {
+        "uid": 71353813,
+        "nick_name": "花费时间"
+    },
     "gh_901b0d722749": {
         "uid": 69637518,
         "nick_name": "深情不为我"
@@ -211,6 +243,10 @@ gh_id_dict = {
         "uid": 69637495,
         "nick_name": "折磨"
     },
+    "gh_91abdbc32d5f": {
+        "uid": 71353822,
+        "nick_name": "一往情深"
+    },
     "gh_93e00e187787": {
         "uid": 69637504,
         "nick_name": "理会"
@@ -227,6 +263,10 @@ gh_id_dict = {
         "uid": 69637471,
         "nick_name": "我与你相遇"
     },
+    "gh_9ee24345c6ce": {
+        "uid": 71353818,
+        "nick_name": "爱隔山河"
+    },
     "gh_9eef14ad6c16": {
         "uid": 70731110,
         "nick_name": "抱一抱"
@@ -255,6 +295,10 @@ gh_id_dict = {
         "uid": 69637499,
         "nick_name": "一厢情愿"
     },
+    "gh_ad7b26ee9e17": {
+        "uid": 71353809,
+        "nick_name": "最宝贝的宝贝"
+    },
     "gh_adca24a8f429": {
         "uid": 69637483,
         "nick_name": "对你何止一句喜欢"
@@ -287,10 +331,18 @@ gh_id_dict = {
         "uid": 69637524,
         "nick_name": "养一只月亮"
     },
+    "gh_ba6e1e4f2de0": {
+        "uid": 71353817,
+        "nick_name": "余欢"
+    },
     "gh_bd57b6978e06": {
         "uid": 69637527,
         "nick_name": "厌遇"
     },
+    "gh_be505c7d28ac": {
+        "uid": 71353823,
+        "nick_name": "遥远的她"
+    },
     "gh_be8c29139989": {
         "uid": 69637502,
         "nick_name": "不负"
@@ -311,6 +363,10 @@ gh_id_dict = {
         "uid": 69637512,
         "nick_name": "骄纵"
     },
+    "gh_c794770120dc": {
+        "uid": 71353819,
+        "nick_name": "以笑相迎"
+    },
     "gh_c91b42649690": {
         "uid": 69637503,
         "nick_name": "荟萃"
@@ -393,6 +449,7 @@ gh_id_dict = {
     }
 }
 
+
 sensitive_words = [
     "台湾",
     "南海",
@@ -467,4 +524,4 @@ article_queue = "long_articles_queue"
 spider_coroutines = 6
 
 # mysql coroutines
-mysql_coroutines = 40
+mysql_coroutines = 40