3This script receives a YouTube URL on stdin, and removes the `list`
6I use this in conjunction with `furl` and `youtube-dl`. Sometimes
7I want to download a single video from YouTube, but youtube-dl tries
8to download an entire playlist. This lets me grab just the first video.
17def noplaylist(url: str) -> str:
19 Remove the playlist query parameters from a URL.
21 u = urllib.parse.urlsplit(url)
23 query = urllib.parse.parse_qs(u.query)
24 for name in ("list", "index", "pp"):
30 return urllib.parse.urlunsplit(
35 urllib.parse.urlencode(query, doseq=True),
41@pytest.mark.parametrize(
45 "https://www.youtube.com/watch?v=wrhDXOZoQ74&list=PL7lRfPsqWBLZ9HK6t-BBaVBgRlkLg_FNI&index=4&pp=iAQB",
46 "https://www.youtube.com/watch?v=wrhDXOZoQ74",
49 "https://www.youtube.com/watch?v=wrhDXOZoQ74",
50 "https://www.youtube.com/watch?v=wrhDXOZoQ74",
54def test_noplaylist(original_url: str, url: str) -> None:
56 Tests for `noplaylist()`.
58 assert noplaylist(original_url) == url
61if __name__ == "__main__":
62 url = sys.stdin.read()