-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpublic_servers.py
32 lines (28 loc) · 1.23 KB
/
public_servers.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
from flask import Flask, request, jsonify
import sqlite3
app = Flask(__name__)
@app.route('/servers', methods=['GET'])
def get_servers():
connection = sqlite3.connect('servers.db')
cursor = connection.cursor()
cursor.execute("SELECT address, port, n_players FROM servers WHERE time >= strftime('%s', 'now') - 60")
servers = [{"address": row[0], "port": row[1], "nPlayers": row[2]} for row in cursor.fetchall()]
connection.close()
return jsonify(servers)
@app.route('/publish', methods=['POST'])
def publish_server():
data = request.get_json()
connection = sqlite3.connect('servers.db')
connection.cursor().execute("INSERT OR REPLACE INTO servers (address, port, time, n_players) VALUES (?, ?, strftime('%s', 'now'), ?)",
(data["address"], data["port"], data["nPlayers"]))
connection.commit()
connection.close()
return "", 204
if __name__ == '__main__':
connection = sqlite3.connect('servers.db')
connection.cursor().execute('''CREATE TABLE IF NOT EXISTS servers
(address TEXT, port TEXT, time INTEGER, n_players INTEGER,
PRIMARY KEY (address, port))''')
connection.commit()
connection.close()
app.run(host='::', port=8080, debug=True)