From e8d9b5aabeebee02b96334060fed0cad4cfe8ad4 Mon Sep 17 00:00:00 2001 From: benonymity Date: Sun, 30 Oct 2022 17:04:56 -0400 Subject: [PATCH 1/2] add: support filtering by content type (livestream, upload) --- scrapetube/scrapetube.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/scrapetube/scrapetube.py b/scrapetube/scrapetube.py index 413f649..932315b 100644 --- a/scrapetube/scrapetube.py +++ b/scrapetube/scrapetube.py @@ -12,6 +12,7 @@ def get_channel( limit: int = None, sleep: int = 1, sort_by: Literal["newest", "oldest", "popular"] = "newest", + filter: Literal["uploads", "past_streams", "future_streams"] = "uploads" ) -> Generator[dict, None, None]: """Get videos for a channel. @@ -38,12 +39,21 @@ def get_channel( ``"newest"``: Get the new videos first. ``"oldest"``: Get the old videos first. ``"popular"``: Get the popular videos first. Defaults to "newest". + + filter_type (``str``, *optional*): + What type of videos to retrieve. Pass one of these allowed values: + ``"uploads"``: Get all public channel uploads. + ``"past_streams"``: Get all past public livestreams. + ``"future_streams"``: Get all visible future livestreams. + Defaults to "uploads". """ sort_by_map = {"newest": "dd", "oldest": "da", "popular": "p"} - url = "{url}/videos?view=0&sort={sort_by}&flow=grid".format( + filter_by_map = {"uploads": "0", "past_streams": "2&live_view=503", "future_streams": "2&live_view=502"} + url = "{url}/videos?view={filter_by}&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], + filter_by=filter_by_map[filter] ) api_endpoint = "https://www.youtube.com/youtubei/v1/browse" videos = get_videos(url, api_endpoint, "gridVideoRenderer", limit, sleep) From ad706c2ffdabc8e054f920fc9ebe6530a4bc0085 Mon Sep 17 00:00:00 2001 From: benonymity Date: Tue, 1 Nov 2022 23:42:00 -0400 Subject: [PATCH 2/2] fix: new youtube link system seperating live and videos --- scrapetube/scrapetube.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/scrapetube/scrapetube.py b/scrapetube/scrapetube.py index 932315b..88ffc92 100644 --- a/scrapetube/scrapetube.py +++ b/scrapetube/scrapetube.py @@ -12,7 +12,7 @@ def get_channel( limit: int = None, sleep: int = 1, sort_by: Literal["newest", "oldest", "popular"] = "newest", - filter: Literal["uploads", "past_streams", "future_streams"] = "uploads" + content_type: Literal["videos", "streams"] = "videos" ) -> Generator[dict, None, None]: """Get videos for a channel. @@ -40,20 +40,18 @@ def get_channel( ``"oldest"``: Get the old videos first. ``"popular"``: Get the popular videos first. Defaults to "newest". - filter_type (``str``, *optional*): + content_type (``str``, *optional*): What type of videos to retrieve. Pass one of these allowed values: - ``"uploads"``: Get all public channel uploads. - ``"past_streams"``: Get all past public livestreams. - ``"future_streams"``: Get all visible future livestreams. - Defaults to "uploads". + ``"videos"``: Get all public videos uploads from the channel. + ``"streams"``: Get all public livestreams from the channel. + Defaults to "videos". """ sort_by_map = {"newest": "dd", "oldest": "da", "popular": "p"} - filter_by_map = {"uploads": "0", "past_streams": "2&live_view=503", "future_streams": "2&live_view=502"} - url = "{url}/videos?view={filter_by}&sort={sort_by}&flow=grid".format( + url = "{url}/{type}&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], - filter_by=filter_by_map[filter] + type=content_type ) api_endpoint = "https://www.youtube.com/youtubei/v1/browse" videos = get_videos(url, api_endpoint, "gridVideoRenderer", limit, sleep)