-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharGUIments-console.py
More file actions
119 lines (96 loc) · 3.64 KB
/
Copy patharGUIments-console.py
File metadata and controls
119 lines (96 loc) · 3.64 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import os
import subprocess
import json
import configparser
import sys
import shlex
if hasattr(sys, '_MEIPASS'):
BASE_DIR = sys._MEIPASS
else:
BASE_DIR = os.path.abspath(".")
PROFILE_FILE = "profiles.json"
SETTINGS_FILE = "settings.ini"
def load_settings():
# config = configparser.ConfigParser()
config = configparser.ConfigParser(interpolation=None)
if not os.path.exists(SETTINGS_FILE):
config['DEFAULT'] = {
'software_path': 'yt-dlp',
'output_flag': '--output',
'output_folder': '',
'filename_template': '%(title)s.%(ext)s',
# 'use_custom_output': 'False'
}
with open(SETTINGS_FILE, 'w') as f:
config.write(f)
else:
config.read(SETTINGS_FILE)
return config
def load_profiles():
if os.path.exists(PROFILE_FILE):
with open(PROFILE_FILE, 'r') as f:
return json.load(f)
return {}
profiles = load_profiles()
settings = load_settings()
def build_command(shortname, user_args):
profile = profiles.get(shortname)
if not profile:
raise ValueError("Profile not found.")
command_template = profile['command_template']
try:
args = shlex.split(command_template.format(*user_args))
except IndexError as e:
raise ValueError(f"Missing arguments: {e}")
path_mode = profile.get("path_mode", "default")
if path_mode == "default":
software_path = settings['DEFAULT'].get('software_path')
elif path_mode == "custom":
software_path = profile.get("program_path")
if not software_path:
raise ValueError("Software path is not defined in settings or in profile.")
command = [software_path] + args
flag = None
exportmode = profile.get("export_output_mode")
if exportmode == "default":
flag = settings['DEFAULT'].get('output_flag')
elif exportmode == "custom":
flag = profile.get("custom_output_flag")
folder = None
mode = profile.get("export_mode", "default")
if mode == "default":
folder = settings['DEFAULT'].get('output_folder')
elif mode == "software":
folder = ''
elif mode == "custom":
folder = profile.get("custom_output_folder", "")
template = None
template_name = profile.get("filename_mode")
if template_name == "default":
template = settings['DEFAULT'].get('filename_template')
elif template_name == "custom":
template = profile.get("custom_filename_template")
if exportmode != "disable":
if folder == "":
command += [flag, f"{template}"]
else:
command += [flag, f"{folder}/{template}"]
return command
if __name__ == "__main__":
# ========= COMMAND-LINE MODE ===========
if len(sys.argv) > 1:
shortname = sys.argv[1]
args = sys.argv[2:]
try:
command = build_command(shortname, args)
# CLI mode uses subprocess.run, which inherits terminal (with colors)
subprocess.run(command)
except KeyboardInterrupt:
# User pressed Ctrl+C. We can exit gracefully without an error message.
# The newline character \n is to ensure the message appears on a new line after the ^C.
print("\nProcess interrupted by user.")
except Exception as e:
print(f"[ERROR] {e}")
sys.exit(0)
else:
print("Provide a profile shortname as first argument if running via command line.")