-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.py
More file actions
90 lines (77 loc) · 2.88 KB
/
Copy pathsettings.py
File metadata and controls
90 lines (77 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""
settings.py
-----------
Loads and saves persistent user settings to a JSON file in the user's
AppData folder.
"""
import json
import os
from pathlib import Path
APP_NAME = "LoopClip"
# Legacy application name used by pre-LoopClip releases.
_OLD_APP_NAME = "AquariumDownloader"
DEFAULTS = {
"output_folder": str(Path.home() / "Videos" / "Downloads"),
"quality": "best", # "best" | "2160p" | "1440p" | "1080p" | "720p" | "480p" | "360p" | "audio_only"
"similarity": 98,
"auto_loop": True,
"audio_bitrate": "192", # "128" | "192" | "256" | "320"
"format_container": "best", # "best" | "mp4" | "mkv" | "webm"
"subtitle_mode": "none", # "none" | "english" | "all"
"embed_subs": False,
"speed_limit": "Unlimited", # "Unlimited" | "1 MB/s" | "2 MB/s" | "5 MB/s" | "10 MB/s" | "Custom"
"speed_limit_custom_mbps": 5,
"simultaneous_downloads": 1, # 1-4
"cookies_from_browser": "none", # "none" | "chrome" | "edge" | "firefox" | "brave"
"notify_on_complete": False,
"notify_on_failure": True,
"notify_on_queue_complete": True,
"play_sound_on_queue_complete": False,
"delete_temp_on_error": True,
"open_folder_when_finished": True,
}
def app_data_dir() -> Path:
"""The per-user folder the app stores settings, queue, history and logs
in. Shared with queue_store.py, history.py and logging_setup.py so
everything lands in one place.
"""
appdata = os.environ.get("APPDATA")
if appdata:
base = Path(appdata) / APP_NAME
else:
base = Path.home() / f".{APP_NAME.lower()}"
base.mkdir(parents=True, exist_ok=True)
return base
def _settings_path() -> Path:
return app_data_dir() / "settings.json"
def load_settings() -> dict:
path = _settings_path()
# If this is the first launch after the application rename, migrate the
# legacy settings file without ever overwriting a current LoopClip file.
if not path.exists():
old_path = Path(os.environ.get("APPDATA", Path.home())) / _OLD_APP_NAME / "settings.json"
if old_path.exists():
try:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_bytes(old_path.read_bytes())
except OSError:
# Migration is best-effort; defaults below still give the app
# a valid configuration if the old file cannot be read.
pass
data = dict(DEFAULTS)
if path.exists():
try:
with open(path, "r", encoding="utf-8") as f:
saved = json.load(f)
if isinstance(saved, dict):
data.update(saved)
except (json.JSONDecodeError, OSError):
pass
return data
def save_settings(settings: dict) -> None:
path = _settings_path()
try:
with open(path, "w", encoding="utf-8") as f:
json.dump(settings, f, indent=2)
except OSError:
pass