KaraokeVideoDownloader/karaoke_downloader/config_manager.py

77 lines
2.4 KiB
Python

"""
Configuration management utilities.
Handles loading and managing application configuration.
"""
import json
from pathlib import Path
DATA_DIR = Path("data")
def load_config():
"""Load configuration from data/config.json or return defaults."""
config_file = DATA_DIR / "config.json"
if config_file.exists():
try:
with open(config_file, 'r', encoding='utf-8') as f:
return json.load(f)
except (json.JSONDecodeError, FileNotFoundError) as e:
print(f"Warning: Could not load config.json: {e}")
return get_default_config()
def get_default_config():
"""Get the default configuration."""
return {
"download_settings": {
"format": "best[height<=720][ext=mp4]/best[height<=720]/best[ext=mp4]/best",
"preferred_resolution": "720p",
"audio_format": "mp3",
"audio_quality": "0",
"subtitle_language": "en",
"subtitle_format": "srt",
"write_metadata": False,
"write_thumbnail": False,
"write_description": False,
"write_annotations": False,
"write_comments": False,
"write_subtitles": False,
"embed_metadata": False,
"add_metadata": False,
"continue_downloads": True,
"no_overwrites": True,
"ignore_errors": True,
"no_warnings": False
},
"folder_structure": {
"downloads_dir": "downloads",
"logs_dir": "logs",
"tracking_file": str(DATA_DIR / "karaoke_tracking.json")
},
"logging": {
"level": "INFO",
"format": "%(asctime)s - %(levelname)s - %(message)s",
"include_console": True,
"include_file": True
},
"yt_dlp_path": "downloader/yt-dlp.exe"
}
def save_config(config):
"""Save configuration to data/config.json."""
config_file = DATA_DIR / "config.json"
config_file.parent.mkdir(exist_ok=True)
try:
with open(config_file, 'w', encoding='utf-8') as f:
json.dump(config, f, indent=2, ensure_ascii=False)
return True
except Exception as e:
print(f"Error saving config: {e}")
return False
def update_config(updates):
"""Update configuration with new values."""
config = load_config()
config.update(updates)
return save_config(config)