-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFireAddOns.py
More file actions
320 lines (262 loc) · 11.8 KB
/
FireAddOns.py
File metadata and controls
320 lines (262 loc) · 11.8 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import importlib, sys, re, os
import shutil, psutil, filecmp
import gi
gi.require_version('Gtk', '4.0')
from gi.repository import GLib, Gtk, Gdk
from SaveManager import DownloadDatabase
def lazy_import(name):
spec = importlib.util.find_spec(name)
if spec is None or spec.loader is None:
raise ImportError(f"Module '{name}' could not be found for lazy import.")
loader = importlib.util.LazyLoader(spec.loader)
spec.loader = loader
module = importlib.util.module_from_spec(spec)
sys.modules[name] = module
loader.exec_module(module)
return module
class _FileManager():
def __init__(self):
if os.name == 'nt':
base_config = os.getenv('APPDATA', os.path.expanduser('~'))
base_data = os.getenv('LOCALAPPDATA', base_config)
self.config_dir = os.path.join(base_data, "flameget")
self.data_dir = os.path.join(base_data, "flameget")
else:
self.config_dir = os.path.join(GLib.get_user_config_dir(), "flameget")
self.data_dir = os.path.join(GLib.get_user_data_dir(), "flameget")
os.makedirs(self.config_dir, exist_ok=True)
os.makedirs(self.data_dir, exist_ok=True)
self.db_file = os.path.join(self.data_dir, "downloads.db")
self.db = DownloadDatabase(db_name=self.db_file)
self.is_compiled = getattr(sys, 'frozen', False) or "__compiled__" in globals()
if self.is_compiled:
self.current_exe = sys.executable
self.install_dir = os.path.dirname(self.current_exe)
ext = ""
else:
self.current_exe = sys.executable
self.install_dir = os.path.dirname(os.path.abspath(__file__))
ext = ".py"
self.downloader_script_path = os.path.join(self.install_dir, f"downloader{ext}")
self.browser_context_menu_handler_script_path = os.path.join(self.install_dir, f"browser_context_menu_handler{ext}")
self.server_script_path = os.path.join(self.install_dir, f"server{ext}")
self.binaries_path = os.path.join(self.install_dir, "binaries")
if self.is_compiled:
self.tray_script_path = os.path.join(self.binaries_path, "tray.exe" if os.name =="nt" else "tray.bin")
else:
self.tray_script_path = os.path.join(self.install_dir, "tray.py")
self.aria2c_path = "aria2c" if os.name != "nt" else os.path.join(self.binaries_path, "aria2c.exe")
self.rustypipe_botguard_path = os.path.join(self.binaries_path, f'rustypipe-botguard{".exe" if os.name == "nt" else ""}')
os.environ["PATH"] = os.path.abspath(self.binaries_path) + os.pathsep + os.environ.get("PATH", "")
self.icons_dir = os.path.join(self.install_dir, "icons")
self.ffmpeg_path = os.path.join(self.binaries_path, "ffmpeg.exe") if os.name == "nt" else shutil.which("ffmpeg")
display = Gdk.Display.get_default()
icon_theme = Gtk.IconTheme.get_for_display(display)
icon_theme.add_search_path(self.icons_dir)
editable_files = ["translations.json", "dark_style.css", "light_style.css", "custom_style.css"]
for filename in editable_files:
user_path = os.path.join(self.config_dir, filename)
if self.is_compiled:
system_path = os.path.join(self.install_dir, "_internal", filename)
else:
system_path = os.path.join(self.install_dir, filename)
if not os.path.exists(user_path):
if os.path.exists(system_path):
try:
shutil.copy2(system_path, user_path)
print(f"Copied default {filename} to user config.")
except Exception as e:
print(f"Failed to copy {filename}: {e}")
else:
open(user_path, 'a').close()
print(f"Made empty {filename}")
else:
if not os.path.exists(system_path):
continue
if filename.endswith(".json"):
try:
if filecmp.cmp(system_path, user_path, shallow=False):
continue
with open(system_path, 'r', encoding='utf-8') as sf:
system_data = json.load(sf)
with open(user_path, 'r', encoding='utf-8') as uf:
try:
user_data = json.load(uf)
except json.JSONDecodeError:
user_data = {}
updated = False
for key, value in system_data.items():
if key not in user_data:
user_data[key] = value
updated = True
if updated:
with open(user_path, 'w', encoding='utf-8') as uf:
json.dump(user_data, uf, indent=4, ensure_ascii=False)
print(f"Updated {filename} with new translation keys.")
except Exception as e:
print(f"Failed to merge {filename}: {e}")
elif filename in ["dark_style.css", "light_style.css"]:
try:
if not filecmp.cmp(system_path, user_path, shallow=False):
shutil.copy2(system_path, user_path)
print(f"Updated {filename} with new system styles.")
except Exception as e:
print(f"Failed to update {filename}: {e}")
FireFiles = _FileManager()
class UNITS():
def get_temp_dir():
if os.name == 'nt':
base_data = os.getenv('LOCALAPPDATA', os.path.expanduser('~'))
RUNTIME_DIR = os.path.join(base_data, "flameget", "run")
os.makedirs(RUNTIME_DIR, exist_ok=True)
return RUNTIME_DIR
else:
return os.environ.get("XDG_RUNTIME_DIR", "/tmp")
SIZE_RE = re.compile(r"/([0-9.]+)([KMG]i?)B", re.I)
RUNTIME_DIR = get_temp_dir()
MULT = {
"b": 1, "byte": 1, "bytes": 1,
"k": 1000, "kb": 1000,
"ki": 1024, "kib": 1024,
"m": 1000**2, "mb": 1000**2,
"mi": 1024**2, "mib": 1024**2,
"g": 1000**3, "gb": 1000**3,
"gi": 1024**3, "gib": 1024**3,
"t": 1000**4, "tb": 1000**4,
"ti": 1024**4, "tib": 1024**4,
# WHO THE FUCK HAS THIS AMOUNT OF DATA DAYUUM
"p": 1000**5, "pb": 1000**5,
"pi": 1024**5, "pib": 1024**5,
}
COMPRESSED = {
".zip", ".rar", ".7z", ".tar", ".gz", ".bz2", ".xz",
".tgz", ".tbz2", ".txz", ".zst", ".iso"
}
PROGRAMS = {
".exe", ".msi", ".apk", ".appimage", ".deb", ".rpm",
".run", ".bin", ".sh"
}
VIDEOS = {
".mp4", ".mkv", ".avi", ".mov", ".wmv", ".flv",
".webm", ".mpeg", ".mpg", ".m4v"
}
MUSIC = {
".mp3", ".flac", ".wav", ".ogg", ".aac", ".m4a",
".opus", ".wma"
}
PICTURES = {
".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp",
".tiff", ".svg", ".avif"
}
DOCUMENTS = {
".pdf", ".doc", ".docx", ".odt", ".rtf", ".txt",
".md", ".ppt", ".pptx", ".xls", ".xlsx", ".ods",
".csv", ".epub"
}
SUPPORTED_SITES = {
"youtube.com", "youtu.be",
"twitch.tv", "tiktok.com",
"instagram.com", "facebook.com", "fb.watch",
"twitter.com", "x.com",
"vimeo.com", "dailymotion.com",
"soundcloud.com", "mixcloud.com",
"reddit.com", "pinterest.com",
"bilibili.com", "vk.com",
"odysee.com", "rumble.com",
"streamable.com"
}
def categorize_filename(filename, is_torrent=False):
ext = os.path.splitext(filename.lower())[1]
if is_torrent:
return "Torrent"
if ext in UNITS.COMPRESSED:
return "Compressed"
if ext in UNITS.PROGRAMS:
return "Programs"
if ext in UNITS.VIDEOS:
return "Videos"
if ext in UNITS.MUSIC:
return "Music"
if ext in UNITS.PICTURES:
return "Pictures"
if ext in UNITS.DOCUMENTS:
return "Documents"
return "Documents"
#for the downloader
def parse_size(file_size_in_bytes):
return (
f"{file_size_in_bytes} B" if file_size_in_bytes < 1024 else
f"{file_size_in_bytes / 1024:.2f} KB" if file_size_in_bytes < 1024**2 else
f"{file_size_in_bytes / (1024 ** 2):.2f} MB" if file_size_in_bytes < 1024**3 else
f"{file_size_in_bytes / (1024 ** 3):.2f} GB" if file_size_in_bytes < 1024**4 else
f"{file_size_in_bytes / (1024 ** 4):.2f} TB"
)
def range_parse_size(val, unit):
unit = unit.lower()
if not unit.endswith("b"):
unit += "b"
return int(float(val) * UNITS.MULT[unit])
def is_pid_alive(pid: int) -> bool:
return psutil.pid_exists(pid)
def set_titlebar_theme(window_title, theme_str="Dark"):
if os.name != 'nt':
return
import ctypes
try:
hwnd = ctypes.windll.user32.FindWindowW(None, window_title)
if hwnd:
DWMWA_USE_IMMERSIVE_DARK_MODE = 20
# If dark_mode is True, send 1. If False, send 0.
dark_mode = 1 if theme_str == "Dark" else 0
set_theme = ctypes.c_int(dark_mode)
ctypes.windll.dwmapi.DwmSetWindowAttribute(
hwnd,
DWMWA_USE_IMMERSIVE_DARK_MODE,
ctypes.byref(set_theme),
ctypes.sizeof(set_theme)
)
print(f"Successfully set Windows title bar to {theme_str} mode.")
else:
print("Could not find window to apply title bar theme.")
except Exception as e:
print(f"DWM API failed: {e}")
import os
import ctypes
from ctypes import wintypes
def force_center_dialog(dialog_title, parent_title=None):
if os.name != 'nt':
return False
user32 = ctypes.windll.user32
user32.FindWindowW.restype = wintypes.HWND
user32.FindWindowW.argtypes = [wintypes.LPCWSTR, wintypes.LPCWSTR]
user32.GetWindowRect.restype = wintypes.BOOL
user32.GetWindowRect.argtypes = [wintypes.HWND, ctypes.POINTER(wintypes.RECT)]
user32.SetWindowPos.restype = wintypes.BOOL
user32.SetWindowPos.argtypes = [wintypes.HWND, wintypes.HWND, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_uint]
hwnd_dialog = user32.FindWindowW(None, dialog_title)
if not hwnd_dialog:
print(f"DEBUG: Could not find window with title '{dialog_title}'")
return False
rect_dlg = wintypes.RECT()
user32.GetWindowRect(hwnd_dialog, ctypes.byref(rect_dlg))
dlg_width = rect_dlg.right - rect_dlg.left
dlg_height = rect_dlg.bottom - rect_dlg.top
hwnd_main = user32.FindWindowW(None, parent_title) if parent_title else None
if hwnd_main:
rect_main = wintypes.RECT()
user32.GetWindowRect(hwnd_main, ctypes.byref(rect_main))
main_width = rect_main.right - rect_main.left
main_height = rect_main.bottom - rect_main.top
x = rect_main.left + (main_width - dlg_width) // 2
y = rect_main.top + (main_height - dlg_height) // 2
else:
SM_CXSCREEN = 0
SM_CYSCREEN = 1
screen_width = user32.GetSystemMetrics(SM_CXSCREEN)
screen_height = user32.GetSystemMetrics(SM_CYSCREEN)
x = (screen_width - dlg_width) // 2
y = (screen_height - dlg_height) // 2
HWND_TOPMOST = ctypes.c_void_p(-1)
SWP_NOSIZE = 0x0001
user32.SetWindowPos(hwnd_dialog, HWND_TOPMOST, x, y, 0, 0, SWP_NOSIZE)
return False