From 9bdd58f92a3c1afcd2e6147119e1dcb4f6f2e1be Mon Sep 17 00:00:00 2001 From: Robert Date: Thu, 9 May 2024 23:22:42 -0700 Subject: [PATCH] Playlists are now checked for --- Get_Playlist_URLs.py | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/Get_Playlist_URLs.py b/Get_Playlist_URLs.py index 0c3444f..aaa39ae 100644 --- a/Get_Playlist_URLs.py +++ b/Get_Playlist_URLs.py @@ -1,15 +1,16 @@ import sys import yt_dlp +from urllib.parse import urlparse, parse_qs def get_playlist_videos(playlist_url): ydl_opts = { - 'extract_flat': True, - 'skip_download': True, - 'quiet': True + 'extract_flat': True, 'skip_download': True, + 'quiet': False } - with yt_dlp.YoutubeDL(ydl_opts) as ydl: - info = ydl.extract_info(playlist_url, download=False) + try: + with yt_dlp.YoutubeDL(ydl_opts) as ydl: + info = ydl.extract_info(playlist_url, download=False) if 'entries' in info: video_urls = [entry['url'] for entry in info['entries']] @@ -18,19 +19,39 @@ def get_playlist_videos(playlist_url): else: print("No videos found in the playlist.") return [], None + except Exception as e: + print(f"An error occurred: {e}") + return [], None + def save_to_file(video_urls, filename): with open(filename, 'w') as file: file.write('\n'.join(video_urls)) print(f"Video URLs saved to {filename}") + +def parse_playlist_url(url): + parsed_url = urlparse(url) + query_params = parse_qs(parsed_url.query) + + if 'list' in query_params: + playlist_id = query_params['list'][0] + base_url = f"{parsed_url.scheme}://{parsed_url.netloc}{parsed_url.path}" + playlist_url = f"{base_url}?list={playlist_id}" + return playlist_url + else: + return url + + if __name__ == '__main__': if len(sys.argv) < 2: print("Please provide the playlist URL as a command-line argument.") + print("""Example:\n\t python Get_Playlist_URLs.py "https://www.youtube.com/playlist?list=PLH15HpR5qRsWalnnt-9eYELxbEcYBPB6I" """) sys.exit(1) playlist_url = sys.argv[1] - video_urls, playlist_title = get_playlist_videos(playlist_url) + parsed_playlist_url = parse_playlist_url(playlist_url) + video_urls, playlist_title = get_playlist_videos(parsed_playlist_url) if video_urls: filename = f"{playlist_title}.txt"