Files
spotify-downloader/core/handle.py
Ritiek Malhotra fcfebc55e6 Save file names using a custom format specifiers (#205)
* Use custom formats to generate file name

* Do not mess up search term

* Fix tests

* Fix conflicting names

* Fix subprocess call on file paths with spaces

* Create directories and keep spaces as defaults

* Fix config merge

* Remove underscores from default file format

* Remove debug info

* Show possible formats in usage help
2018-01-19 13:58:27 +05:30

137 lines
5.2 KiB
Python

from core import internals
import logging
import yaml
import argparse
import os
import sys
_LOG_LEVELS_STR = ['INFO', 'WARNING', 'ERROR', 'DEBUG']
default_conf = { 'spotify-downloader':
{ 'manual' : False,
'no-metadata' : False,
'avconv' : False,
'folder' : os.path.join(sys.path[0], 'Music'),
'overwrite' : 'prompt',
'input-ext' : '.m4a',
'output-ext' : '.mp3',
'download-only-metadata' : False,
'dry-run' : False,
'music-videos-only' : False,
'no-spaces' : False,
'file-format' : '{artist} - {track_name}',
'log-level' : 'INFO' }
}
def log_leveller(log_level_str):
loggin_levels = [logging.INFO, logging.WARNING, logging.ERROR, logging.DEBUG]
log_level_str_index = _LOG_LEVELS_STR.index(log_level_str)
loggin_level = loggin_levels[log_level_str_index]
return loggin_level
def merge(default, config):
""" Override default dict with config dict. """
merged = default.copy()
merged.update(config)
return merged
def get_config(config_file):
try:
with open(config_file, 'r') as ymlfile:
cfg = yaml.load(ymlfile)
except FileNotFoundError:
with open(config_file, 'w') as ymlfile:
yaml.dump(default_conf, ymlfile, default_flow_style=False)
cfg = default_conf
return cfg['spotify-downloader']
def get_arguments(raw_args=None, to_group=True, to_merge=True):
parser = argparse.ArgumentParser(
description='Download and convert songs from Spotify, Youtube etc.',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
if to_merge:
config_file = os.path.join(sys.path[0], 'config.yml')
config = merge(default_conf['spotify-downloader'], get_config(config_file))
else:
config = default_conf['spotify-downloader']
if to_group:
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument(
'-s', '--song', help='download song by spotify link or name')
group.add_argument(
'-l', '--list', help='download songs from a file')
group.add_argument(
'-p', '--playlist', help='load songs from playlist URL into <playlist_name>.txt')
group.add_argument(
'-b', '--album', help='load songs from album URL into <album_name>.txt')
group.add_argument(
'-u', '--username',
help="load songs from user's playlist into <playlist_name>.txt")
parser.add_argument(
'-m', '--manual', default=config['manual'],
help='choose the song to download manually', action='store_true')
parser.add_argument(
'-nm', '--no-metadata', default=config['no-metadata'],
help='do not embed metadata in songs', action='store_true')
parser.add_argument(
'-a', '--avconv', default=config['avconv'],
help='Use avconv for conversion otherwise set defaults to ffmpeg',
action='store_true')
parser.add_argument(
'-f', '--folder', default=os.path.relpath(config['folder'], os.getcwd()),
help='path to folder where files will be stored in')
parser.add_argument(
'--overwrite', default=config['overwrite'],
help='change the overwrite policy',
choices={'prompt', 'force', 'skip'})
parser.add_argument(
'-i', '--input-ext', default=config['input-ext'],
help='prefered input format .m4a or .webm (Opus)')
parser.add_argument(
'-o', '--output-ext', default=config['output-ext'],
help='prefered output extension .mp3 or .m4a (AAC)')
parser.add_argument(
'-ff', '--file-format', default=config['file-format'],
help='File format to save the downloaded song with, each tag '
'is surrounded by curly braces. Possible formats: '
'{}'.format([internals.formats[x] for x in internals.formats]),
action='store_true')
parser.add_argument(
'-dm', '--download-only-metadata', default=config['download-only-metadata'],
help='download songs for which metadata is found',
action='store_true')
parser.add_argument(
'-d', '--dry-run', default=config['dry-run'],
help='Show only track title and YouTube URL',
action='store_true')
parser.add_argument(
'-mo', '--music-videos-only', default=config['music-videos-only'],
help='Search only for music on Youtube',
action='store_true')
parser.add_argument(
'-ns', '--no-spaces', default=config['no-spaces'],
help='Replace spaces with underscores in file names',
action='store_true')
parser.add_argument(
'-ll', '--log-level', default=config['log-level'],
choices=_LOG_LEVELS_STR,
type=str.upper,
help='set log verbosity')
parsed = parser.parse_args(raw_args)
parsed.log_level = log_leveller(parsed.log_level)
return parsed