浏览代码

added option to get channel by channel_url

Cheskel Twersky 4 年之前
父节点
当前提交
7de6f4bb53
共有 1 个文件被更改,包括 18 次插入3 次删除
  1. 18 3
      scrapetube/scrapetube.py

+ 18 - 3
scrapetube/scrapetube.py

@@ -4,12 +4,24 @@ import json
 import time
 
 
-def get_channel(channel_id: str, limit: int = None, sleep: int = 1, sort_by: str = 'newest') -> Generator[dict, None, None]:
+def get_channel(
+    channel_id: str = None,
+    channel_url: str = None,
+    limit: int = None,
+    sleep: int = 1,
+    sort_by: str = 'newest'
+    ) -> Generator[dict, None, None]:
+
     """Get videos for a channel.
 
     Parameters:
-        channel_id (``str``):
+        channel_id (``str``, *optional*):
             The channel id from the channel you want to get the videos for.
+            If you prefer to use the channel url instead, see ``channel_url`` below.
+        channel_url (``str``, *optional*):
+            The url to the channel you want to get the videos for.
+            Since there is a few type's of channel url's, you can use the one you want
+            by passing it here instead of using ``channel_id``.
         limit (``int``, *optional*):
             Limit to number of videos you want to get.
         sleep (``int``, *optional*):
@@ -27,7 +39,10 @@ def get_channel(channel_id: str, limit: int = None, sleep: int = 1, sort_by: str
         'oldest': 'da',
         'popular': 'p'
     }
-    url = f'https://www.youtube.com/channel/{channel_id}/videos?view=0&sort={sort_by_map[sort_by]}&flow=grid'
+    url = '{url}/videos?view=0&sort={sort_by}&flow=grid'.format(
+        url= channel_url or f'https://www.youtube.com/channel/{channel_id}',
+        sort_by= sort_by_map[sort_by]
+        )
     api_endpoint = 'https://www.youtube.com/youtubei/v1/browse'
     videos = get_videos(url, api_endpoint, 'gridVideoRenderer', limit, sleep)
     for video in videos: