-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
307 lines (228 loc) · 8.78 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
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
import configparser
import json
import os
import smtplib
from datetime import datetime
from flask import Flask, redirect, render_template, request, send_file, url_for, flash
from werkzeug.utils import secure_filename
config = configparser.ConfigParser()
config.read("/root/saberfilmsapp/config/config.ini")
HOST = config.get("Server", "ip")
PORT = config.get("Server", "port")
ACCESSLOG = config.get("Server", "logfile")
PASSWORDS = config.get("Server", "admin_password").split(',')
UPLOAD_FOLDER = '/root/saberfilmsapp/bts/'
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
app = Flask(__name__)
def get_importance(file: str) -> int:
with open("people/" + file, "r") as f:
print(f"{file} - {json.loads(f.read())['importance']}")
return json.loads(f.read())["importance"]
def sort_files(files):
return files # need to implement sorting here
# return sorted(files, reverse=True)
def password_prompt(message):
return f"""
<form action="/admin" method='post'>
<label for="password">{message}:</label><br>
<input type="password" id="password" name="password" value=""><br>
<input type="submit" value="Submit">
</form>"""
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route("/")
@app.route("/index")
@app.route("/home")
def index():
return render_template("index.html")
@app.route("/about")
def about():
return render_template("about.html")
@app.route("/people")
def cast():
people = []
for _, _, files in os.walk("people"):
for file in sort_files(files):
with open("people/" + file, "r") as f:
people.append(json.load(f))
return render_template("people.html", people=people)
@app.route("/person/<name>")
def person(name):
picture = False
data = {"Name": "ERROR"}
try:
with open(f"people/{name}.json", "r") as f:
data = json.load(f)
if data.get("Picture") != "":
picture = True
except FileNotFoundError:
data = {
"Name": "ERROR",
"About": "This Person does not exist.",
"Roles": ["Error"],
}
return render_template("person.html", person=data, picture=picture)
@app.route("/bts")
def behind_the_scenes():
images = []
for _, _, files in os.walk("bts"):
for file in sort_files(files):
images.append({"filename": file, "url": f"https://thelostarchive.cf/bts/{file}"})
return render_template("bts.html",images=images)
@app.route("/bts/<name>")
def btsdownloads(name):
return send_file(f'/root/saberfilmsapp/bts/{name}')
@app.route("/contact")
def contact():
return render_template("contact.html")
@app.route("/submit", methods=["GET", "POST"])
def submit_user():
if request.method == "POST":
data = request.form
if data["password"] not in PASSWORDS:
return "You do not have permission to do that!"
str_json = json.dumps(data)
data = json.loads(str_json)
data["link"] = "".join(data["Name"].split(" ")).lower()
data["socials"] = data["socials"].split(",")
data["Roles"] = data["Roles"].split(",")
data["password"] = ""
if os.path.exists(f"people/{data['link']}.json"):
return "That user already exists."
with open(f"people/{data['link']}.json", "w+") as file:
json.dump(data, file)
return redirect("/people")
if request.method == "GET":
return render_template("add_user.html")
@app.route('/update', methods=['GET', 'POST'])
@app.route("/update/<name>", methods=["GET", "POST"])
def update_user(name=None):
if request.method == "POST":
data = request.form
if data["password"] not in PASSWORDS:
return "You do not have permission to do that!"
str_json = json.dumps(data)
data = json.loads(str_json)
data["link"] = "".join(data["Name"].split(" ")).lower()
if data.get('socials') is not None:
data["socials"] = data["socials"].split(",")
data["Roles"] = data["Roles"].split(",")
data["password"] = ""
with open(f"people/{data['link']}.json", "w") as file:
json.dump(data, file)
return redirect("/people")
if request.method == "GET":
try:
with open(f"people/{name}.json", "r") as f:
data = json.load(f)
if data.get("Picture") != "":
picture = True
except FileNotFoundError:
data = {
"Name": "ERROR",
"About": "This Person does not exist.",
"Roles": ["Error"],
}
return render_template('edituser.html', person=data)
@app.route("/email", methods=["GET", "POST"])
def send_email():
if request.method == "POST":
data = json.loads(json.dumps(request.form))
ip_address = request.remote_addr
return "Error: not finished :( sorry"
@app.route("/getinvolved", methods=["GET", "POST"])
def get_involved():
if request.method == "GET":
return render_template("getinvolved.html")
if request.method == "POST":
pass
@app.route("/sponsors")
def sponsors():
sponsors = []
for _, _, files in os.walk("sponsors"):
for file in sort_files(files):
with open("sponsors/" + file, "r") as f:
sponsors.append(json.load(f))
return render_template("sponsors.html", sponsors=sponsors)
@app.route("/picture/<name>")
def get_picture(name):
return send_file(f"/root/saberfilmsapp/static/images/{name}",
attachment_filename="img.jpg")
@app.route("/downloads/<name>")
def downloads(name):
if "pdf" in name:
return send_file(f"/root/saberfilmsapp/downloads/{name}")
if "jpg" in name:
return send_file(f"/root/saberfilmsapp/downloads/{name}",
attachment_filename="TLA_Image.jpg")
if "ttf" in name:
return send_file(f"/root/saberfilmsapp/downloads/{name}", as_attachment=False)
return send_file(f"/root/saberfilmsapp/downloads/{name}",
as_attachment=True)
@app.route('/plot')
def plot():
return render_template("plot.html")
@app.route("/admin", methods=["GET", "POST"])
def admin():
if request.method == "GET":
return password_prompt("Enter password for Admin page")
if request.method == "POST":
data = json.load(json.dumps(request.form))
return data
@app.route("/urmom")
def urmom():
return render_template('person.html', data={"Name": "Your Mother", "About": "Its ur mom dont ask me", "Roles": ["Bitch", "Whore"], "Socials": []})
@app.route("/logs")
def logs():
data = []
lineNum = 0
with open("/root/saberfilmsapp/logs/gunicorn.access.log", "r") as f:
for line in f.readlines():
lineNum += 1
if not "uptimerobot" in line.lower():
if lineNum > len( f.readlines() ) / 2 :
data.append(line)
return "<br>".join(data)
@app.route('/upload', methods=["GET", "POST"])
def upload():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# If the user does not select a file, the browser submits an
# empty file without a filename.
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(UPLOAD_FOLDER, filename))
return redirect(url_for('behind_the_scenes'))
return '''
<!doctype html>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form method=post enctype=multipart/form-data>
<input type=file name=file>
<input type=submit value=Upload>
</form>
'''
@app.errorhandler(404)
def page_not_found_404(e):
return render_template("404.html"), 404
@app.errorhandler(500)
def server_error_500(e):
return render_template("500.html"), 500
if __name__ == "__main__":
app.run(
host=HOST,
port=PORT,
ssl_context=(
"/root/saberfilmsapp/certs/cert.pem",
"/root/saberfilmsapp/certs/key.pem",
),
debug=True,
)