Then, I would move all of the main specific code into the __name__ == __main__ section because that's the purpose of that section.
#!/usr/bin/env python3.5.2
import urllib.request, urllib.parse
import os
def get_sourcecode(url):
return urllib.request.urlopen(url).read()
def get_info():
return urllib.parse.parse_qs(get_sourcecode(url).decode('unicode_escape'))
def get_video_title_path():
try:
return os.path.join(directory, '{}.mp4'.format(get_info()['title'][0]))
except KeyError:
print('Parsing error, please rerun the code!')
def get_stream_map_list():
video_links = []
for video_stream in get_info()['url']:
if "googlevideo" == video_stream.split('/')[2].split('.')[1]:
video_links.append(video_stream)
return video_links
quality_map = {'small': 0, 'medium': 1, 'high': 2}
def download_video(quality='medium'):
k = quality_map.get(quality)
map_list = get_stream_map_list()[k]
title_path = get_video_title_path()
print("Downloading {} -----------> {} in {} quality.".format(
get_stream_map_list()[k]map_list, get_video_title_path()title_path, quality))
return urllib.request.urlretrieve(get_stream_map_listmap_list, title_path)
if __name__ == '__main__':
print("\n * * * * * * * * * * * * * * *")
print(" * *")[k],
get_video_title_path print(" * Youtube Video Downloader! *")
print(" * *")
print(" * * * * * * * * * * * * * * *\n")
# make sure to change directory path
directory = r'C:\Users\SalahGfx\Desktop\DownloadedVideos'
while True:
try:
url, quality = input(
"Please type a youtube video url and its quality(choose between high, medium and small) and separate them with a space: ").split()
except ValueError:
print("Please type the right url and quality.");
print("Make sure the url start with 'https://www.youtube.com/'");
print("high - for downloading high quality video");
print("medium - for downloading medium quality video");
print("small - for downloading small quality video");
continue
if validate_url(url.startswith('https://www.youtube.com/') and quality in quality_map:
get_sourcecode(url)
download_video(quality)
break