mirror of
				https://github.com/THIS-IS-NOT-A-BACKUP/zspotify.git
				synced 2025-11-03 21:10:34 +00:00 
			
		
		
		
	Supply config values per commandline args
This commit is contained in:
		
							parent
							
								
									9082938dfd
								
							
						
					
					
						commit
						a35d46e6ae
					
				@ -1,8 +1,7 @@
 | 
			
		||||
import argparse
 | 
			
		||||
 | 
			
		||||
from app import client
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
from config import CONFIG_VALUES
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    parser = argparse.ArgumentParser(prog='zspotify',
 | 
			
		||||
@ -35,6 +34,12 @@ if __name__ == '__main__':
 | 
			
		||||
                       type=str,
 | 
			
		||||
                       help='Downloads tracks, playlists and albums from the URLs written in the file passed.')
 | 
			
		||||
 | 
			
		||||
    for configkey in CONFIG_VALUES:
 | 
			
		||||
        parser.add_argument(CONFIG_VALUES[configkey]['arg'],
 | 
			
		||||
                            type=str,
 | 
			
		||||
                            default=None,
 | 
			
		||||
                            help='Specify the value of the ['+configkey+'] config value')
 | 
			
		||||
 | 
			
		||||
    parser.set_defaults(func=client)
 | 
			
		||||
 | 
			
		||||
    args = parser.parse_args()
 | 
			
		||||
 | 
			
		||||
@ -20,19 +20,19 @@ LANGUAGE = 'LANGUAGE'
 | 
			
		||||
BITRATE = 'BITRATE'
 | 
			
		||||
 | 
			
		||||
CONFIG_VALUES = {
 | 
			
		||||
    ROOT_PATH:                  { 'default': '../ZSpotify Music/',    'type': 'str',  'arg': '--root-path'                  },
 | 
			
		||||
    ROOT_PODCAST_PATH:          { 'default': '../ZSpotify Podcasts/', 'type': 'str',  'arg': '--root-podcast-path'          },
 | 
			
		||||
    SKIP_EXISTING_FILES:        { 'default': True,                    'type': 'bool', 'arg': '--skip-existing-files'        },
 | 
			
		||||
    SKIP_PREVIOUSLY_DOWNLOADED: { 'default': False,                   'type': 'bool', 'arg': '--skip-previously-downloaded' },
 | 
			
		||||
    DOWNLOAD_FORMAT:            { 'default': 'ogg',                   'type': 'str',  'arg': '--download-format'            },
 | 
			
		||||
    FORCE_PREMIUM:              { 'default': False,                   'type': 'bool', 'arg': '--force-premium'              },
 | 
			
		||||
    ANTI_BAN_WAIT_TIME:         { 'default': 1,                       'type': 'int',  'arg': '--anti-ban-wait-time'         },
 | 
			
		||||
    OVERRIDE_AUTO_WAIT:         { 'default': False,                   'type': 'bool', 'arg': '--override-auto-wait'         },
 | 
			
		||||
    CHUNK_SIZE:                 { 'default': 50000,                   'type': 'int',  'arg': '--chunk-size'                 },
 | 
			
		||||
    SPLIT_ALBUM_DISCS:          { 'default': False,                   'type': 'bool', 'arg': '--split-album-discs'          },
 | 
			
		||||
    DOWNLOAD_REAL_TIME:         { 'default': False,                   'type': 'bool', 'arg': '--download-real-time'         },
 | 
			
		||||
    LANGUAGE:                   { 'default': 'en',                    'type': 'str',  'arg': '--language'                   },
 | 
			
		||||
    BITRATE:                    { 'default': '',                      'type': 'str',  'arg': '--bitrate'                    },
 | 
			
		||||
    ROOT_PATH:                  { 'default': '../ZSpotify Music/',    'type': str,  'arg': '--root-path'                  },
 | 
			
		||||
    ROOT_PODCAST_PATH:          { 'default': '../ZSpotify Podcasts/', 'type': str,  'arg': '--root-podcast-path'          },
 | 
			
		||||
    SKIP_EXISTING_FILES:        { 'default': 'True',                  'type': bool, 'arg': '--skip-existing-files'        },
 | 
			
		||||
    SKIP_PREVIOUSLY_DOWNLOADED: { 'default': 'False',                 'type': bool, 'arg': '--skip-previously-downloaded' },
 | 
			
		||||
    DOWNLOAD_FORMAT:            { 'default': 'ogg',                   'type': str,  'arg': '--download-format'            },
 | 
			
		||||
    FORCE_PREMIUM:              { 'default': 'False',                 'type': bool, 'arg': '--force-premium'              },
 | 
			
		||||
    ANTI_BAN_WAIT_TIME:         { 'default': '1',                     'type': int,  'arg': '--anti-ban-wait-time'         },
 | 
			
		||||
    OVERRIDE_AUTO_WAIT:         { 'default': 'False',                 'type': bool, 'arg': '--override-auto-wait'         },
 | 
			
		||||
    CHUNK_SIZE:                 { 'default': '50000',                 'type': int,  'arg': '--chunk-size'                 },
 | 
			
		||||
    SPLIT_ALBUM_DISCS:          { 'default': 'False',                 'type': bool, 'arg': '--split-album-discs'          },
 | 
			
		||||
    DOWNLOAD_REAL_TIME:         { 'default': 'False',                 'type': bool, 'arg': '--download-real-time'         },
 | 
			
		||||
    LANGUAGE:                   { 'default': 'en',                    'type': str,  'arg': '--language'                   },
 | 
			
		||||
    BITRATE:                    { 'default': '',                      'type': str,  'arg': '--bitrate'                    },
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -49,24 +49,57 @@ class Config:
 | 
			
		||||
 | 
			
		||||
        true_config_file_path = os.path.join(app_dir, config_fp)
 | 
			
		||||
 | 
			
		||||
        # Load config from zs_config.json
 | 
			
		||||
 | 
			
		||||
        if not os.path.exists(true_config_file_path):
 | 
			
		||||
            with open(true_config_file_path, 'w', encoding='utf-8') as config_file:
 | 
			
		||||
                json.dump(cls.get_default_json(), config_file, indent=4)
 | 
			
		||||
            cls.Values = cls.get_default_json()
 | 
			
		||||
        else:
 | 
			
		||||
            with open(true_config_file_path, encoding='utf-8') as config_file:
 | 
			
		||||
                cls.Values = json.load(config_file)
 | 
			
		||||
                jsonvalues = json.load(config_file)
 | 
			
		||||
                cls.Values = {}
 | 
			
		||||
                for key in CONFIG_VALUES:
 | 
			
		||||
                    if key in jsonvalues:
 | 
			
		||||
                        cls.Values[key] = cls.parse_arg_value(key, jsonvalues[key])
 | 
			
		||||
 | 
			
		||||
        # Add default values for missing keys
 | 
			
		||||
 | 
			
		||||
        for key in CONFIG_VALUES:
 | 
			
		||||
            if key not in cls.Values:
 | 
			
		||||
                cls.Values[key] = CONFIG_VALUES[key].default
 | 
			
		||||
                cls.Values[key] = cls.parse_arg_value(key, CONFIG_VALUES[key]['default'])
 | 
			
		||||
 | 
			
		||||
        # Override config from commandline arguments
 | 
			
		||||
 | 
			
		||||
        for key in CONFIG_VALUES:
 | 
			
		||||
            if key.lower() in vars(args) and vars(args)[key.lower()] is not None:
 | 
			
		||||
                cls.Values[key] = cls.parse_arg_value(key, vars(args)[key.lower()])
 | 
			
		||||
 | 
			
		||||
        print(cls.Values, ' ', '\n')
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def get_default_json(cls) -> Any:
 | 
			
		||||
        r = {}
 | 
			
		||||
        for key in CONFIG_VALUES:
 | 
			
		||||
            r[key] = CONFIG_VALUES[key].default
 | 
			
		||||
            r[key] = CONFIG_VALUES[key]['default']
 | 
			
		||||
        return r
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def parse_arg_value(cls, key, value) -> Any:
 | 
			
		||||
        if type(value) == CONFIG_VALUES[key]['type']:
 | 
			
		||||
            return value
 | 
			
		||||
        if CONFIG_VALUES[key]['type'] == str:
 | 
			
		||||
            return str(value)
 | 
			
		||||
        if CONFIG_VALUES[key]['type'] == int:
 | 
			
		||||
            return int(value)
 | 
			
		||||
        if CONFIG_VALUES[key]['type'] == bool:
 | 
			
		||||
            if str(value).lower() in ['yes', 'true', '1']:
 | 
			
		||||
                return True
 | 
			
		||||
            if str(value).lower() in ['no', 'false', '0']:
 | 
			
		||||
                return False
 | 
			
		||||
            raise ValueError("Not a boolean: " + value)
 | 
			
		||||
        raise ValueError("Unknown Type: " + value)
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def get(cls, key) -> Any:
 | 
			
		||||
        return cls.Values.get(key)
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user