Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions supadata/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,18 @@ def __call__(self, id: str) -> YoutubePlaylist:

return YoutubePlaylist(**response, last_updated=last_updated)

def videos(self, id: str, limit: Optional[int] = None) -> VideoIds:
def videos(
self, id: str, limit: Optional[int] = None, type: Literal["all", "video", "short", "live"] = "all"
) -> VideoIds:
"""Get video IDs from a YouTube playlist.

Args:
id: YouTube Playlist ID.
limit: Max videos to return (default 30, max 5000).
type: Type of videos ('all', 'video', 'short', 'live'). Default 'all'.

Returns:
VideoIds object containing lists of video IDs.
VideoIds object containing lists of video IDs, short IDs, and live IDs.

Raises:
SupadataError: If the API request fails or limit is invalid.
Expand Down
22 changes: 20 additions & 2 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ def test_youtube_playlist_videos(client: Supadata, requests_mock) -> None:
]
}
requests_mock.get(
f"{client.base_url}/youtube/playlist/videos?id={playlist_id}",
f"{client.base_url}/youtube/playlist/videos?id={playlist_id}&type=all",
json=mock_response,
)

Expand All @@ -534,6 +534,24 @@ def test_youtube_playlist_videos(client: Supadata, requests_mock) -> None:
assert i in mock_response["liveIds"]


def test_youtube_playlist_videos_with_type(client: Supadata, requests_mock) -> None:
playlist_id = "PL0vfts4VzfNjQOM9VClyL5R0LeuTxlAR3"
mock_response = {
"videoIds": [],
"shortIds": ["short1", "short2"],
"liveIds": [],
}
requests_mock.get(
f"{client.base_url}/youtube/playlist/videos?id={playlist_id}&type=short",
json=mock_response,
)

playlist_videos = client.youtube.playlist.videos(playlist_id, type="short")
assert playlist_videos.video_ids == []
assert playlist_videos.short_ids == mock_response["shortIds"]
assert playlist_videos.live_ids == []


def test_youtube_playlist_videos_invalid_id(client: Supadata, requests_mock) -> None:
playlist_id = "PL0vfts4VzfNjQOM9VClyL50LeuTxlAR3"
mock_response = {
Expand All @@ -543,7 +561,7 @@ def test_youtube_playlist_videos_invalid_id(client: Supadata, requests_mock) ->
}

requests_mock.get(
f"{client.base_url}/youtube/playlist/videos?id={playlist_id}",
f"{client.base_url}/youtube/playlist/videos?id={playlist_id}&type=all",
status_code=404,
json=mock_response,
)
Expand Down