-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
101 lines (80 loc) · 2.96 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
from flask import Flask, send_from_directory, request
from backend.beatmap_helper import *
from backend.utils import *
import traceback
app = Flask(__name__, static_folder='dist')
@app.route('/')
def index():
return app.send_static_file('index.html')
@app.route('/static/<path:path>')
def serve_static(path):
return send_from_directory('./dist/static', path)
@app.route('/api/get_file/<path:path>')
def serve_get_file(path):
return send_from_directory('./upload_dir', path)
@app.route('/api/get_speeds')
def api_get_speeds():
speeds = []
try:
with open("speeds.txt", "r") as f:
for line in f.readlines():
try:
speed = int(1000 * float(line)) / 1000
if (speed > 0.49999) and (speed < 2.00001):
speeds.append(speed)
except:
pass
except:
pass
if len(speeds) == 0:
speeds = [0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1.05, 1.1, 1.15, 1.2, 1.25, 1.3, 1.4, 1.5, 1.6, 1.7, 2.0]
return json.dumps({
"success": True,
"speeds": speeds
})
@app.route('/api/upload_file', methods=["POST"])
def api_upload_file():
if request.method == 'POST':
if 'file' not in request.files:
return json.dumps({'success': False, 'msg': 'error parameters'})
file = request.files['file']
if file.filename == '':
return json.dumps({'success': False, 'msg': 'no file selected'})
else:
if file and allowed_file(file.filename):
origin_file_name = file.filename
print(origin_file_name)
file.save(os.path.join("upload_dir", "tmp", origin_file_name))
with open(os.path.join("upload_dir", "now_file.txt"), "w") as ft:
ft.write(origin_file_name)
beatmaps = get_beatmaps(create=True)
if len(beatmaps) != 0:
return json.dumps({'success': True, 'beatmaps': beatmaps, 'msg': 'success'})
else:
return json.dumps({'success': False, 'msg': 'cannot find beatmap'})
else:
return json.dumps({'success': False, 'msg': 'error file type'})
@app.route('/api/generate_beatmaps')
def api_generate_beatmaps():
try:
speeds = request.args.get("speeds")
index = request.args.get("index")
index = int(index)
speeds = json.loads(speeds)
beatmaps = get_beatmaps(create=False)
beatmap = beatmaps[index]
_type = beatmap["type"]
outdir = beatmap["out_dir"]
json_data = beatmap["json_data"]
tmp_file = generate_beatmaps(_type, outdir, json_data, speeds)
return json.dumps({
"success": True,
"file": tmp_file
})
except:
traceback.print_exc()
return json.dumps({
"success": False
})
if __name__ == '__main__':
app.run(port=4776, debug=True)