zspotify/zspotify/config.py

158 lines
5.7 KiB
Python
Raw Normal View History

2021-11-18 22:16:07 +01:00
import json
import os
import sys
from typing import Any
CONFIG_FILE_PATH = '../zs_config.json'
ROOT_PATH = 'ROOT_PATH'
ROOT_PODCAST_PATH = 'ROOT_PODCAST_PATH'
SKIP_EXISTING_FILES = 'SKIP_EXISTING_FILES'
SKIP_PREVIOUSLY_DOWNLOADED = 'SKIP_PREVIOUSLY_DOWNLOADED'
DOWNLOAD_FORMAT = 'DOWNLOAD_FORMAT'
FORCE_PREMIUM = 'FORCE_PREMIUM'
ANTI_BAN_WAIT_TIME = 'ANTI_BAN_WAIT_TIME'
OVERRIDE_AUTO_WAIT = 'OVERRIDE_AUTO_WAIT'
CHUNK_SIZE = 'CHUNK_SIZE'
SPLIT_ALBUM_DISCS = 'SPLIT_ALBUM_DISCS'
DOWNLOAD_REAL_TIME = 'DOWNLOAD_REAL_TIME'
LANGUAGE = 'LANGUAGE'
BITRATE = 'BITRATE'
2021-11-18 22:39:17 +01:00
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' },
2021-11-18 22:16:07 +01:00
}
2021-11-18 22:39:17 +01:00
class Config:
2021-11-18 22:16:07 +01:00
Values = {}
@classmethod
def load(cls, args) -> None:
app_dir = os.path.dirname(__file__)
2021-11-18 22:24:45 +01:00
config_fp = CONFIG_FILE_PATH
if args.config_location:
config_fp = args.config_location
true_config_file_path = os.path.join(app_dir, config_fp)
2021-11-18 22:16:07 +01:00
# Load config from zs_config.json
2021-11-18 22:16:07 +01:00
if not os.path.exists(true_config_file_path):
with open(true_config_file_path, 'w', encoding='utf-8') as config_file:
2021-11-18 22:39:17 +01:00
json.dump(cls.get_default_json(), config_file, indent=4)
cls.Values = cls.get_default_json()
2021-11-18 22:16:07 +01:00
else:
with open(true_config_file_path, encoding='utf-8') as 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
2021-11-18 22:39:17 +01:00
for key in CONFIG_VALUES:
if key not in cls.Values:
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')
2021-11-18 22:39:17 +01:00
@classmethod
def get_default_json(cls) -> Any:
r = {}
for key in CONFIG_VALUES:
r[key] = CONFIG_VALUES[key]['default']
2021-11-18 22:39:17 +01:00
return r
2021-11-18 22:16:07 +01:00
@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)
2021-11-18 22:16:07 +01:00
@classmethod
def get(cls, key) -> Any:
return cls.Values.get(key)
2021-11-18 22:39:17 +01:00
2021-11-18 22:16:07 +01:00
@classmethod
2021-11-18 22:39:17 +01:00
def get_root_path(cls) -> str:
2021-11-18 22:16:07 +01:00
return cls.get(ROOT_PATH)
@classmethod
2021-11-18 22:39:17 +01:00
def get_root_podcast_path(cls) -> str:
2021-11-18 22:16:07 +01:00
return cls.get(ROOT_PODCAST_PATH)
@classmethod
2021-11-18 22:39:17 +01:00
def get_skip_existing_files(cls) -> bool:
2021-11-18 22:16:07 +01:00
return cls.get(SKIP_EXISTING_FILES)
@classmethod
2021-11-18 22:39:17 +01:00
def get_skip_previously_downloaded(cls) -> bool:
2021-11-18 22:16:07 +01:00
return cls.get(SKIP_PREVIOUSLY_DOWNLOADED)
@classmethod
2021-11-18 22:39:17 +01:00
def get_split_album_discs(cls) -> bool:
2021-11-18 22:16:07 +01:00
return cls.get(SPLIT_ALBUM_DISCS)
@classmethod
2021-11-18 22:39:17 +01:00
def get_chunk_size(cls) -> int():
2021-11-18 22:16:07 +01:00
return cls.get(CHUNK_SIZE)
@classmethod
2021-11-18 22:39:17 +01:00
def get_override_auto_wait(cls) -> bool:
2021-11-18 22:16:07 +01:00
return cls.get(OVERRIDE_AUTO_WAIT)
@classmethod
2021-11-18 22:39:17 +01:00
def get_force_premium(cls) -> bool:
2021-11-18 22:16:07 +01:00
return cls.get(FORCE_PREMIUM)
2021-11-18 22:39:17 +01:00
2021-11-18 22:16:07 +01:00
@classmethod
2021-11-18 22:39:17 +01:00
def get_download_format(cls) -> str:
2021-11-18 22:16:07 +01:00
return cls.get(DOWNLOAD_FORMAT)
2021-11-18 22:39:17 +01:00
2021-11-18 22:16:07 +01:00
@classmethod
2021-11-18 22:39:17 +01:00
def get_anti_ban_wait_time(cls) -> int:
2021-11-18 22:16:07 +01:00
return cls.get(ANTI_BAN_WAIT_TIME)
2021-11-18 22:39:17 +01:00
2021-11-18 22:16:07 +01:00
@classmethod
2021-11-18 22:39:17 +01:00
def get_language(cls) -> str:
2021-11-18 22:16:07 +01:00
return cls.get(LANGUAGE)
@classmethod
2021-11-18 22:39:17 +01:00
def get_download_real_time(cls) -> bool:
2021-11-18 22:16:07 +01:00
return cls.get(DOWNLOAD_REAL_TIME)
2021-11-18 22:39:17 +01:00
2021-11-18 22:16:07 +01:00
@classmethod
2021-11-18 22:39:17 +01:00
def get_bitrate(cls) -> str:
2021-11-18 22:16:07 +01:00
return cls.get(BITRATE)