Global config location

This commit is contained in:
logykk 2022-02-04 22:11:49 +13:00
parent 70da426463
commit 3d50d8f141
7 changed files with 39 additions and 47 deletions

View file

@ -18,7 +18,7 @@ def main():
help='Suppress the splash screen when loading.')
parser.add_argument('--config-location',
type=str,
help='Specify the zconfig.json location')
help='Specify the json config location')
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('urls',
type=str,

View file

@ -23,10 +23,10 @@ def client(args) -> None:
Printer.print(PrintChannel.SPLASH, splash())
if Zotify.check_premium():
Printer.print(PrintChannel.SPLASH, '[ DETECTED PREMIUM ACCOUNT - USING VERY_HIGH QUALITY ]\n\n')
Printer.print(PrintChannel.WARNINGS, '[ DETECTED PREMIUM ACCOUNT - USING VERY_HIGH QUALITY ]\n\n')
Zotify.DOWNLOAD_QUALITY = AudioQuality.VERY_HIGH
else:
Printer.print(PrintChannel.SPLASH, '[ DETECTED FREE ACCOUNT - USING HIGH QUALITY ]\n\n')
Printer.print(PrintChannel.WARNINGS, '[ DETECTED FREE ACCOUNT - USING HIGH QUALITY ]\n\n')
Zotify.DOWNLOAD_QUALITY = AudioQuality.HIGH
if args.download:
@ -67,8 +67,7 @@ def download_from_urls(urls: list[str]) -> bool:
download = False
for spotify_url in urls:
track_id, album_id, playlist_id, episode_id, show_id, artist_id = regex_input_for_urls(
spotify_url)
track_id, album_id, playlist_id, episode_id, show_id, artist_id = regex_input_for_urls(spotify_url)
if track_id is not None:
download = True

View file

@ -1,9 +1,8 @@
import json
# import os
import sys
from pathlib import Path, PurePath
from typing import Any
CONFIG_FILE_PATH = './zconfig.json'
ROOT_PATH = 'ROOT_PATH'
ROOT_PODCAST_PATH = 'ROOT_PODCAST_PATH'
@ -77,28 +76,28 @@ class Config:
@classmethod
def load(cls, args) -> None:
#app_dir = PurePath(__file__).parent
app_dir = Path.cwd()
config_fp = CONFIG_FILE_PATH
system_paths = {
'win32': Path.home() / 'AppData/Roaming/Zotify',
'linux': Path.home() / '.config/zotify',
'darwin': Path.home() / 'Library/Application Support/Zotify'
}
config_fp = system_paths[sys.platform] / 'config.json'
if args.config_location:
config_fp = args.config_location
true_config_file_path = PurePath(app_dir).joinpath(config_fp)
true_config_file_path = Path(config_fp).expanduser()
# Load config from zconfig.json
Path(PurePath(true_config_file_path).parent).mkdir(parents=True, exist_ok=True)
if not Path(true_config_file_path).exists():
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:
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])
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
@ -144,11 +143,13 @@ class Config:
@classmethod
def get_root_path(cls) -> str:
return PurePath(Path.cwd()).joinpath(cls.get(ROOT_PATH))
# return PurePath(Path.cwd()).joinpath(cls.get(ROOT_PATH))
return PurePath(Path(cls.get(ROOT_PATH)).expanduser())
@classmethod
def get_root_podcast_path(cls) -> str:
return PurePath(Path.cwd()).joinpath(cls.get(ROOT_PODCAST_PATH))
# return PurePath(Path.cwd()).joinpath(cls.get(ROOT_PODCAST_PATH))
return PurePath(Path(cls.get(ROOT_PODCAST_PATH)).expanduser())
@classmethod
def get_skip_existing_files(cls) -> bool:
@ -223,36 +224,26 @@ class Config:
return v
if mode == 'playlist':
if cls.get_split_album_discs():
# split = os.path.split(OUTPUT_DEFAULT_PLAYLIST)
# return os.path.join(split[0], 'Disc {disc_number}', split[0])
split = PurePath(OUTPUT_DEFAULT_PLAYLIST).parent
return PurePath(split).joinpath('Disc {disc_number}').joinpath(split)
return OUTPUT_DEFAULT_PLAYLIST
if mode == 'extplaylist':
if cls.get_split_album_discs():
# split = os.path.split(OUTPUT_DEFAULT_PLAYLIST_EXT)
# return os.path.join(split[0], 'Disc {disc_number}', split[0])
split = PurePath(OUTPUT_DEFAULT_PLAYLIST_EXT).parent
return PurePath(split).joinpath('Disc {disc_number}').joinpath(split)
return OUTPUT_DEFAULT_PLAYLIST_EXT
if mode == 'liked':
if cls.get_split_album_discs():
# split = os.path.split(OUTPUT_DEFAULT_LIKED_SONGS)
# return os.path.join(split[0], 'Disc {disc_number}', split[0])
split = PurePath(OUTPUT_DEFAULT_LIKED_SONGS).parent
return PurePath(split).joinpath('Disc {disc_number}').joinpath(split)
return OUTPUT_DEFAULT_LIKED_SONGS
if mode == 'single':
if cls.get_split_album_discs():
# split = os.path.split(OUTPUT_DEFAULT_SINGLE)
# return os.path.join(split[0], 'Disc {disc_number}', split[0])
split = PurePath(OUTPUT_DEFAULT_SINGLE).parent
return PurePath(split).joinpath('Disc {disc_number}').joinpath(split)
return OUTPUT_DEFAULT_SINGLE
if mode == 'album':
if cls.get_split_album_discs():
# split = os.path.split(OUTPUT_DEFAULT_ALBUM)
# return os.path.join(split[0], 'Disc {disc_number}', split[0])
split = PurePath(OUTPUT_DEFAULT_ALBUM).parent
return PurePath(split).joinpath('Disc {disc_number}').joinpath(split)
return OUTPUT_DEFAULT_ALBUM

View file

@ -7,7 +7,7 @@ from librespot.metadata import EpisodeId
from zotify.const import ERROR, ID, ITEMS, NAME, SHOW, DURATION_MS
from zotify.termoutput import PrintChannel, Printer
from zotify.utils import create_download_directory, fix_filename
from zotify.utils import create_download_directory, fix_filename, convert_audio_format
from zotify.zotify import Zotify
from zotify.loader import Loader

View file

@ -23,7 +23,6 @@ class MusicFormat(str, Enum):
def create_download_directory(download_path: str) -> None:
""" Create directory and add a hidden file with song ids """
# os.makedirs(download_path, exist_ok=True)
Path(download_path).mkdir(parents=True, exist_ok=True)
# add hidden file with song ids
@ -282,5 +281,3 @@ def fmt_seconds(secs: float) -> str:
return f'{m}'.zfill(2) + ':' + f'{s}'.zfill(2)
else:
return f'{h}'.zfill(2) + ':' + f'{m}'.zfill(2) + ':' + f'{s}'.zfill(2)