allow init of playlist from id
Browse files- pytube/contrib/playlist.py +5 -1
- tests/contrib/test_playlist.py +10 -0
pytube/contrib/playlist.py
CHANGED
@@ -24,7 +24,11 @@ class Playlist:
|
|
24 |
if proxies:
|
25 |
install_proxy(proxies)
|
26 |
|
27 |
-
|
|
|
|
|
|
|
|
|
28 |
self.playlist_url: str = (
|
29 |
"https://www.youtube.com/playlist?list=" + self.playlist_id
|
30 |
)
|
|
|
24 |
if proxies:
|
25 |
install_proxy(proxies)
|
26 |
|
27 |
+
try:
|
28 |
+
self.playlist_id: str = parse_qs(url.split("?")[1])["list"][0]
|
29 |
+
except IndexError: # assume that url is just the id
|
30 |
+
self.playlist_id = url
|
31 |
+
|
32 |
self.playlist_url: str = (
|
33 |
"https://www.youtube.com/playlist?list=" + self.playlist_id
|
34 |
)
|
tests/contrib/test_playlist.py
CHANGED
@@ -39,6 +39,16 @@ def test_init_with_watch_url(request_get):
|
|
39 |
)
|
40 |
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
@mock.patch("pytube.contrib.playlist.request.get")
|
43 |
def test_parse_links(request_get, playlist_html):
|
44 |
url = "https://www.fakeurl.com/playlist?list=whatever"
|
|
|
39 |
)
|
40 |
|
41 |
|
42 |
+
@mock.patch("pytube.contrib.playlist.request.get")
|
43 |
+
def test_init_with_watch_id(request_get):
|
44 |
+
request_get.return_value = ""
|
45 |
+
playlist = Playlist("PLS1QulWo1RIaJECMeUT4LFwJ-ghgoSH6n")
|
46 |
+
assert (
|
47 |
+
playlist.playlist_url
|
48 |
+
== "https://www.youtube.com/playlist?list=PLS1QulWo1RIaJECMeUT4LFwJ-ghgoSH6n"
|
49 |
+
)
|
50 |
+
|
51 |
+
|
52 |
@mock.patch("pytube.contrib.playlist.request.get")
|
53 |
def test_parse_links(request_get, playlist_html):
|
54 |
url = "https://www.fakeurl.com/playlist?list=whatever"
|