Very brittle command-line frontend

This commit is contained in:
Ritiek Malhotra
2020-04-11 22:03:47 +05:30
parent 14104e6870
commit 9afd14282a
16 changed files with 167 additions and 220 deletions

View File

@@ -0,0 +1,3 @@
from spotdl.command_line.arguments import get_arguments
from spotdl.command_line import helpers

View File

@@ -1,55 +1,61 @@
def match_args():
if const.args.song:
for track in const.args.song:
track_dl = downloader.Downloader(raw_song=track)
track_dl.download_single()
elif const.args.list:
if const.args.write_m3u:
from spotdl.authorize.services import AuthorizeSpotify
from spotdl import command_line
def match_arguments(arguments):
if arguments.tracks:
# TODO: Also support reading from stdin for -t parameter
# Also supported writing to stdout for all parameters
if len(arguments.tracks) > 1:
# log.warning("download multiple tracks with optimized list instead")
pass
for track in arguments.tracks:
command_line.helpers.download_track(track, arguments)
elif arguments.list:
if arguments.write_m3u:
youtube_tools.generate_m3u(
track_file=const.args.list
track_file=arguments.list
)
else:
list_dl = downloader.ListDownloader(
tracks_file=const.args.list,
skip_file=const.args.skip,
write_successful_file=const.args.write_successful,
tracks_file=arguments.list,
skip_file=arguments.skip,
write_successful_file=arguments.write_successful,
)
list_dl.download_list()
elif const.args.playlist:
elif arguments.playlist:
spotify_tools.write_playlist(
playlist_url=const.args.playlist, text_file=const.args.write_to
playlist_url=arguments.playlist, text_file=arguments.write_to
)
elif const.args.album:
elif arguments.album:
spotify_tools.write_album(
album_url=const.args.album, text_file=const.args.write_to
album_url=arguments.album, text_file=arguments.write_to
)
elif const.args.all_albums:
elif arguments.all_albums:
spotify_tools.write_all_albums_from_artist(
artist_url=const.args.all_albums, text_file=const.args.write_to
artist_url=arguments.all_albums, text_file=arguments.write_to
)
elif const.args.username:
elif arguments.username:
spotify_tools.write_user_playlist(
username=const.args.username, text_file=const.args.write_to
username=arguments.username, text_file=arguments.write_to
)
def main():
const.args = handle.get_arguments()
arguments = command_line.get_arguments()
internals.filter_path(const.args.folder)
youtube_tools.set_api_key()
AuthorizeSpotify(
client_id=arguments.spotify_client_id,
client_secret=arguments.spotify_client_secret
)
# youtube_tools.set_api_key()
logzero.setup_default_logger(formatter=const._formatter, level=const.args.log_level)
# logzero.setup_default_logger(formatter=const._formatter, level=const.args.log_level)
try:
match_args()
# actually we don't necessarily need this, but yeah...
# explicit is better than implicit!
sys.exit(0)
match_arguments(arguments)
except KeyboardInterrupt as e:
# log.exception(e)
sys.exit(3)
sys.exit(2)
if __name__ == "__main__":

View File

@@ -23,7 +23,10 @@ def log_leveller(log_level_str):
def override_config(config_file, parser, argv=None):
""" Override default dict with config dict passed as comamnd line argument. """
config_file = os.path.realpath(config_file)
config = spotdl.util.merge(DEFAULT_CONFIGURATION["spotify-downloader"], spotdl.config.get_config(config_file))
config = spotdl.util.merge(
spotdl.config.DEFAULT_CONFIGURATION["spotify-downloader"],
spotdl.config.get_config(config_file)
)
parser.set_defaults(**config)
return parser.parse_args(argv)
@@ -34,8 +37,8 @@ def get_arguments(argv=None, to_group=True, to_merge=True):
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
config_file = spotdl.config.default_config_file
if to_merge:
config_file = spotdl.config.default_config_file
config_dir = os.path.dirname(spotdl.config.default_config_file)
os.makedirs(os.path.dirname(spotdl.config.default_config_file), exist_ok=True)
config = spotdl.util.merge(
@@ -49,7 +52,7 @@ def get_arguments(argv=None, to_group=True, to_merge=True):
group = parser.add_mutually_exclusive_group(required=True)
# TODO: --song is deprecated. Remove in future versions.
# Use --track instead.
# Use --tracks instead.
group.add_argument(
"-s",
"--song",
@@ -58,9 +61,9 @@ def get_arguments(argv=None, to_group=True, to_merge=True):
)
group.add_argument(
"-t",
"--track",
"--tracks",
nargs="+",
help="download track by spotify link or name"
help="download track(s) by spotify link or name"
)
group.add_argument(
"-l",
@@ -143,14 +146,14 @@ def get_arguments(argv=None, to_group=True, to_merge=True):
"-i",
"--input-ext",
default=config["input-ext"],
help="preferred input format .m4a or .webm (Opus)",
choices={".m4a", ".webm"},
help="preferred input format 'm4a' or 'webm' (Opus)",
choices={"m4a", "webm"},
)
parser.add_argument(
"-o",
"--output-ext",
default=config["output-ext"],
help="preferred output format .mp3, .m4a (AAC), .flac, etc.",
help="preferred output format: 'mp3', 'm4a' (AAC), 'flac', etc.",
)
parser.add_argument(
"--write-to",
@@ -163,7 +166,7 @@ def get_arguments(argv=None, to_group=True, to_merge=True):
default=config["file-format"],
help="file format to save the downloaded track with, each tag "
"is surrounded by curly braces. Possible formats: "
"{}".format([spotdl.util.formats[x] for x in spotdl.util.formats]),
# "{}".format([spotdl.util.formats[x] for x in spotdl.util.formats]),
)
parser.add_argument(
"--trim-silence",
@@ -177,7 +180,7 @@ def get_arguments(argv=None, to_group=True, to_merge=True):
default=config["search-format"],
help="search format to search for on YouTube, each tag "
"is surrounded by curly braces. Possible formats: "
"{}".format([spotdl.util.formats[x] for x in spotdl.util.formats]),
# "{}".format([spotdl.util.formats[x] for x in spotdl.util.formats]),
)
parser.add_argument(
"-dm",
@@ -238,19 +241,19 @@ def get_arguments(argv=None, to_group=True, to_merge=True):
parser.add_argument(
"-sci",
"--spotify-client-id",
default=config["spotify_client_id"],
default=config["spotify-client-id"],
help=argparse.SUPPRESS,
)
parser.add_argument(
"-scs",
"--spotify-client-secret",
default=config["spotify_client_secret"],
default=config["spotify-client-secret"],
help=argparse.SUPPRESS,
)
parser.add_argument(
"-c",
"--config",
default=None,
default=config_file,
help="path to custom config.yml file"
)
parser.add_argument(
@@ -289,11 +292,11 @@ def get_arguments(argv=None, to_group=True, to_merge=True):
"--write-to can only be used with --playlist, --album, --all-albums, or --username"
)
song_parameter_passed = parsed.song is not None and parsed.track is None
song_parameter_passed = parsed.song is not None and parsed.tracks is None
if song_parameter_passed:
# log.warn("-s / --song is deprecated and will be removed in future versions. "
# "Use -t / --track instead.")
setattr(parsed, "track", parsed.song)
# "Use -t / --tracks instead.")
setattr(parsed, "tracks", parsed.song)
del parsed.song
parsed.log_level = log_leveller(parsed.log_level)

View File

@@ -36,7 +36,12 @@ def search_metadata(track, lyrics=True):
return metadata
def download_track(metadata, arguments):
def download_track(track, arguments):
metadata = search_metadata(track)
download_track_from_metadata(metadata, arguments)
def download_track_from_metadata(metadata, arguments):
# TODO: CONFIG.YML
# Exit here if config.dry_run
@@ -45,19 +50,31 @@ def download_track(metadata, arguments):
# log.info(log_fmt)
track = Track(metadata, cache_albumart=True)
# TODO: CONFIG.YML
# Download tracks with name config.file_format
# TODO: CONFIG.YML
# Append config.output_ext to config.file_format
track = Track(metadata, cache_albumart=True)
track.download_while_re_encoding("test.mp3")
# TODO: CONFIG.YML
# Check config.overwrite here
filename = spotdl.util.format_string(
arguments.file_format,
metadata,
output_extension=arguments.output_ext
)
track.download_while_re_encoding(
filename,
target_encoding=arguments.output_ext
)
# TODO: CONFIG.YML
# Skip metadata if config.no_metadata
track.apply_metadata("test.mp3")
track.apply_metadata(filename, encoding=arguments.output_ext)
def download_tracks_from_file(path, arguments):
@@ -107,7 +124,10 @@ def download_tracks_from_file(path, arguments):
)
next_track_metadata.start()
download_track(metadata["current_track"], log_fmt=(str(current_iteration) + ". {artist} - {track_name}"))
download_track_from_metadata(
metadata["current_track"],
log_fmt=(str(current_iteration) + ". {artist} - {track_name}")
)
current_iteration += 1
next_track_metadata.join()
except (urllib.request.URLError, TypeError, IOError) as e: