diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9771680 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +venv/ \ No newline at end of file diff --git a/ytd.py b/ytd.py index 519b80b..e85c90e 100644 --- a/ytd.py +++ b/ytd.py @@ -68,6 +68,33 @@ def init_message(): def exit_message(t): print("\n %s Has been downloaded" % t) +def ytdl_download(ytdl_options, song_url): + with YoutubeDL(ytdl_options) as ytdl: + try: + song_title = get_title(song_url) + print("Downloading %s" % song_title) + ytdl.download([song_url]) + exit_message(song_title) + except: + print('Error downloading %s' %song_title) + return None + +def validate_url(url): + parse = urllib.parse.urlparse(url) + if parse.scheme not in ['http', 'https']: + return False + if '.' not in parse.netlock: + return False + return True + +def validate_and_fix_url(url): + url = url.lower() + if not validate_url(url): + if 'www' in url: + url = url.replace('www.youtube.com/','https://www.youtube.com/') + else: + url = url.replace('youtube.com/','https://www.youtube.com/') + return url def download(song=None, folder_path=None, playlist=False, playlist_items=None, playlist_start=1, playlist_end=None): @@ -86,17 +113,6 @@ def download(song=None, folder_path=None, playlist=False, playlist_items=None, p }] } - def ytdl_download(song_url): - with YoutubeDL(ytdl_options) as ytdl: - try: - song_title = get_title(song_url) - print("Downloading %s" % song_title) - ytdl.download([song_url]) - exit_message(song_title) - except: - print('Error downloading %s' %song_title) - return None - if not song: song = user_input('Enter the name of the song or the URL: ') @@ -112,10 +128,11 @@ def ytdl_download(song_url): print("There's some problem in your network") return None - ytdl_download(video_url) + ytdl_download(ytdl_options, video_url) else: - ytdl_download(song) + video_url = validate_and_fix_url(song) + ytdl_download(ytdl_options, video_url) def main(): screen_clear() @@ -123,43 +140,46 @@ def main(): default_path = check_args(sys.argv[1:]).default config_default = config_settings(new_path=default_path) - path = check_args(sys.argv[1:], default=config_default).output + arguments = check_args(sys.argv[1:], default=config_default) + + path = arguments.output + try: - if check_args(sys.argv[1:]).video: - download(song=check_args(sys.argv[1:]).video, folder_path=path) - elif check_args(sys.argv[1:]).playlist: + if arguments.video: + download(song=arguments.video, folder_path=path) + elif arguments.playlist: #check if --playlist_items format is correct - if check_args(sys.argv[1:]).playlist_items: + if arguments.playlist_items: try: # the following assignment is only to check the format, after that we can turn back to a str value. - playlist_items = [int(item) for item in check_args(sys.argv[1:]).playlist_items.split(',')] - playlist_items = check_args(sys.argv[1:]).playlist_items + playlist_items = [int(item) for item in arguments.playlist_items.split(',')] + playlist_items = arguments.playlist_items except ValueError: print("ValueError: --playlist-items must be integers and divided by commas, e.g. -pi '1,2,4'") else: playlist_items = None # check if --playlist-start can be converted into an integer, otherwise 1 is assigned - if check_args(sys.argv[1:]).playlist_start: + if arguments.playlist_start: try: - playlist_start = int(check_args(sys.argv[1:]).playlist_start) + playlist_start = int(arguments.playlist_start) except ValueError: print('ValueError: --playlist-start must an integer') else: playlist_start = 1 # check if --playlist-start can be converted into an integer, otherwise None value is assigned - if check_args(sys.argv[1:]).playlist_end: + if arguments.playlist_end: try: - playlist_end = int(check_args(sys.argv[1:]).playlist_end) + playlist_end = int(arguments.playlist_end) except ValueError: print('ValueError: --playlist-end must be an integer') else: playlist_end = None print(playlist_items, playlist_start, playlist_end, sep='----\n', end='----\n') - download(song=check_args(sys.argv[1:]).playlist, folder_path=path, playlist=True, + download(song=arguments.playlist, folder_path=path, playlist=True, playlist_items=playlist_items, playlist_start=playlist_start, playlist_end=playlist_end) else: while True: