forked from doc22940/freeaudioconverter.net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
331 lines (284 loc) · 13.1 KB
/
main.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
from flask import Flask, request, render_template, send_from_directory, session
from flask_session import Session
from yt import yt # Importing the blueprint in yt.py
from trimmer import trimmer # Importing the blueprint in trimmer.py
from loggers import log, log_this, log_visit
from werkzeug.utils import secure_filename
import time
from datetime import datetime
import os
import converter
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from smtplib import SMTP
from confidential import *
app = Flask(__name__)
secret_key = str(os.urandom(16))
app.secret_key = secret_key
SESSION_TYPE = 'filesystem'
app.config.from_object(__name__)
Session(app)
app.register_blueprint(trimmer)
app.register_blueprint(yt)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
app.config['MAX_CONTENT_LENGTH'] = 5 * 1000 * 1000 * 1000 # 5 GB max upload size.
app.jinja_env.auto_reload = True
# When a file has been uploaded, a POST request is sent to the homepage.
@app.route("/", methods=["POST"])
def homepage():
if request.form['request_type'] == 'log_convert_clicked':
log_this('clicked Convert Button')
return ''
elif request.form["request_type"] == "uploaded":
session['progress_filename'] = str(time.time())[:-8]
log_this('uploaded a file:')
chosen_file = request.files["chosen_file"]
filesize = request.form["filesize"]
log.info(chosen_file)
log.info(f'Size: {filesize} MB')
# Make the filename safe
filename_secure = secure_filename(chosen_file.filename)
# Save the uploaded file to the uploads folder.
chosen_file.save(os.path.join("uploads", filename_secure))
return session['progress_filename']
elif request.form["request_type"] == "convert":
wav_bit_depth = request.form["wav_bit_depth"]
filename = request.form["filename"]
filename_without_ext = filename.split('.')[0]
chosen_codec = request.form["chosen_codec"]
crf_value = request.form["crf_value"]
mp4_encoding_mode = request.form["mp4_encoding_mode"]
is_keep_video = request.form["is_keep_video"]
uploaded_file_path = os.path.join("uploads", secure_filename(filename))
# MP3
mp3_encoding_type = request.form["mp3_encoding_type"]
mp3_bitrate = request.form["mp3_bitrate"]
mp3_vbr_setting = request.form["mp3_vbr_setting"]
# AAC
fdk_type = request.form["fdk_type"]
fdk_cbr = request.form["fdk_cbr"]
fdk_vbr = request.form["fdk_vbr"]
is_fdk_lowpass = request.form["is_fdk_lowpass"]
fdk_lowpass = request.form["fdk_lowpass"]
# Vorbis
vorbis_encoding = request.form["vorbis_encoding"]
vorbis_quality = request.form["vorbis_quality"]
# Vorbis/Opus
opus_vorbis_slider = request.form["opus_vorbis_slider"]
# AC3
ac3_bitrate = request.form["ac3_bitrate"]
# FLAC
flac_compression = request.form["flac_compression"]
# DTS
dts_bitrate = request.form["dts_bitrate"]
# Opus
opus_cbr_bitrate = request.form["opus_cbr_bitrate"]
opus_encoding_type = request.form["opus_encoding_type"]
# Desired filename
output_name = request.form["output_name"]
variables_to_validate = [filename_without_ext, chosen_codec, is_keep_video, mp3_encoding_type, mp3_bitrate,
mp3_vbr_setting, fdk_type, fdk_cbr, fdk_vbr, is_fdk_lowpass, vorbis_encoding, vorbis_quality,
opus_vorbis_slider, ac3_bitrate, flac_compression, dts_bitrate, opus_cbr_bitrate, opus_encoding_type,
output_name, wav_bit_depth, mp4_encoding_mode, crf_value]
strings_not_allowed = ['command', ';', '$', '&&', '/', '\\' '"', '?', '*', '<', '>', '|', ':', '`', '.']
# check_no_variable_contains_bad_string is a func defined in converter.py
if converter.is_bad_string_in_variables(variables_to_validate, strings_not_allowed):
return 'You tried being clever, but there\'s a server-side check for disallowed strings', 400
else:
log.info(f'They chose {chosen_codec}\nOutput Filename: {output_name}')
output_path = f'"conversions/{output_name}"'
# Run the appropritate section of converter.py:
if chosen_codec == 'MP3':
converter.run_mp3(session['progress_filename'], uploaded_file_path, is_keep_video, mp3_encoding_type, mp3_bitrate, mp3_vbr_setting, output_path)
if is_keep_video == "yes":
just_ext = uploaded_file_path.split('.')[-1]
if just_ext == 'mp4':
extension = 'mp4'
else:
extension = 'mkv'
else:
extension = 'mp3'
elif chosen_codec == 'AAC':
converter.run_aac(session['progress_filename'], uploaded_file_path, is_keep_video, fdk_type, fdk_cbr, fdk_vbr, is_fdk_lowpass,
fdk_lowpass, output_path)
if is_keep_video == "yes":
just_ext = uploaded_file_path.split('.')[-1]
if just_ext == 'mp4':
extension = 'mp4'
else:
extension = 'mkv'
else:
extension = 'm4a'
elif chosen_codec == 'Opus':
converter.run_opus(session['progress_filename'], uploaded_file_path, opus_encoding_type, opus_vorbis_slider, opus_cbr_bitrate,
output_path)
extension = 'opus'
elif chosen_codec == 'FLAC':
converter.run_flac(session['progress_filename'], uploaded_file_path, is_keep_video, flac_compression, output_path)
if is_keep_video == "yes":
extension = 'mkv'
else:
extension = 'flac'
elif chosen_codec == 'Vorbis':
converter.run_vorbis(session['progress_filename'], uploaded_file_path, vorbis_encoding, vorbis_quality, opus_vorbis_slider,
output_path)
extension = 'mka'
elif chosen_codec == 'WAV':
converter.run_wav(session['progress_filename'], uploaded_file_path, is_keep_video, wav_bit_depth, output_path)
if is_keep_video == "yes":
extension = 'mkv'
else:
extension = 'wav'
elif chosen_codec == 'MKV':
converter.run_mkv(session['progress_filename'], uploaded_file_path, output_path)
extension = 'mkv'
elif chosen_codec == 'MKA':
converter.run_mka(session['progress_filename'], uploaded_file_path, output_path)
extension = 'mka'
elif chosen_codec == 'ALAC':
converter.run_alac(session['progress_filename'], uploaded_file_path, is_keep_video, output_path)
if is_keep_video == "yes":
extension = 'mkv'
else:
extension = 'm4a'
elif chosen_codec == 'AC3':
converter.run_ac3(session['progress_filename'], uploaded_file_path, is_keep_video, ac3_bitrate, output_path)
if is_keep_video == "yes":
just_ext = uploaded_file_path.split('.')[-1]
if just_ext == 'mp4':
extension = 'mp4'
else:
extension = 'mkv'
else:
extension = 'ac3'
elif chosen_codec == 'CAF':
converter.run_caf(session['progress_filename'], uploaded_file_path, output_path)
extension = 'caf'
elif chosen_codec == 'DTS':
converter.run_dts(session['progress_filename'], uploaded_file_path, is_keep_video, dts_bitrate, output_path)
if is_keep_video == "yes":
extension = 'mkv'
else:
extension = 'dts'
elif chosen_codec == 'MP4':
converter.run_mp4(session['progress_filename'], uploaded_file_path, mp4_encoding_mode, crf_value, output_path)
extension = 'mp4'
elif chosen_codec == 'MKV':
converter.run_mkv(session['progress_filename'], uploaded_file_path, output_path)
extension = 'mkv'
converted_file_name = output_name + "." + extension
return f'/conversions/{converted_file_name}'
@app.route("/conversions/<filename>", methods=["GET"])
def send_file(filename):
just_extension = filename.split('.')[-1]
if just_extension == "m4a":
log.info('Sending the file to the user.')
return send_from_directory(f'{os.getcwd()}/conversions', filename, mimetype="audio/mp4", as_attachment=True)
else:
log.info('Sending the file to the user.')
return send_from_directory(f'{os.getcwd()}/conversions', filename, as_attachment=True)
# CONTACT PAGE
@app.route("/contact", methods=["POST"])
def send_email():
send_from = "[email protected]"
send_to = "[email protected]"
text = MIMEMultipart()
text['From'] = send_from
text['To'] = send_to
text['Subject'] = "Your Website"
body = request.form['message']
text.attach(MIMEText(body, 'plain'))
server = SMTP(host='smtp-mail.outlook.com', port=587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(my_email, password)
text = text.as_string()
server.sendmail(send_from, send_to, text)
return "Message sent!"
# GAME 1
@app.route("/game", methods=['POST'])
def return_world_record():
current_datetime = datetime.now().strftime('%d-%m-%y at %H:%M:%S')
user = request.environ.get("HTTP_X_REAL_IP").split(',')[0]
user_agent = request.headers.get('User-Agent')
score = request.form['score']
times_missed = request.form['times_missed']
canvas_width = request.form['canvas_width']
canvas_height = request.form['canvas_height']
try:
int(score)
int(times_missed)
int(canvas_width)
int(canvas_width)
except ValueError:
log.error("GAME 1: The user changed something to a non-int.")
else:
with open("Game Scores/HighScores.txt", "a") as f:
f.write(f'{score} | {times_missed} | {user} | {user_agent} | {canvas_width}'
f'x{canvas_height} | {current_datetime}\n')
finally:
just_scores = []
with open('Game Scores/HighScores.txt', 'r') as f:
lines = f.readlines()
for line in lines:
just_scores.append(line.split('|')[0].strip())
valid_scores = [x for x in just_scores if int(x) < 100]
world_record = max(valid_scores, key=lambda x: int(x))
return world_record
# GAME 2
@app.route("/game2", methods=['POST'])
def save_game2_stats():
current_datetime = datetime.now().strftime('%d-%m-%y at %H:%M:%S')
user = request.environ.get("HTTP_X_REAL_IP").split(',')[0]
user_agent = request.headers.get('User-Agent')
reaction_time = request.form['reaction_time']
try:
int(reaction_time)
except ValueError:
log.error("GAME 2: The user changed reaction_time to a non-int.")
else:
with open("Game Scores/ReactionTimes.txt", "a") as f:
f.write(f'{reaction_time} ms | {user} | {user_agent} | {current_datetime}\n')
finally:
reaction_times = []
with open('Game Scores/ReactionTimes.txt', 'r') as f:
lines = f.readlines()
for line in lines:
reaction_times.append(line.split('|')[0][:-3].strip())
# reaction_record = min(reaction_times, key=lambda x: int(x))
return ''
@app.route("/")
def homepage_visited():
log_visit("visited homepage")
return render_template("home.html", title="FreeAudioConverter.net")
@app.route("/about")
def about_page_visited():
log_visit("visited about page")
return render_template("about.html", title="About")
@app.route("/filetypes")
def filetypes_visited():
log_visit("visited filetypes")
return render_template("filetypes.html", title="Filetypes")
@app.route("/yt")
def yt_page_visited():
log_visit("visited YT")
return render_template("yt.html")
@app.route("/trimmer")
def trimmer_visited():
log_visit("visited trimmer")
return render_template("trimmer.html", title="File Trimmer")
@app.route("/contact")
def contact_page_visited():
log_visit("visited contact page")
return render_template("contact.html", title="Contact")
@app.route("/game")
def game_visited():
log_visit("visited game")
return render_template("game.html", title="Game")
@app.route("/game2")
def game2_visited():
log_visit("visited game 2")
return render_template("game2.html", title="Game 2")
if __name__ == "__main__":
app.run(host='0.0.0.0')