Przeglądaj źródła

自动入池方法上线

罗俊辉 1 rok temu
rodzic
commit
a93c2d4a9c
1 zmienionych plików z 110 dodań i 33 usunięć
  1. 110 33
      app/auto_send_to_flow_pool.py

+ 110 - 33
app/auto_send_to_flow_pool.py

@@ -36,8 +36,7 @@ def bot(success_list):
             "header": {"title": {"content": "自动送入流量池: 通知 ✅", "tag": "plain_text"}},
         },
     }
-    w = requests.request("POST", url=url, headers=headers, data=json.dumps(payload))
-    print(w.json())
+    requests.request("POST", url=url, headers=headers, data=json.dumps(payload))
 
 
 class OdpsFunction(object):
@@ -70,6 +69,56 @@ class OdpsFunction(object):
                 return []
 
 
+def generate_flow_pool_levels():
+    """
+    读取流量池 level_id表并且通过流量池层级来生成 id
+    :return:
+    """
+    url = "https://admin.piaoquantv.com/manager/flowpool/pageList"
+    payload = {
+        "pageNum": 1,
+        "pageSize": 10000
+    }
+    headers = {
+        'authority': 'admin.piaoquantv.com',
+        'accept': 'application/json',
+        'accept-language': 'en,zh;q=0.9',
+        'content-type': 'application/json',
+        'cookie': 'SESSION=ZDY1MTYxODgtNGQ2OS00YzA4LThlMzAtMmE3YzllNGQ4ODk5',
+        'origin': 'https://admin.piaoquantv.com',
+        'referer': 'https://admin.piaoquantv.com/cms/post-detail/19014920/detail',
+        'sec-ch-ua': '"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',
+        'sec-ch-ua-mobile': '?0',
+        'sec-ch-ua-platform': '"macOS"',
+        'sec-fetch-dest': 'empty',
+        'sec-fetch-mode': 'cors',
+        'sec-fetch-site': 'same-origin',
+        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36'
+    }
+    res = requests.request("POST", url, json=payload, headers=headers).json()
+    # print(res.json())
+    pool_list = res['content']['list']
+    for pool in pool_list:
+        if pool['id'] == -1:
+            levels = pool['levels']
+            return levels
+
+
+def generate_flow_map():
+    """
+    生成 level_id 映射表
+    :return:
+    """
+    try:
+        levels = generate_flow_pool_levels()
+        w = {}
+        for level in levels:
+            w[str(level['level'])] = level['id']
+        return w
+    except:
+        return {}
+
+
 class AutoSendToFlowPool(object):
     """
     定时任务方式
@@ -96,14 +145,14 @@ class AutoSendToFlowPool(object):
             "sec-fetch-site": "same-origin",
             "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36",
         }
-        self.enter_url = "https://admin.piaoquantv.com/manager/flowpool/video/enter"
+        self.enter_url = "https://api-internal.piaoquantv.com/flowpool/video/enter/abPool"
         self.flow_pool_map = {
-            1: [1, 24, 30, 36, 42, 48, 54, 60, 66, 72, 78],
-            2: [2, 25, 31, 37, 43, 49, 55, 61, 67, 73, 79],
-            3: [3, 26, 32, 38, 44, 50, 56, 62, 68, 74, 80],
-            4: [4, 27, 33, 39, 45, 51, 57, 63, 69, 75, 81],
-            5: [22, 28, 34, 40, 46, 52, 58, 64, 70, 76, 82],
-            6: [23, 29, 35, 41, 47, 53, 59, 65, 71, 77, 83],
+            "1": 24,
+            "2": 25,
+            "3": 26,
+            "4": 27,
+            "5": 28,
+            "6": 29
         }
         self.odp = OdpsFunction()
         self.aliyun_log = AliyunLogger(platform="home", mode="automatic")
@@ -114,7 +163,7 @@ class AutoSendToFlowPool(object):
         :return: List [{}, {}, {}......]
         """
         t = datetime.datetime.now().strftime("%Y%m%d%H")
-        sql = f"""select videoid, target_level from loghubods.auto_enter_flow_pool_videolist where dt = '{t}';"""
+        sql = f"""select videoid, target_level, uid from loghubods.auto_enter_flow_pool_videolist where dt = '{t}';"""
         data = self.odp.select(sql)
         return data
 
@@ -128,20 +177,41 @@ class AutoSendToFlowPool(object):
         :param flow_pool_id: flow pool id, define -1
         :return: bool, False / True
         """
+        pool_map = generate_flow_map()
+        if not pool_map:
+            pool_map = self.flow_pool_map
+        level = str(obj["target_level"])
+        level_id = pool_map[level]
+
         o = {
             "flowPoolId": flow_pool_id,
-            "startType": 0,  # 1 human / 0 machine
-            "videoId": obj["videoid"],
-            "flowPoolLevelId": obj["target_level"],
+            "startType": 2,
+            "uid": int(obj['uid']),
+            "videoId": int(obj["videoid"]),
+            "flowPoolLevelId": level_id
         }
+        self.aliyun_log.logging(
+            code="4003",
+            message="请求地址: {}".format(self.enter_url),
+            data=o
+        )
         response = requests.request("POST", self.enter_url, headers=self.header, json=o)
         if response.json()['code'] == 0:
+            self.aliyun_log.logging(
+                code="4003",
+                message="返回",
+                data=response.json()
+            )
+            self.aliyun_log.logging(
+                code="4001", message="有一条视频成功送入流量池", data={"vid": obj['videoid'], "level": level_id}
+            )
             return True
         else:
             self.aliyun_log.logging(
                 code="4004",
                 message=response.json()['msg'] if response.json().get('msg') else "操作失败",
-                data=response.json()
+                data=response.json(),
+                account=int(obj["videoid"])
             )
             return False
 
@@ -151,28 +221,35 @@ class AutoSendToFlowPool(object):
         :return:
         """
         successful_list = []
-        data = self.get_data_from_flow_pool()
-        if data:
-            self.aliyun_log.logging(
-                code=4001, message="该小时一共有 {} 条视频需要被送入流量池".format(len(data)), data=data
-            )
-            for obj in data:
-                if self.send_to_flow_pool_level(obj):
-                    successful_list.append(obj['videoid'])
-                    self.aliyun_log.logging(
-                        code="4001", message="有一条视频成功送入流量池", data=obj
-                    )
-            # robot send message to the group, notice the successful count and the fail count
-            if successful_list:
-                bot(successful_list)
-        else:
-            # robot notice no videos found this hour
-            return
+        for i in range(20):
+            data = self.get_data_from_flow_pool()
+            if data:
+                self.aliyun_log.logging(
+                    code=4001, message="该小时一共有 {} 条视频需要被送入流量池".format(len(data)),
+                    data=[item['videoid'] for item in data]
+                )
+                for obj in data:
+                    if self.send_to_flow_pool_level(obj):
+                        successful_list.append(obj['videoid'])
+
+                # robot send message to the group, notice the successful count and the fail count
+                if successful_list:
+                    bot(successful_list)
+                return
+            else:
+                self.aliyun_log.logging(
+                    code=4006,
+                    message="未扫描到数据, 等待 60s * {}".format(i + 1)
+                )
+                time.sleep(60)
+
+        # 本小时没有扫描到数据
 
 
 if __name__ == "__main__":
     AS = AutoSendToFlowPool()
-    schedule.every().hour.at(":20").do(AS.auto_process)
+
+    schedule.every().hour.at(":08").do(AS.auto_process)
     while True:
         schedule.run_pending()
-        time.sleep(10)
+        time.sleep(10)