Przeglądaj źródła

增加快手渠道

zhangyong 10 miesięcy temu
rodzic
commit
779962e128
2 zmienionych plików z 102 dodań i 7 usunięć
  1. 83 1
      data_channel/kuaishou.py
  2. 19 6
      video_rewriting/video_prep.py

+ 83 - 1
data_channel/kuaishou.py

@@ -1,3 +1,85 @@
+import random
+import time
+import requests
+import json
+import urllib3
+from requests.adapters import HTTPAdapter
+
+from common import Feishu, Material, Common
+from common.sql_help import sqlCollect
+from data_channel.data_help import dataHelp
+
 
 class KS:
-    pass
+
+    @classmethod
+    def get_ks_url(cls, task_mark, url_id, number, mark, feishu_id, cookie_sheet, channel_id, name):
+        list = []
+        pcursor = ""
+        url = "https://www.kuaishou.com/graphql"
+
+        for i in range(3):
+            cookie = Material.get_cookie_data(feishu_id, cookie_sheet, channel_id)
+            time.sleep(random.randint(5, 10))
+            payload = json.dumps({
+                "operationName": "visionProfilePhotoList",
+                "variables": {
+                    "userId": url_id,
+                    "pcursor": pcursor,
+                    "page": "profile"
+                },
+                "query": "fragment photoContent on PhotoEntity {\n  __typename\n  id\n  duration\n  caption\n  originCaption\n  likeCount\n  viewCount\n  commentCount\n  realLikeCount\n  coverUrl\n  photoUrl\n  photoH265Url\n  manifest\n  manifestH265\n  videoResource\n  coverUrls {\n    url\n    __typename\n  }\n  timestamp\n  expTag\n  animatedCoverUrl\n  distance\n  videoRatio\n  liked\n  stereoType\n  profileUserTopPhoto\n  musicBlocked\n  riskTagContent\n  riskTagUrl\n}\n\nfragment recoPhotoFragment on recoPhotoEntity {\n  __typename\n  id\n  duration\n  caption\n  originCaption\n  likeCount\n  viewCount\n  commentCount\n  realLikeCount\n  coverUrl\n  photoUrl\n  photoH265Url\n  manifest\n  manifestH265\n  videoResource\n  coverUrls {\n    url\n    __typename\n  }\n  timestamp\n  expTag\n  animatedCoverUrl\n  distance\n  videoRatio\n  liked\n  stereoType\n  profileUserTopPhoto\n  musicBlocked\n  riskTagContent\n  riskTagUrl\n}\n\nfragment feedContent on Feed {\n  type\n  author {\n    id\n    name\n    headerUrl\n    following\n    headerUrls {\n      url\n      __typename\n    }\n    __typename\n  }\n  photo {\n    ...photoContent\n    ...recoPhotoFragment\n    __typename\n  }\n  canAddComment\n  llsid\n  status\n  currentPcursor\n  tags {\n    type\n    name\n    __typename\n  }\n  __typename\n}\n\nquery visionProfilePhotoList($pcursor: String, $userId: String, $page: String, $webPageArea: String) {\n  visionProfilePhotoList(pcursor: $pcursor, userId: $userId, page: $page, webPageArea: $webPageArea) {\n    result\n    llsid\n    webPageArea\n    feeds {\n      ...feedContent\n      __typename\n    }\n    hostName\n    pcursor\n    __typename\n  }\n}\n"
+            })
+            headers = {
+                'accept': '*/*',
+                'content-type': 'application/json',
+                'Origin': 'https://www.kuaishou.com',
+                'Cookie': cookie,
+                'Accept-Language': 'zh-CN,zh;q=0.9',
+                'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36',
+                'Referer': f'https://www.kuaishou.com/profile/{url_id}',
+                'Accept-Encoding': 'gzip, deflate, br',
+                'Connection': 'keep-alive'
+            }
+            urllib3.disable_warnings()
+            s = requests.session()
+            s.mount('http://', HTTPAdapter(max_retries=3))
+            s.mount('https://', HTTPAdapter(max_retries=3))
+            # response = requests.request("POST", url, headers=headers, data=payload, timeout=10)
+
+            response = s.post(url=url, headers=headers, data=payload, verify=False, timeout=10)
+            response.close()
+            if response.status_code != 200:
+                return list
+            elif "visionProfilePhotoList" not in response.json()["data"]:
+                Feishu.bot(mark, '机器自动改造消息通知', f'快手cookie过期,请及时更换', name)
+                time.sleep(900)
+                continue
+            elif "feeds" not in response.json()["data"]["visionProfilePhotoList"]:
+                Feishu.bot(mark, '机器自动改造消息通知', f'快手cookie过期,请及时更换', name)
+                time.sleep(900)
+                continue
+            elif len(response.json()["data"]["visionProfilePhotoList"]["feeds"]) == 0:
+                Feishu.bot(mark, '机器自动改造消息通知', f'快手cookie使用频繁无法获取到数据,请及时更换', name)
+                time.sleep(900)
+                continue
+            pcursor = response.json()['data']['visionProfilePhotoList']['pcursor']
+            feeds = response.json()['data']['visionProfilePhotoList']['feeds']
+            for i in range(len(feeds)):
+                try:
+                    video_id = feeds[i].get("photo", {}).get("videoResource").get("h264", {}).get("videoId", "")
+                except KeyError:
+                    video_id = feeds[i].get("photo", {}).get("videoResource").get("hevc", {}).get("videoId", "")
+                status = sqlCollect.is_used(task_mark, video_id, mark, channel_id)
+                if status:
+                    cover_url = feeds[i].get('photo', {}).get('coverUrl', "")
+                    video_url = feeds[i].get('photo', {}).get('photoUrl', "")
+                    duration = dataHelp.video_duration(video_url)
+                    if int(duration) >= 45:
+                        all_data = {"video_id": video_id, "cover": cover_url, "video_url": video_url}
+                        list.append(all_data)
+                        if len(list) == int(number):
+                            Common.logger("log").info(f"获取快手视频总数:{len(list)}\n")
+                            return list
+        return list
+

+ 19 - 6
video_rewriting/video_prep.py

@@ -9,6 +9,7 @@ import concurrent.futures
 from common import Material, Feishu, Common, Oss
 from common.ffmpeg import FFmpeg
 from data_channel.douyin import DY
+from data_channel.kuaishou import KS
 from data_channel.piaoquan import PQ
 from common.sql_help import sqlCollect
 from data_channel.shipinhao import SPH
@@ -107,8 +108,8 @@ class getVideo:
                     data_list = PQ.get_pq_url(task_mark, url, number, mark)
                 elif channel_id == "视频号":
                     data_list = SPH.get_sph_url(task_mark, url, number, mark)
-                # elif channel_id == "快手":
-                #     pass
+                elif channel_id == "快手":
+                    data_list = KS.get_ks_url(task_mark, url, number, mark, feishu_id, cookie_sheet, channel_id, name)
                 if len(data_list) == 0:
                     Common.logger("log").info(f"{task_mark}下的视频ID{url} 已经改造过了")
                     Feishu.bot(mark, '机器自动改造消息通知', f'{task_mark}任务下的用户ID{url},没有已经改造的视频了', name)
@@ -194,12 +195,24 @@ class getVideo:
                                 sqlCollect.insert_task(task_mark, v_id, mark, channel_id)  # 插入数据库
                                 current_time = datetime.now()
                                 formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
-                                values = [[name, task_mark, v_id, piaoquan_id, new_title, str(code), formatted_time]]
+                                values = [[name, task_mark, channel_id, url, v_id, piaoquan_id, new_title, str(code), formatted_time]]
                                 # 使用锁保护表格插入操作
                                 with lock:
-                                    Feishu.insert_columns("ILb4sa0LahddRktnRipcu2vQnLb", "a74fc4", "ROWS", 1, 2)
+                                    if name == "王雪珂":
+                                        sheet = "a74fc4"
+                                    elif name == "王雪珂-1":
+                                        sheet = "61kvW7"
+                                    elif name == "鲁涛":
+                                        sheet = "FhewlS"
+                                    elif name == "范军":
+                                        sheet = "B6dCfS"
+                                    elif name == "余海涛":
+                                        sheet = "mfBrNT"
+                                    elif name == "罗情":
+                                        sheet = "2J3PwN"
+                                    Feishu.insert_columns("ILb4sa0LahddRktnRipcu2vQnLb", sheet, "ROWS", 1, 2)
                                     time.sleep(0.5)
-                                    Feishu.update_values("ILb4sa0LahddRktnRipcu2vQnLb", "a74fc4", "A2:Z2", values)
+                                    Feishu.update_values("ILb4sa0LahddRktnRipcu2vQnLb", sheet, "A2:Z2", values)
                             cls.remove_files(video_path_url)
                         else:
                             cls.remove_files(video_path_url)
@@ -209,7 +222,7 @@ class getVideo:
                     cls.remove_files(video_path_url)
                     Common.logger("warning").warning(f"{name}的{task_mark}任务处理失败:{e}\n")
 
-        batch_size = 2
+        batch_size = 1
         with concurrent.futures.ThreadPoolExecutor(max_workers=batch_size) as executor:
             index = 0
             while index < len(task_data):