-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathytsage_utils.py
85 lines (81 loc) · 3.58 KB
/
ytsage_utils.py
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
import sys
import os
import json
from pathlib import Path
import subprocess
def check_ffmpeg():
try:
subprocess.run(['ffmpeg', '-version'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True)
return True
except (subprocess.SubprocessError, FileNotFoundError):
return False
def get_yt_dlp_path():
"""Get the appropriate yt-dlp path based on platform and deployment method"""
try:
if getattr(sys, 'frozen', False):
if sys.platform == 'darwin':
# For macOS .app bundle
if 'Contents/MacOS' in sys.executable:
# Inside .app bundle
return os.path.join(os.path.dirname(sys.executable), 'yt-dlp')
else:
# Fallback to user's home directory for macOS
base_path = os.path.expanduser('~/Library/Application Support/YTSage')
os.makedirs(base_path, exist_ok=True)
return os.path.join(base_path, 'yt-dlp')
elif sys.platform == 'win32':
# For Windows executable
app_data = os.getenv('APPDATA')
if app_data:
base_path = os.path.join(app_data, 'YTSage')
else:
base_path = os.path.dirname(sys.executable)
os.makedirs(base_path, exist_ok=True)
return os.path.join(base_path, 'yt-dlp.exe')
else:
# For Linux AppImage or binary
if 'APPIMAGE' in os.environ:
# Inside AppImage
xdg_data = os.getenv('XDG_DATA_HOME', os.path.expanduser('~/.local/share'))
base_path = os.path.join(xdg_data, 'YTSage')
else:
base_path = os.path.dirname(sys.executable)
os.makedirs(base_path, exist_ok=True)
return os.path.join(base_path, 'yt-dlp')
else:
# For development/script mode
return os.path.join(os.path.dirname(os.path.abspath(__file__)),
'yt-dlp.exe' if sys.platform == 'win32' else 'yt-dlp')
except Exception as e:
print(f"Error determining yt-dlp path: {e}")
# Fallback to current directory
return os.path.join(os.getcwd(), 'yt-dlp.exe' if sys.platform == 'win32' else 'yt-dlp')
def load_saved_path(main_window_instance): # Pass the main window instance
config_file = main_window_instance.config_file # Access config_file via instance
try:
if config_file.exists():
with open(config_file, 'r') as f:
config = json.load(f)
saved_path = config.get('download_path', '')
if os.path.exists(saved_path):
main_window_instance.last_path = saved_path # Access last_path via instance
else:
main_window_instance.last_path = str(Path.home() / 'Downloads')
else:
main_window_instance.last_path = str(Path.home() / 'Downloads')
except Exception as e:
print(f"Error loading saved settings: {e}")
main_window_instance.last_path = str(Path.home() / 'Downloads')
def save_path(main_window_instance, path): # Pass main window instance
config_file = main_window_instance.config_file # Access config_file via instance
try:
config = {
'download_path': path
}
with open(config_file, 'w') as f:
json.dump(config, f)
except Exception as e:
print(f"Error saving settings: {e}")