Sie sind auf Seite 1von 3

#========================================================================

# Description
#========================================================================

"""
File Name : music_downloader.py

Purpose : Download automatically music.

Author : ---

Date :
- Created at 8.4.17
"""

#========================================================================
# Imports
#========================================================================

import argparse
import youtube_dl

from os import path


from os import sys

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.

Return Value: True if valid, otherwise raises exception.


"""

if not path.isfile(args.input_file):
raise ValueError(INPUT_FILE_ERROR)

return True

def get_user_arguments():
"""
Purpose : parses the users's arguments.

Return Value: The user arguments in an object.


"""

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.
"""

with open(args.input_file) as input_file_handler:


return input_file_handler.readlines()

def download_songs(youtube_links, args, options=OPTIONS):


"""
Purpose : This function downloads the songs.

Parameters :
- youtube_links : A list of youtube links.
- args : The user's arguments.
- options : youtube_dl configurations.
"""

with youtube_dl.YoutubeDL(options) as youtube_downloader:


youtube_downloader.download(youtube_links)

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()

Das könnte Ihnen auch gefallen