mirror of
				https://github.com/KevinMidboe/spotify-downloader.git
				synced 2025-10-29 18:00:15 +00:00 
			
		
		
		
	Add --remove-config
This commit is contained in:
		| @@ -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: | ||||
|   | ||||
| @@ -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" | ||||
|         ): | ||||
|   | ||||
| @@ -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() | ||||
|   | ||||
		Reference in New Issue
	
	Block a user