Professional Documents
Culture Documents
# Description
#========================================================================
"""
File Name : music_downloader.py
Author : ---
Date :
- Created at 8.4.17
"""
#========================================================================
# Imports
#========================================================================
import argparse
import youtube_dl
FMPEG_BIN_PATH = ""
sys.path.append(FMPEG_BIN_PATH)
#========================================================================
# Constants
#========================================================================
OUTPUT_PATH = ""
INPUT_FILE_ERROR = "Your input file path is not valid nor exists!"
INPUT_HELP = """File input path. contains links to youtube
songs,
separated by \\n"""
# YoutubeDL config
#~~~~~~~~~~~~~~~~~~
POSTPROCESSORS = [{
'key' : 'FFmpegExtractAudio',
'preferredcodec' : 'mp3',
'preferredquality' : '192',
}]
OPTIONS = {
'format' : 'bestaudio/best',
'extractaudio' : True,
'audioformat' : 'mp3',
'outtmpl' : OUTPUT_PATH,
'noplaylist' : True,
'nocheckcertificate' : True,
'postprocessors' : POSTPROCESSORS,
}
#========================================================================
# Code
#========================================================================
def is_args_valid(args):
"""
Purpose : checks if the user's arguments are valid.
Parameters :
- args : An object contains the user arguments.
if not path.isfile(args.input_file):
raise ValueError(INPUT_FILE_ERROR)
return True
def get_user_arguments():
"""
Purpose : parses the users's arguments.
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input-file", help=INPUT_HELP, required=True)
args = parser.parse_args()
return args
def get_links(args):
"""
Purspose : This function gets the links to download.
Parameters :
- args: the user's arguments.
Return Value:
- youtube_links: the youtube links to download.
"""
Parameters :
- youtube_links : A list of youtube links.
- args : The user's arguments.
- options : youtube_dl configurations.
"""
def main():
"""
Purpose : Downloads songs.
"""
args = get_user_arguments()
is_args_valid(args)
youtube_links = get_links(args)
download_songs(youtube_links, args)
if __name__ == "__main__":
main()