musicdlp/backend/config.py
DarkCat09 9071017dbf
Refactor: unify YDL object and proxy cfg field
This would allow to add sites much simplier
or even drop the ydl_fn_keys.
Main purpose of the refactoring is a code cleanup.
2024-05-28 13:21:23 +04:00

46 lines
1.3 KiB
Python

import os
from pathlib import Path
class Config:
def __init__(self) -> None:
self.host = os.getenv('HOST') or '0.0.0.0'
self.port = int(os.getenv('PORT') or 4009)
# This directory will be searched for <site>.txt (e.g. yandex.txt)
# Cookies are in Netscape CSV format, see yt-dlp docs
self.cookies_dir = Path(os.getenv('COOKIES_DIR') or 'cookies')
self.tmpl = os.path.join(
# 1. `artists.0` instead of `artist`, because the latter can contain "feat. ..."
# 2. as for %(field)S, see https://git.dc09.ru/DarkCat09/musicdlp/issues/3#issuecomment-210
os.getenv('ALBUM_PATH_TMPL') or 'music/%(artists.0)S/%(album)S',
os.getenv('TRACK_FILE_TMPL') or '%(track)S.%(ext)s',
)
# `or None` -- defaults to None also if PROXY is an empty string
self.proxy = os.getenv('PROXY') or None
self.save_lyrics = _parse_bool(os.getenv('SAVE_LYRICS'), True)
self.save_cover = _parse_bool(os.getenv('SAVE_COVER'), True)
_config: Config | None = None
def get() -> Config:
global _config
if _config is None:
_config = Config()
return _config
def _parse_bool(val: str | None, default: bool) -> bool:
if val is None:
return default
if val in {'false', 'off', 'no', '0'}:
return False
return bool(val)