diff --git a/setup.py b/setup.py index 917b20a..6eb5f40 100644 --- a/setup.py +++ b/setup.py @@ -15,6 +15,7 @@ setup( # https://docs.python.org/3.6/distutils/sourcedist.html#specifying-the-files-to-distribute packages=[ "spotdl", + "spotdl.command_line", "spotdl.lyrics", "spotdl.lyrics.providers", "spotdl.encode", @@ -22,8 +23,11 @@ setup( "spotdl.metadata", "spotdl.metadata.embedders", "spotdl.metadata.providers", + "spotdl.lyrics", + "spotdl.lyrics.providers", "spotdl.authorize", "spotdl.authorize.services", + "spotdl.helpers", ], version=__version__, install_requires=[ @@ -43,10 +47,10 @@ setup( description="Download songs from YouTube using Spotify song URLs or playlists with albumart and meta-tags.", long_description=long_description, long_description_content_type="text/markdown", - author="Ritiek Malhotra and the spotify-downloader contributors", + author="Ritiek Malhotra", author_email="ritiekmalhotra123@gmail.com", license="MIT", - python_requires=">=3.4", + python_requires=">=3.6", url="https://github.com/ritiek/spotify-downloader", download_url="https://pypi.org/project/spotdl/", keywords=[ @@ -71,6 +75,5 @@ setup( "Topic :: Multimedia :: Sound/Audio", "Topic :: Utilities", ], - # entry_points={"console_scripts": ["spotdl = spotdl.command_line.__main__:main"]}, entry_points={"console_scripts": ["spotdl = spotdl:main"]}, ) diff --git a/spotdl/command_line/__main__.py b/spotdl/command_line/__main__.py index d4b4a45..ab3f0b5 100644 --- a/spotdl/command_line/__main__.py +++ b/spotdl/command_line/__main__.py @@ -34,6 +34,7 @@ def main(): argument_handler = get_arguments() logging_level = argument_handler.get_logging_level() logger = set_logger(logging_level) + try: spotdl = Spotdl(argument_handler) except ArgumentError as e: diff --git a/spotdl/command_line/arguments.py b/spotdl/command_line/arguments.py index df07a8e..7d22b00 100644 --- a/spotdl/command_line/arguments.py +++ b/spotdl/command_line/arguments.py @@ -22,9 +22,14 @@ _LOG_LEVELS = { "DEBUG": logging.DEBUG, } +if os.path.isfile(spotdl.config.DEFAULT_CONFIG_FILE): + saved_config = spotdl.config.read_config(spotdl.config.DEFAULT_CONFIG_FILE) +else: + saved_config = {} + _CONFIG_BASE = spotdl.util.merge( - spotdl.config.get_config(spotdl.config.default_config_file), spotdl.config.DEFAULT_CONFIGURATION, + saved_config, ) @@ -35,7 +40,10 @@ def get_arguments(config_base=_CONFIG_BASE): formatter_class=argparse.ArgumentDefaultsHelpFormatter, ) - group = parser.add_mutually_exclusive_group(required=True) + # `--remove-config` does not require the any of the group arguments to be passed. + require_group_args = not "--remove-config" in sys.argv[1:] + group = parser.add_mutually_exclusive_group(required=require_group_args) + group.add_argument( "-s", "--song", @@ -201,13 +209,11 @@ def get_arguments(config_base=_CONFIG_BASE): help="path to file to write successful tracks to", ) parser.add_argument( - "-sci", "--spotify-client-id", default=defaults["spotify_client_id"], help=argparse.SUPPRESS, ) parser.add_argument( - "-scs", "--spotify-client-secret", default=defaults["spotify_client_secret"], help=argparse.SUPPRESS, @@ -215,9 +221,15 @@ def get_arguments(config_base=_CONFIG_BASE): parser.add_argument( "-c", "--config", - default=spotdl.config.default_config_file, + default=spotdl.config.DEFAULT_CONFIG_FILE, help="path to custom config.yml file" ) + parser.add_argument( + "--remove-config", + default=False, + action="store_true", + help="remove previously saved config" + ) parser.add_argument( "-V", "--version", @@ -235,7 +247,8 @@ class ArgumentHandler: args = parser.parse_args().__dict__ config_file = args.get("config") - if config_file: + configured_args = args.copy() + if config_file and os.path.isfile(config_file): config = spotdl.config.read_config(config_file) parser.set_defaults(**config["spotify-downloader"]) configured_args = parser.parse_args().__dict__ @@ -259,6 +272,7 @@ class ArgumentHandler: def run_errands(self): args = self.get_configured_args() + if (args.get("list") and not mimetypes.MimeTypes().guess_type(args["list"])[0] == "text/plain" ): diff --git a/spotdl/command_line/lib.py b/spotdl/command_line/lib.py index d1f43de..204d0bb 100644 --- a/spotdl/command_line/lib.py +++ b/spotdl/command_line/lib.py @@ -42,13 +42,18 @@ class Spotdl: del self def match_arguments(self): + logger.debug("Received arguments:\n{}".format(self.arguments)) + + if self.arguments["remove_config"]: + self.remove_saved_config() + return + self.save_default_config() + AuthorizeSpotify( client_id=self.arguments["spotify_client_id"], client_secret=self.arguments["spotify_client_secret"] ) spotify_tools = SpotifyHelpers() - logger.debug("Received arguments:\n{}".format(self.arguments)) - if self.arguments["song"]: for track in self.arguments["song"]: if track == "-": @@ -88,6 +93,31 @@ class Spotdl: playlist = spotify_tools.fetch_playlist(playlist_url) spotify_tools.write_playlist_tracks(playlist, self.arguments["write_to"]) + def save_config(self, config_file=spotdl.config.DEFAULT_CONFIG_FILE, config=spotdl.config.DEFAULT_CONFIGURATION): + config_dir = os.path.dirname(config_file) + os.makedirs(config_dir, exist_ok=True) + logger.info('Writing configuration to "{0}":'.format(config_file)) + spotdl.config.dump_config(config_file=config_file, config=spotdl.config.DEFAULT_CONFIGURATION) + config = spotdl.config.dump_config(config=spotdl.config.DEFAULT_CONFIGURATION["spotify-downloader"]) + for line in config.split("\n"): + if line.strip(): + logger.info(line.strip()) + logger.info( + "Please note that command line arguments have higher priority " + "than their equivalents in the configuration file.\n" + ) + + def save_default_config(self): + if not os.path.isfile(spotdl.config.DEFAULT_CONFIG_FILE): + self.save_config() + + def remove_saved_config(self, config_file=spotdl.config.DEFAULT_CONFIG_FILE): + if os.path.isfile(spotdl.config.DEFAULT_CONFIG_FILE): + logger.info('Removing "{}".'.format(spotdl.config.DEFAULT_CONFIG_FILE)) + os.remove(spotdl.config.DEFAULT_CONFIG_FILE) + else: + logger.info('File does not exist: "{}".'.format(spotdl.config.DEFAULT_CONFIG_FILE)) + def write_m3u(self, track_file, target_file=None): with open(track_file, "r") as fin: tracks = fin.read().splitlines() diff --git a/spotdl/config.py b/spotdl/config.py index a434dd8..c979292 100644 --- a/spotdl/config.py +++ b/spotdl/config.py @@ -34,7 +34,7 @@ DEFAULT_CONFIGURATION = { } } -default_config_file = os.path.join( +DEFAULT_CONFIG_FILE = os.path.join( appdirs.user_config_dir(), "spotdl", "config.yml" @@ -46,30 +46,11 @@ def read_config(config_file): return config -def dump_config(config_file, config=DEFAULT_CONFIGURATION): - with open(config_file, "w") as ymlfile: - yaml.dump(DEFAULT_CONFIGURATION, ymlfile, default_flow_style=False) - - -def get_config(config_file): - if os.path.isfile(config_file): - config = read_config(config_file) +def dump_config(config_file=None, config=DEFAULT_CONFIGURATION): + if config_file is None: + config = yaml.dump(config, default_flow_style=False) return config - config_dir = os.path.dirname(config_file) - os.makedirs(config_dir, exist_ok=True) - dump_config(config_file, config=DEFAULT_CONFIGURATION) - - logger.info("Writing default configuration to {0}.".format(config_file)) - - for line in yaml.dump( - DEFAULT_CONFIGURATION["spotify-downloader"], default_flow_style=False - ).split("\n"): - if line.strip(): - logger.info(line.strip()) - logger.info( - "Please note that command line arguments have higher priority " - "than their equivalents in the configuration file." - ) - return DEFAULT_CONFIGURATION + with open(config_file, "w") as ymlfile: + yaml.dump(config, ymlfile, default_flow_style=False)