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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
venv/
70 changes: 45 additions & 25 deletions ytd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand All @@ -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: ')

Expand All @@ -112,54 +128,58 @@ 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()
init_message()

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:
Expand Down