forked from thomas-xin/Miza
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
208 lines (195 loc) · 7.45 KB
/
main.py
File metadata and controls
208 lines (195 loc) · 7.45 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
# Loads the install_update module, which makes sure all required libraries are installed to their required versions.
from install_update import *
# Makes sure an authentication file exists.
if not os.path.exists("auth.json") or not os.path.getsize("auth.json"):
print("Authentication file not found. Generating empty template...")
d = {
"prefix": "~",
"slash_commands": False,
"webserver_address": "0.0.0.0",
"webserver_port": "",
"discord_token": "",
"owner_id": [],
"rapidapi_key": "",
"rapidapi_secret": "",
"alexflipnote_key": "",
"giphy_key": "",
}
import json
with open("auth.json", "w", encoding="utf-8") as f:
json.dump(d, f, indent=4)
input("auth.json generated. Please fill in discord_token and restart bot when done.")
raise SystemExit
import time, datetime, psutil, subprocess
ffmpeg = "./ffmpeg"
print("Verifying FFmpeg installation...")
if os.name == "nt":
import requests
try:
os.system("color")
except:
traceback.print_exc()
with requests.get("https://www.gyan.dev/ffmpeg/builds/ffmpeg-release-essentials.zip", stream=True) as resp:
try:
v = resp.url.rsplit("/", 1)[-1].split("-", 1)[-1].rsplit(".", 1)[0].split("-", 1)[0]
r = subprocess.run(ffmpeg, stderr=subprocess.PIPE)
s = r.stderr[:r.stderr.index(b"\n")].decode("utf-8", "replace").strip().lower()
if s.startswith("ffmpeg"):
s = s[6:].lstrip()
if s.startswith("version"):
s = s[7:].lstrip()
s = s.split("-", 1)[0]
if s != v:
print(f"FFmpeg version outdated ({v} > {s})")
raise FileNotFoundError
print(f"FFmpeg version {s} found; skipping installation...")
except FileNotFoundError:
print(f"Downloading FFmpeg version {v}...")
subprocess.run([sys.executable, "downloader.py", "https://www.gyan.dev/ffmpeg/builds/ffmpeg-release-essentials.zip", "ffmpeg.zip"], cwd="misc")
import zipfile, io
print("Download complete; extracting new FFmpeg installation...")
f = "misc/ffmpeg.zip"
with zipfile.ZipFile(f) as z:
names = [name for name in z.namelist() if "/bin/" in name and ".exe" in name]
for i, name in enumerate(names):
print(f"{i}/{len(names)}")
fn = name.rsplit("/", 1)[-1]
with open(fn, "wb") as y:
with z.open(name, "r") as x:
while True:
b = x.read(1048576)
if not b:
break
y.write(b)
print("FFmpeg extraction complete.")
os.remove(f)
if not os.path.exists("misc/poppler"):
print("Downloading Poppler version 21.10.0...")
os.mkdir("misc/poppler")
subprocess.run([sys.executable, "downloader.py", "https://cdn.discordapp.com/attachments/731709481863479436/899556463016554496/Poppler.zip", "poppler.zip"], cwd="misc")
import zipfile, io
print("Download complete; extracting new Poppler installation...")
f = "misc/poppler.zip"
with zipfile.ZipFile(f) as z:
z.extractall("misc/poppler")
print("Poppler extraction complete.")
os.remove(f)
else:
try:
subprocess.run(ffmpeg)
except FileNotFoundError:
print(f"Downloading FFmpeg...")
subprocess.run(("wget", "https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-amd64-static.tar.xz"))
print("Download complete; extracting new FFmpeg installation...")
os.mkdir(".temp")
subprocess.run(("tar", "-xf", "ffmpeg-release-amd64-static.tar.xz", "-C", ".temp"))
fi = os.listdir(".temp")[0]
os.rename(f".temp/{fi}/ffmpeg", "ffmpeg")
os.rename(f".temp/{fi}/ffprobe", "ffprobe")
os.rename(f".temp/{fi}/qt-faststart", "qt-faststart")
subprocess.run(("rm", "-rf", ".temp"))
# Repeatedly attempts to delete a file, waiting 1 second between attempts.
def delete(f):
while os.path.exists(f):
try:
os.remove(f)
return
except:
traceback.print_exc()
time.sleep(1)
sd = "shutdown.tmp"
rs = "restart.tmp"
hb = "heartbeat.tmp"
hb_ack = "heartbeat_ack.tmp"
delete(sd)
delete("log.txt")
# Main watchdog loop.
att = 0
while not os.path.exists(sd):
delete(rs)
delete(hb)
proc = psutil.Popen([python, "bot.py"])
start = time.time()
print("Bot started with PID \033[1;34;40m" + str(proc.pid) + "\033[1;37;40m.")
time.sleep(12)
try:
alive = True
if proc.is_running():
print("\033[1;32;40mHeartbeat started\033[1;37;40m.")
while alive:
if not os.path.exists(hb):
if os.path.exists(hb_ack):
os.rename(hb_ack, hb)
else:
with open(hb, "wb"):
pass
print(
"\033[1;36;40m Heartbeat at "
+ str(datetime.datetime.now())
+ "\033[1;37;40m."
)
for i in range(32):
time.sleep(0.25)
ld = os.listdir()
if rs in ld or sd in ld:
alive = False
break
if os.path.exists(hb):
break
for child in proc.children(recursive=True):
try:
child.terminate()
try:
child.wait(timeout=2)
except psutil.TimeoutExpired:
child.kill()
except:
traceback.print_exc()
try:
proc.terminate()
try:
proc.wait(timeout=2)
except psutil.TimeoutExpired:
proc.kill()
except psutil.NoSuchProcess:
pass
if os.path.exists(sd):
break
if time.time() - start < 60:
att += 1
else:
att = 0
if att > 16:
print("\033[1;31;40mBot crashed 16 times in a row. Waiting 5 minutes before trying again.\033[1;37;40m")
time.sleep(300)
att = 0
if alive:
print("\033[1;31;40mBot failed to acknowledge heartbeat signal, restarting...\033[1;37;40m")
else:
print("\033[1;31;40mBot sent restart signal, advancing...\033[1;37;40m")
except KeyboardInterrupt:
raise
except:
traceback.print_exc()
time.sleep(0.5)
if proc.is_running():
try:
for child in proc.children():
child.terminate()
try:
child.wait(timeout=2)
except psutil.TimeoutExpired:
child.kill()
except:
traceback.print_exc()
proc.terminate()
try:
proc.wait(timeout=2)
except psutil.TimeoutExpired:
proc.kill()
delete(sd)
delete(rs)
delete(hb)
delete(hb_ack)
print("Shutdown signal confirmed. Program will now terminate. ")
raise SystemExit