forked from axewater/sharewarez
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
138 lines (116 loc) · 5.62 KB
/
app.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
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
# /app.py
import time
from modules import create_app
from modules.updateschema import DatabaseManager
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import threading
from flask import current_app
import os
import zipfile
import shutil
app = create_app()
def initialize_library_folders():
"""Initialize the required folders and theme files for the application."""
library_path = os.path.join('modules', 'static', 'library')
themes_path = os.path.join(library_path, 'themes')
images_path = os.path.join(library_path, 'images')
zips_path = os.path.join(library_path, 'zips')
# Check if default theme exists
if not os.path.exists(os.path.join(themes_path, 'default', 'theme.json')):
print("Default theme not found. Initializing from themes.zip...")
# Extract themes.zip
themes_zip = os.path.join('modules', 'setup', 'themes.zip')
if os.path.exists(themes_zip):
with zipfile.ZipFile(themes_zip, 'r') as zip_ref:
zip_ref.extractall(library_path)
print("Themes extracted successfully")
else:
print("Warning: themes.zip not found in modules/setup/")
# Create images folder if it doesn't exist
if not os.path.exists(images_path):
os.makedirs(images_path)
print("Created images folder")
# Create zips folder if it doesn't exist
if not os.path.exists(zips_path):
os.makedirs(zips_path)
print("Created zips folder")
# Initialize library folders before creating the app
initialize_library_folders()
# Initialize and run the database schema update
db_manager = DatabaseManager()
db_manager.add_column_if_not_exists()
global last_trigger_time
last_trigger_time = time.time()
global last_modified
last_modified = ""
shutdown_event = threading.Event()
class MyHandler(FileSystemEventHandler):
global last_trigger_time
last_trigger_time = time.time()
def on_created(self, event):
global last_modified
last_modified = event.src_path
if event.src_path.find('~') == -1:
with app.app_context():
allowed_ext = current_app.config['ALLOWED_FILE_TYPES'] # List of allowed extensions.
ignore_ext = current_app.config['MONITOR_IGNORE_EXT'] # List of extensions to ignore.
if os.name == "nt":
file_name = event.src_path.split('\\')[-1]
else:
file_name = event.src_path.split('/')[-1]
if not event.is_directory:
file_ext = file_name.split('.')[-1]
else:
file_ext = "None"
if (file_ext not in ignore_ext and file_ext in allowed_ext) or file_ext == "None":
from modules.utilities import discord_update
from modules.models import GlobalSettings
settings = GlobalSettings.query.first()
if settings.enable_game_updates or settings.enable_game_extras:
print(f"Event: {event.src_path} was {event.event_type} - Processing File Name: {file_name} with File Extension {file_ext}")
time.sleep(5)
discord_update(event.src_path, event.event_type)
def on_modified(self, event):
global last_trigger_time
global last_modified
current_time = time.time()
if not event.is_directory and last_modified != event.src_path:
if event.src_path.find('~') == -1 and (current_time - last_trigger_time) > 1:
last_modified = event.src_path
last_trigger_time = current_time
with app.app_context():
allowed_ext = current_app.config['ALLOWED_FILE_TYPES'] # List of allowed extensions.
ignore_ext = current_app.config['MONITOR_IGNORE_EXT'] # List of extensions to ignore.
file_name = event.src_path.split('/')[-1]
file_ext = file_name.split('.')[-1]
if file_ext not in ignore_ext and file_ext in allowed_ext:
from modules.utilities import discord_update
from modules.models import GlobalSettings
settings = GlobalSettings.query.first()
if settings.enable_main_game_updates:
print(f"Event: {event.src_path} was {event.event_type} - Processing File Name: {file_name} with File Extension {file_ext}")
time.sleep(5)
discord_update(event.src_path, event.event_type)
def watch_directory(path):
observer = Observer()
observer.schedule(MyHandler(), path, recursive=True)
observer.start()
while not shutdown_event.is_set():
time.sleep(1)
observer.stop()
observer.join()
if __name__ == "__main__":
with app.app_context():
config_path = current_app.config['MONITOR_PATHS'] # Replace with the directory you want to watch
paths = config_path
for p in paths:
targetPath = str(p)
# configure a watchdog thread
thread = threading.Thread(target=watch_directory, name="Watchdog", daemon=True, args=(targetPath,))
# start the watchdog thread
try:
thread.start()
except KeyboardInterrupt:
shutdown_event.set()
app.run(host="0.0.0.0", debug=True, use_reloader=False, port=5001)