mirror of
https://github.com/FAUSheppy/tmnf-replay-server.git
synced 2025-12-06 07:01:37 +01:00
wip: basic parse + multiupload
This commit is contained in:
53
server.py
53
server.py
@@ -1,17 +1,21 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
import os
|
import os
|
||||||
import flask
|
import flask
|
||||||
|
import werkzeug
|
||||||
import argparse
|
import argparse
|
||||||
import sys
|
import sys
|
||||||
import json
|
import json
|
||||||
|
import datetime
|
||||||
|
|
||||||
|
from pygbx import Gbx, GbxType
|
||||||
|
|
||||||
from sqlalchemy import Column, Integer, String, Boolean, or_, and_, asc, desc
|
from sqlalchemy import Column, Integer, String, Boolean, or_, and_, asc, desc
|
||||||
from flask_sqlalchemy import
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
|
|
||||||
|
app = flask.Flask("TM Friends Replay Server")
|
||||||
|
|
||||||
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
|
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
|
||||||
app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get("SQLITE_LOCATION") or "sqlite:///sqlite.db"
|
app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get("SQLITE_LOCATION") or "sqlite:///sqlite.db"
|
||||||
|
|
||||||
app = flask.Flask("TM Friends Replay Server")
|
|
||||||
db = SQLAlchemy(app)
|
db = SQLAlchemy(app)
|
||||||
|
|
||||||
class ParsedReplay(db.Model):
|
class ParsedReplay(db.Model):
|
||||||
@@ -27,16 +31,19 @@ class ParsedReplay(db.Model):
|
|||||||
upload_dt = Column(String)
|
upload_dt = Column(String)
|
||||||
|
|
||||||
map_uid = Column(String) # ghost_uid
|
map_uid = Column(String) # ghost_uid
|
||||||
ghost_login = Column(String)
|
login = Column(String)
|
||||||
|
cp_times = Column(String)
|
||||||
|
|
||||||
ghost_cp_times = Column(String)
|
def guess_map(self):
|
||||||
|
base = os.path.basename(self.filepath)
|
||||||
|
return base.split("_")[1].split(".Replay")[0]
|
||||||
|
|
||||||
def get_human_readable_time(self):
|
def get_human_readable_time(self):
|
||||||
t = datetime.timedelta(seconds=racetime)
|
t = datetime.timedelta(microseconds=self.race_time*1000)
|
||||||
t_string = str(t)
|
t_string = str(t)
|
||||||
if t.hours == 0:
|
if t.seconds < 60*60:
|
||||||
return t_string[2:]
|
t_string = t_string[2:]
|
||||||
return t_string
|
return t_string[:-4]
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "{time} on {map_n} by {login}/{uploader}".format(time=self.get_human_readable_time(),
|
return "{time} on {map_n} by {login}/{uploader}".format(time=self.get_human_readable_time(),
|
||||||
@@ -56,10 +63,10 @@ def replay_from_path(fullpath, uploader=None):
|
|||||||
race_time=ghost.race_time,
|
race_time=ghost.race_time,
|
||||||
uploader=uploader,
|
uploader=uploader,
|
||||||
filepath=fullpath,
|
filepath=fullpath,
|
||||||
map_uid=ghost.uid
|
map_uid=ghost.uid,
|
||||||
ghost_login=ghost.login,
|
login=ghost.login,
|
||||||
upload_dt=datetime.datetime.now().isoformat(),
|
upload_dt=datetime.datetime.now().isoformat(),
|
||||||
ghost.cp_times=",".join(ghost.cp_times))
|
cp_times=",".join(map(str, ghost.cp_times)))
|
||||||
|
|
||||||
return replay
|
return replay
|
||||||
|
|
||||||
@@ -72,14 +79,14 @@ def upload():
|
|||||||
if flask.request.method == 'POST':
|
if flask.request.method == 'POST':
|
||||||
#f = flask.request.files['file']
|
#f = flask.request.files['file']
|
||||||
f_list = flask.request.files.getlist("file[]")
|
f_list = flask.request.files.getlist("file[]")
|
||||||
print(f_list)
|
for f_storage in f_list:
|
||||||
return ""
|
fname = werkzeug.utils.secure_filename(f_storage.filename)
|
||||||
fname = werkzeug.utils.secure_filename(f.filename)
|
fullpath = os.path.join("uploads/", fname)
|
||||||
fullpath = os.path.join("uploads/", fname)
|
f_storage.save(fullpath)
|
||||||
f.save(fullpath)
|
replay = replay_from_path(fullpath)
|
||||||
replay = replay_from_path(fullpath)
|
print(replay)
|
||||||
db.add(replay)
|
db.session.add(replay)
|
||||||
db.commit()
|
db.session.commit()
|
||||||
else:
|
else:
|
||||||
return flask.render_template("upload.html")
|
return flask.render_template("upload.html")
|
||||||
|
|
||||||
@@ -94,8 +101,10 @@ if __name__ == "__main__":
|
|||||||
# general parameters #
|
# general parameters #
|
||||||
parser.add_argument("-i", "--interface", default="127.0.0.1", help="Interface to listen on")
|
parser.add_argument("-i", "--interface", default="127.0.0.1", help="Interface to listen on")
|
||||||
parser.add_argument("-p", "--port", default="5000", help="Port to listen on")
|
parser.add_argument("-p", "--port", default="5000", help="Port to listen on")
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
# startup #
|
# startup #
|
||||||
args = parser.parse_args()
|
with app.app_context():
|
||||||
create_app()
|
create_app()
|
||||||
|
|
||||||
app.run(host=args.interface, port=args.port)
|
app.run(host=args.interface, port=args.port)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<html>
|
<html>
|
||||||
<body>
|
<body>
|
||||||
<form action="/upload" method="POST" enctype="multipart/form-data">
|
<form action="/upload" method="POST" enctype="multipart/form-data">
|
||||||
<input type="file" name="file []" multiple=""/>
|
<input type="file" name="file[]" multiple=""/>
|
||||||
<input type="submit"/>
|
<input type="submit"/>
|
||||||
</form>
|
</form>
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
Reference in New Issue
Block a user