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:
		
							
								
								
									
										9
									
								
								setup.py
									
									
									
									
									
								
							
							
						
						
									
										9
									
								
								setup.py
									
									
									
									
									
								
							| @@ -15,6 +15,7 @@ setup( | |||||||
|     # https://docs.python.org/3.6/distutils/sourcedist.html#specifying-the-files-to-distribute |     # https://docs.python.org/3.6/distutils/sourcedist.html#specifying-the-files-to-distribute | ||||||
|     packages=[ |     packages=[ | ||||||
|         "spotdl", |         "spotdl", | ||||||
|  |         "spotdl.command_line", | ||||||
|         "spotdl.lyrics", |         "spotdl.lyrics", | ||||||
|         "spotdl.lyrics.providers", |         "spotdl.lyrics.providers", | ||||||
|         "spotdl.encode", |         "spotdl.encode", | ||||||
| @@ -22,8 +23,11 @@ setup( | |||||||
|         "spotdl.metadata", |         "spotdl.metadata", | ||||||
|         "spotdl.metadata.embedders", |         "spotdl.metadata.embedders", | ||||||
|         "spotdl.metadata.providers", |         "spotdl.metadata.providers", | ||||||
|  |         "spotdl.lyrics", | ||||||
|  |         "spotdl.lyrics.providers", | ||||||
|         "spotdl.authorize", |         "spotdl.authorize", | ||||||
|         "spotdl.authorize.services", |         "spotdl.authorize.services", | ||||||
|  |         "spotdl.helpers", | ||||||
|     ], |     ], | ||||||
|     version=__version__, |     version=__version__, | ||||||
|     install_requires=[ |     install_requires=[ | ||||||
| @@ -43,10 +47,10 @@ setup( | |||||||
|     description="Download songs from YouTube using Spotify song URLs or playlists with albumart and meta-tags.", |     description="Download songs from YouTube using Spotify song URLs or playlists with albumart and meta-tags.", | ||||||
|     long_description=long_description, |     long_description=long_description, | ||||||
|     long_description_content_type="text/markdown", |     long_description_content_type="text/markdown", | ||||||
|     author="Ritiek Malhotra and the spotify-downloader contributors", |     author="Ritiek Malhotra", | ||||||
|     author_email="ritiekmalhotra123@gmail.com", |     author_email="ritiekmalhotra123@gmail.com", | ||||||
|     license="MIT", |     license="MIT", | ||||||
|     python_requires=">=3.4", |     python_requires=">=3.6", | ||||||
|     url="https://github.com/ritiek/spotify-downloader", |     url="https://github.com/ritiek/spotify-downloader", | ||||||
|     download_url="https://pypi.org/project/spotdl/", |     download_url="https://pypi.org/project/spotdl/", | ||||||
|     keywords=[ |     keywords=[ | ||||||
| @@ -71,6 +75,5 @@ setup( | |||||||
|         "Topic :: Multimedia :: Sound/Audio", |         "Topic :: Multimedia :: Sound/Audio", | ||||||
|         "Topic :: Utilities", |         "Topic :: Utilities", | ||||||
|     ], |     ], | ||||||
|     # entry_points={"console_scripts": ["spotdl = spotdl.command_line.__main__:main"]}, |  | ||||||
|     entry_points={"console_scripts": ["spotdl = spotdl:main"]}, |     entry_points={"console_scripts": ["spotdl = spotdl:main"]}, | ||||||
| ) | ) | ||||||
|   | |||||||
| @@ -34,6 +34,7 @@ def main(): | |||||||
|     argument_handler = get_arguments() |     argument_handler = get_arguments() | ||||||
|     logging_level = argument_handler.get_logging_level() |     logging_level = argument_handler.get_logging_level() | ||||||
|     logger = set_logger(logging_level) |     logger = set_logger(logging_level) | ||||||
|  |  | ||||||
|     try: |     try: | ||||||
|         spotdl = Spotdl(argument_handler) |         spotdl = Spotdl(argument_handler) | ||||||
|     except ArgumentError as e: |     except ArgumentError as e: | ||||||
|   | |||||||
| @@ -22,9 +22,14 @@ _LOG_LEVELS = { | |||||||
|     "DEBUG": logging.DEBUG, |     "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( | _CONFIG_BASE = spotdl.util.merge( | ||||||
|     spotdl.config.get_config(spotdl.config.default_config_file), |  | ||||||
|     spotdl.config.DEFAULT_CONFIGURATION, |     spotdl.config.DEFAULT_CONFIGURATION, | ||||||
|  |     saved_config, | ||||||
| ) | ) | ||||||
|  |  | ||||||
|  |  | ||||||
| @@ -35,7 +40,10 @@ def get_arguments(config_base=_CONFIG_BASE): | |||||||
|         formatter_class=argparse.ArgumentDefaultsHelpFormatter, |         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( |     group.add_argument( | ||||||
|         "-s", |         "-s", | ||||||
|         "--song", |         "--song", | ||||||
| @@ -201,13 +209,11 @@ def get_arguments(config_base=_CONFIG_BASE): | |||||||
|         help="path to file to write successful tracks to", |         help="path to file to write successful tracks to", | ||||||
|     ) |     ) | ||||||
|     parser.add_argument( |     parser.add_argument( | ||||||
|         "-sci", |  | ||||||
|         "--spotify-client-id", |         "--spotify-client-id", | ||||||
|         default=defaults["spotify_client_id"], |         default=defaults["spotify_client_id"], | ||||||
|         help=argparse.SUPPRESS, |         help=argparse.SUPPRESS, | ||||||
|     ) |     ) | ||||||
|     parser.add_argument( |     parser.add_argument( | ||||||
|         "-scs", |  | ||||||
|         "--spotify-client-secret", |         "--spotify-client-secret", | ||||||
|         default=defaults["spotify_client_secret"], |         default=defaults["spotify_client_secret"], | ||||||
|         help=argparse.SUPPRESS, |         help=argparse.SUPPRESS, | ||||||
| @@ -215,9 +221,15 @@ def get_arguments(config_base=_CONFIG_BASE): | |||||||
|     parser.add_argument( |     parser.add_argument( | ||||||
|         "-c", |         "-c", | ||||||
|         "--config", |         "--config", | ||||||
|         default=spotdl.config.default_config_file, |         default=spotdl.config.DEFAULT_CONFIG_FILE, | ||||||
|         help="path to custom config.yml 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( |     parser.add_argument( | ||||||
|         "-V", |         "-V", | ||||||
|         "--version", |         "--version", | ||||||
| @@ -235,7 +247,8 @@ class ArgumentHandler: | |||||||
|             args = parser.parse_args().__dict__ |             args = parser.parse_args().__dict__ | ||||||
|  |  | ||||||
|         config_file = args.get("config") |         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) |             config = spotdl.config.read_config(config_file) | ||||||
|             parser.set_defaults(**config["spotify-downloader"]) |             parser.set_defaults(**config["spotify-downloader"]) | ||||||
|             configured_args = parser.parse_args().__dict__ |             configured_args = parser.parse_args().__dict__ | ||||||
| @@ -259,6 +272,7 @@ class ArgumentHandler: | |||||||
|  |  | ||||||
|     def run_errands(self): |     def run_errands(self): | ||||||
|         args = self.get_configured_args() |         args = self.get_configured_args() | ||||||
|  |  | ||||||
|         if (args.get("list") |         if (args.get("list") | ||||||
|             and not mimetypes.MimeTypes().guess_type(args["list"])[0] == "text/plain" |             and not mimetypes.MimeTypes().guess_type(args["list"])[0] == "text/plain" | ||||||
|         ): |         ): | ||||||
|   | |||||||
| @@ -42,13 +42,18 @@ class Spotdl: | |||||||
|         del self |         del self | ||||||
|  |  | ||||||
|     def match_arguments(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( |         AuthorizeSpotify( | ||||||
|             client_id=self.arguments["spotify_client_id"], |             client_id=self.arguments["spotify_client_id"], | ||||||
|             client_secret=self.arguments["spotify_client_secret"] |             client_secret=self.arguments["spotify_client_secret"] | ||||||
|         ) |         ) | ||||||
|         spotify_tools = SpotifyHelpers() |         spotify_tools = SpotifyHelpers() | ||||||
|         logger.debug("Received arguments:\n{}".format(self.arguments)) |  | ||||||
|  |  | ||||||
|         if self.arguments["song"]: |         if self.arguments["song"]: | ||||||
|             for track in self.arguments["song"]: |             for track in self.arguments["song"]: | ||||||
|                 if track == "-": |                 if track == "-": | ||||||
| @@ -88,6 +93,31 @@ class Spotdl: | |||||||
|             playlist = spotify_tools.fetch_playlist(playlist_url) |             playlist = spotify_tools.fetch_playlist(playlist_url) | ||||||
|             spotify_tools.write_playlist_tracks(playlist, self.arguments["write_to"]) |             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): |     def write_m3u(self, track_file, target_file=None): | ||||||
|         with open(track_file, "r") as fin: |         with open(track_file, "r") as fin: | ||||||
|             tracks = fin.read().splitlines() |             tracks = fin.read().splitlines() | ||||||
|   | |||||||
| @@ -34,7 +34,7 @@ DEFAULT_CONFIGURATION = { | |||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| default_config_file = os.path.join( | DEFAULT_CONFIG_FILE = os.path.join( | ||||||
|     appdirs.user_config_dir(), |     appdirs.user_config_dir(), | ||||||
|     "spotdl", |     "spotdl", | ||||||
|     "config.yml" |     "config.yml" | ||||||
| @@ -46,30 +46,11 @@ def read_config(config_file): | |||||||
|     return config |     return config | ||||||
|  |  | ||||||
|  |  | ||||||
| def dump_config(config_file, config=DEFAULT_CONFIGURATION): | def dump_config(config_file=None, config=DEFAULT_CONFIGURATION): | ||||||
|     with open(config_file, "w") as ymlfile: |     if config_file is None: | ||||||
|         yaml.dump(DEFAULT_CONFIGURATION, ymlfile, default_flow_style=False) |         config = yaml.dump(config, default_flow_style=False) | ||||||
|  |  | ||||||
|  |  | ||||||
| def get_config(config_file): |  | ||||||
|     if os.path.isfile(config_file): |  | ||||||
|         config = read_config(config_file) |  | ||||||
|         return config |         return config | ||||||
|  |  | ||||||
|     config_dir = os.path.dirname(config_file) |     with open(config_file, "w") as ymlfile: | ||||||
|     os.makedirs(config_dir, exist_ok=True) |         yaml.dump(config, ymlfile, default_flow_style=False) | ||||||
|     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 |  | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user