from flask import Flask, render_template, request, redirect import os import json from datetime import datetime import pytz app = Flask(__name__) datap = "" @app.route('/r1/') def game_r1(): with open("frabjousish.json", "r") as f: json_file = f.read() database = json.loads(json_file) response = database["frabjousish"] return render_template("index3.html", response = response) @app.route('/r2/') def game_r2(): with open("shusho.json", "r") as f: json_file = f.read() database = json.loads(json_file) response = database["shusho"] return render_template("index5.html", response = response) @app.route('/r3/') def game_r3(): with open("t offut.json", "r") as f: json_file = f.read() database = json.loads(json_file) response = database["t offut"] return render_template("index7.html", response = response) @app.route("/", methods=['POST', 'GET']) def game(): print("frabjousish.json") print(os.path.exists("frabjousish.json")) if not os.path.exists("frabjousish.json"): with open("frabjousish.json", "w") as fra: frab = { "frabjousish": ["frabjousish", "frabjousish"] } json.dump(frab, fra, indent=1) with open("frabjousish.json", "r") as fra: json_file = fra.read() data2 = json.loads(json_file) datap = data2["frabjousish"][-1] datap2 = data2["frabjousish"][-2] # type(datap) if request.method == 'GET': return render_template("index2.html", datap=datap, datap2=datap2) else: if ".wav" in datap: spel = request.form.get('spel') with open("frabjousish.json", "r+") as jsonFile: data = json.load(jsonFile) data["frabjousish"].append(spel) with open("frabjousish.json", "w") as jsonFile: json.dump(data, jsonFile, indent=1) with open("frabjousish.json", "r") as f: json_file = f.read() database = json.loads(json_file) response = database["frabjousish"] return render_template("index3.html", database = database, response = response, datap=datap, data2=data2) # else: # if request.method == "POST": # f = request.files['audio_data'] # filename = f.filename # with open(f'{ filename }.wav', 'wb') as audio: # f.save(audio) # print(f'file uploaded successfully: { filename }.wav') # with open("frabjousish.json", "r+") as jsonFile: # data = json.load(jsonFile) # data["frabjousish"].append(filename+".wav") # with open("frabjousish.json", "w") as jsonFile: # json.dump(data, jsonFile, indent=1) # return render_template('index3.html', datap=datap) # else: # return render_template("index3.html") @app.route("/upload/", methods=['POST']) def game_post(): if request.method == "POST": f = request.files['audio_data'] filename = f.filename with open(f'{ filename }.wav', 'wb') as audio: f.save(audio) print(f'file uploaded successfully: { filename }.wav') with open("frabjousish.json", "r+") as jsonFile: data = json.load(jsonFile) data["frabjousish"].append(filename+".wav") with open("frabjousish.json", "w") as jsonFile: json.dump(data, jsonFile, indent=1) return "uploaded" @app.route("/g1/", methods=['POST', 'GET']) def game2(): print("shusho.json") print(os.path.exists("shusho.json")) if not os.path.exists("shusho.json"): with open("shusho.json", "w") as fra: frab = { "shusho": ["shusho", "shusho"] } json.dump(frab, fra, indent=1) with open("shusho.json", "r") as fra: json_file = fra.read() data2 = json.loads(json_file) datap = data2["shusho"][-1] datap2 = data2["shusho"][-2] # type(datap) if request.method == 'GET': return render_template("index4.html", datap=datap, datap2=datap2) else: if ".wav" in datap: spel = request.form.get('spel') with open("shusho.json", "r+") as jsonFile: data = json.load(jsonFile) data["shusho"].append(spel) with open("shusho.json", "w") as jsonFile: json.dump(data, jsonFile, indent=1) with open("shusho.json", "r") as f: json_file = f.read() database = json.loads(json_file) response = database["shusho"] return render_template("index5.html", database = database, response = response, datap=datap, data2=data2) else: if request.method == "POST": f = request.files['audio_data'] filename = f.filename with open(f'{ filename }.wav', 'wb') as audio: f.save(audio) print(f'file uploaded successfully: { filename }.wav') with open("shusho.json", "r+") as jsonFile: data = json.load(jsonFile) data["shusho"].append(filename+".wav") with open("shusho.json", "w") as jsonFile: json.dump(data, jsonFile, indent=1) return render_template('index5.html', request="POST", datap=datap) else: return render_template("index5.html") @app.route("/g2/", methods=['POST', 'GET']) def game3(): print("t offut.json") print(os.path.exists("t offut.json")) if not os.path.exists("t offut.json"): with open("t offut.json", "w") as fra: frab = { "t offut": ["t offut", "t offut"] } json.dump(frab, fra, indent=1) with open("t offut.json", "r") as fra: json_file = fra.read() data2 = json.loads(json_file) datap = data2["t offut"][-1] datap2 = data2["t offut"][-2] # type(datap) if request.method == 'GET': return render_template("index6.html", datap=datap, datap2=datap2) else: if ".wav" in datap: spel = request.form.get('spel') with open("t offut.json", "r+") as jsonFile: data = json.load(jsonFile) data["t offut"].append(spel) with open("t offut.json", "w") as jsonFile: json.dump(data, jsonFile, indent=1) with open("t offut.json", "r") as f: json_file = f.read() database = json.loads(json_file) response = database["t offut"] return render_template("index7.html", database = database, response = response, datap=datap, data2=data2) else: if request.method == "POST": f = request.files['audio_data'] filename = f.filename with open(f'{ filename }.wav', 'wb') as audio: f.save(audio) print(f'file uploaded successfully: { filename }.wav') with open("t offut.json", "r+") as jsonFile: data = json.load(jsonFile) data["t offut"].append(filename+".wav") with open("t offut.json", "w") as jsonFile: json.dump(data, jsonFile, indent=1) return render_template('index7.html', request="POST", datap=datap) else: return render_template("index7.html") if __name__ == "__main__": app.run(debug=True, port=5050)