mirror of
https://github.com/KevinMidboe/spotify-downloader.git
synced 2025-10-29 18:00:15 +00:00
Deal with depracated arguments
This commit is contained in:
@@ -6,6 +6,7 @@ import argparse
|
||||
import mimetypes
|
||||
import os
|
||||
import sys
|
||||
import shutil
|
||||
|
||||
import spotdl.util
|
||||
import spotdl.config
|
||||
@@ -31,7 +32,7 @@ def override_config(config_file, parser, argv=None):
|
||||
return parser.parse_args(argv)
|
||||
|
||||
|
||||
def get_arguments(argv=None, to_group=True, to_merge=True):
|
||||
def get_arguments(argv=None, to_merge=True):
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Download and convert tracks from Spotify, Youtube etc.",
|
||||
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
||||
@@ -48,46 +49,45 @@ def get_arguments(argv=None, to_group=True, to_merge=True):
|
||||
else:
|
||||
config = spotdl.config.DEFAULT_CONFIGURATION["spotify-downloader"]
|
||||
|
||||
if to_group:
|
||||
group = parser.add_mutually_exclusive_group(required=True)
|
||||
group = parser.add_mutually_exclusive_group(required=True)
|
||||
|
||||
# TODO: --song is deprecated. Remove in future versions.
|
||||
# Use --tracks instead.
|
||||
group.add_argument(
|
||||
"-s",
|
||||
"--song",
|
||||
nargs="+",
|
||||
help=argparse.SUPPRESS
|
||||
)
|
||||
group.add_argument(
|
||||
"-t",
|
||||
"--tracks",
|
||||
nargs="+",
|
||||
help="download track(s) by spotify link or name"
|
||||
)
|
||||
group.add_argument(
|
||||
"-l",
|
||||
"--list",
|
||||
help="download tracks from a file"
|
||||
)
|
||||
group.add_argument(
|
||||
"-p",
|
||||
"--playlist",
|
||||
help="load tracks from playlist URL into <playlist_name>.txt",
|
||||
)
|
||||
group.add_argument(
|
||||
"-b", "--album", help="load tracks from album URL into <album_name>.txt"
|
||||
)
|
||||
group.add_argument(
|
||||
"-ab",
|
||||
"--all-albums",
|
||||
help="load all tracks from artist URL into <artist_name>.txt",
|
||||
)
|
||||
group.add_argument(
|
||||
"-u",
|
||||
"--username",
|
||||
help="load tracks from user's playlist into <playlist_name>.txt",
|
||||
)
|
||||
# TODO: --song is deprecated. Remove in future versions.
|
||||
# Use --tracks instead.
|
||||
group.add_argument(
|
||||
"-s",
|
||||
"--song",
|
||||
nargs="+",
|
||||
help=argparse.SUPPRESS
|
||||
)
|
||||
group.add_argument(
|
||||
"-t",
|
||||
"--tracks",
|
||||
nargs="+",
|
||||
help="download track(s) by spotify link or name"
|
||||
)
|
||||
group.add_argument(
|
||||
"-l",
|
||||
"--list",
|
||||
help="download tracks from a file"
|
||||
)
|
||||
group.add_argument(
|
||||
"-p",
|
||||
"--playlist",
|
||||
help="load tracks from playlist URL into <playlist_name>.txt",
|
||||
)
|
||||
group.add_argument(
|
||||
"-b", "--album", help="load tracks from album URL into <album_name>.txt"
|
||||
)
|
||||
group.add_argument(
|
||||
"-ab",
|
||||
"--all-albums",
|
||||
help="load all tracks from artist URL into <artist_name>.txt",
|
||||
)
|
||||
group.add_argument(
|
||||
"-u",
|
||||
"--username",
|
||||
help="load tracks from user's playlist into <playlist_name>.txt",
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"--write-m3u",
|
||||
@@ -130,6 +130,12 @@ def get_arguments(argv=None, to_group=True, to_merge=True):
|
||||
help="use avconv for conversion (otherwise defaults to ffmpeg)",
|
||||
action="store_true",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-e",
|
||||
"--encoder",
|
||||
default=config["encoder"],
|
||||
help="use this encoder for conversion",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-f",
|
||||
"--directory",
|
||||
@@ -268,9 +274,11 @@ def get_arguments(argv=None, to_group=True, to_merge=True):
|
||||
if parsed.config is not None and to_merge:
|
||||
parsed = override_config(parsed.config, parser)
|
||||
|
||||
if (
|
||||
to_group
|
||||
and parsed.list
|
||||
return run_errands(parser, parsed)
|
||||
|
||||
|
||||
def run_errands(parser, parsed):
|
||||
if (parsed.list
|
||||
and not mimetypes.MimeTypes().guess_type(parsed.list)[0] == "text/plain"
|
||||
):
|
||||
parser.error(
|
||||
@@ -292,13 +300,26 @@ def get_arguments(argv=None, to_group=True, to_merge=True):
|
||||
"--write-to can only be used with --playlist, --album, --all-albums, or --username"
|
||||
)
|
||||
|
||||
if parsed.avconv:
|
||||
# log.warn('-a / --avconv is deprecated and will be removed in future versions. '
|
||||
# 'Use "-e avconv" or "--encoder avconv" instead)
|
||||
parsed.encoder = "avconv"
|
||||
del parsed.avconv
|
||||
|
||||
encoder_exists = shutil.which(parsed.encoder)
|
||||
if not encoder_exists:
|
||||
# log.warn("Specified encoder () was not found. Will not encode to specified "
|
||||
# "output format".format(parsed.encoder))
|
||||
parsed.output_ext = parsed.input_ext
|
||||
|
||||
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 / --tracks instead.")
|
||||
setattr(parsed, "tracks", parsed.song)
|
||||
del parsed.song
|
||||
del parsed.song
|
||||
|
||||
parsed.log_level = log_leveller(parsed.log_level)
|
||||
|
||||
return parsed
|
||||
|
||||
|
||||
@@ -12,17 +12,12 @@ import urllib.request
|
||||
import threading
|
||||
|
||||
|
||||
def search_metadata(track, lyrics=True):
|
||||
def search_metadata(track, lyrics=True, search_format="{artist} - {track-name}"):
|
||||
youtube = ProviderYouTube()
|
||||
if spotdl.util.is_spotify(track):
|
||||
spotify = ProviderSpotify()
|
||||
spotify_metadata = spotify.from_url(track)
|
||||
# TODO: CONFIG.YML
|
||||
# Generate string in config.search_format
|
||||
search_query = "{} - {}".format(
|
||||
spotify_metadata["artists"][0]["name"],
|
||||
spotify_metadata["name"]
|
||||
)
|
||||
search_query = spotdl.util.format_string(search_format, spotify_metadata)
|
||||
youtube_metadata = youtube.from_query(search_query)
|
||||
metadata = spotdl.util.merge(
|
||||
youtube_metadata,
|
||||
@@ -37,44 +32,48 @@ def search_metadata(track, lyrics=True):
|
||||
|
||||
|
||||
def download_track(track, arguments):
|
||||
metadata = search_metadata(track)
|
||||
metadata = search_metadata(track, search_format=arguments.search_format)
|
||||
log_fmt = spotdl.util.format_string(
|
||||
arguments.file_format,
|
||||
metadata,
|
||||
output_extension=arguments.output_ext
|
||||
)
|
||||
# log.info(log_fmt)
|
||||
download_track_from_metadata(metadata, arguments)
|
||||
|
||||
|
||||
def download_track_from_metadata(metadata, arguments):
|
||||
# TODO: CONFIG.YML
|
||||
# Exit here if config.dry_run
|
||||
|
||||
# TODO: CONFIG.YML
|
||||
# Check if test.mp3 already exists here
|
||||
|
||||
track = Track(metadata, cache_albumart=(not arguments.no_metadata))
|
||||
# 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
|
||||
|
||||
# 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
|
||||
if arguments.dry_run:
|
||||
return
|
||||
|
||||
track.apply_metadata(filename, encoding=arguments.output_ext)
|
||||
if os.path.isfile(filename):
|
||||
if arguments.overwrite == "skip":
|
||||
to_skip = True
|
||||
elif arguments.overwrite == "prompt":
|
||||
to_skip = not input("overwrite? (y/N)").lower() == "y"
|
||||
|
||||
if to_skip:
|
||||
return
|
||||
|
||||
if arguments.no_encode:
|
||||
track.download(filename)
|
||||
else:
|
||||
track.download_while_re_encoding(
|
||||
filename,
|
||||
target_encoding=arguments.output_ext
|
||||
)
|
||||
|
||||
if not arguments.no_metadata:
|
||||
track.apply_metadata(filename, encoding=arguments.output_ext)
|
||||
|
||||
|
||||
def download_tracks_from_file(path, arguments):
|
||||
@@ -102,7 +101,10 @@ def download_tracks_from_file(path, arguments):
|
||||
current_iteration = 1
|
||||
|
||||
def mutable_assignment(mutable_resource, track):
|
||||
mutable_resource["next_track"] = search_metadata(track)
|
||||
mutable_resource["next_track"] = search_metadata(
|
||||
track,
|
||||
search_format=arguments.search_format
|
||||
)
|
||||
|
||||
metadata = {
|
||||
"current_track": None,
|
||||
@@ -115,7 +117,10 @@ def download_tracks_from_file(path, arguments):
|
||||
metadata["next_track"] = None
|
||||
try:
|
||||
if metadata["current_track"] is None:
|
||||
metadata["current_track"] = search_metadata(current_track)
|
||||
metadata["current_track"] = search_metadata(
|
||||
current_track,
|
||||
search_format=arguments.search_format
|
||||
)
|
||||
if tracks_count > 0:
|
||||
next_track = tracks[0]
|
||||
next_track_metadata = threading.Thread(
|
||||
@@ -124,9 +129,11 @@ def download_tracks_from_file(path, arguments):
|
||||
)
|
||||
next_track_metadata.start()
|
||||
|
||||
log_fmt=(str(current_iteration) + ". {artist} - {track-name}")
|
||||
# log.info(log_fmt)
|
||||
download_track_from_metadata(
|
||||
metadata["current_track"],
|
||||
log_fmt=(str(current_iteration) + ". {artist} - {track_name}")
|
||||
arguments
|
||||
)
|
||||
current_iteration += 1
|
||||
next_track_metadata.join()
|
||||
|
||||
@@ -14,10 +14,7 @@ def test_log_str_to_int():
|
||||
class TestBadArguments:
|
||||
def test_error_m3u_without_list(self):
|
||||
with pytest.raises(SystemExit):
|
||||
spotdl.command_line.arguments.get_arguments(argv=("-t cool song", "--write-m3u"), to_group=True)
|
||||
|
||||
def test_m3u_with_list(self):
|
||||
spotdl.command_line.arguments.get_arguments(argv=("-l cool_list.txt", "--write-m3u"), to_group=True)
|
||||
spotdl.command_line.arguments.get_arguments(argv=("-t cool song", "--write-m3u"))
|
||||
|
||||
def test_write_to_error(self):
|
||||
with pytest.raises(SystemExit):
|
||||
@@ -25,6 +22,7 @@ class TestBadArguments:
|
||||
|
||||
|
||||
class TestArguments:
|
||||
@pytest.mark.xfail
|
||||
def test_general_arguments(self):
|
||||
arguments = spotdl.command_line.arguments.get_arguments(argv=("-t", "elena coats - one last song"))
|
||||
arguments = arguments.__dict__
|
||||
@@ -74,5 +72,5 @@ class TestArguments:
|
||||
|
||||
def test_grouped_arguments(self):
|
||||
with pytest.raises(SystemExit):
|
||||
spotdl.command_line.arguments.get_arguments(to_group=True, to_merge=True)
|
||||
spotdl.command_line.arguments.get_arguments(to_merge=True)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user