Files
TiropitLauncher/API/Tiri.py

106 lines
3.0 KiB
Python
Raw Normal View History

2025-09-06 02:57:46 +03:00
from flask import Flask, jsonify, request
import mysql.connector
import uuid
import base64
import json
2025-09-06 15:52:22 +03:00
import keyfile
2025-09-06 02:57:46 +03:00
2025-09-06 15:52:22 +03:00
API_KEY_NS = keyfile.API_KEY
2025-09-06 02:57:46 +03:00
app = Flask(__name__)
# Database connection
2025-09-06 15:55:28 +03:00
db_config = keyfile.requestDBCreds
2025-09-06 02:57:46 +03:00
def get_db_connection():
try:
connection = mysql.connector.connect(**db_config)
return connection
except mysql.connector.Error as err:
print(f"Error: {err}")
return None
2025-09-06 17:00:20 +03:00
2025-09-06 02:57:46 +03:00
# NEEDS FIXING
@app.route('/api/tiropitAddUser', methods=['POST'])
def tiropit_add_user():
2025-09-06 17:00:20 +03:00
data = request.json
if not data or "UserName" not in data or "UserPIN" not in data:
return jsonify({"error": "Account MUST include at least UserName & UserPIN"}), 400
user_pin = data.get("UserPIN")
friend_code = data.get("FriendCode")
2025-09-06 02:57:46 +03:00
if not friend_code or len(friend_code) != 5:
return jsonify({"error": "FirendCode must be 5 characters long"}), 400
2025-09-06 17:00:20 +03:00
if not user_pin or len(user_pin) != 4:
2025-09-06 02:57:46 +03:00
return jsonify({"error": "UserPIN must be 4 characters long"}), 400
2025-09-15 16:59:07 +03:00
if isinstance(awards_data, str):
awards_data = json.loads(awards_data)
if isinstance(user_games_data, str):
user_games_data = json.loads(user_games_data)
2025-09-06 17:00:20 +03:00
connection = get_db_connection()
if connection is None:
return jsonify({"error": "Failed to contact db"}), 500
2025-09-06 02:57:46 +03:00
try:
2025-09-06 17:13:44 +03:00
cursor = connection.cursor(dictionary=True)
2025-09-06 17:00:20 +03:00
user_data = {
"UserID" : str(uuid.uuid4()),
"UserName" : data['UserName'],
"UserPIN" : user_pin,
"SteamName" : data.get('SteamName', ''),
"Level" : data.get('Level', 0),
"Awards" : json.dumps(data.get('Awards', '[]')),
"ProfilePicture" : data.get('ProfilePicture', ''),
"UserGames" : json.dumps(data.get('UserGames', '[]')),
"WarnLevel" : data.get('WarnLevel', 0),
"Banned" : data.get('Banned', False),
"AdminLevel" : 0,
"UserDiscord" : data.get('UserDiscord', ""),
"SteamID" : data.get('SteamID', '') ,
"FriendCode" : friend_code,
}
2025-09-06 17:13:44 +03:00
print(user_data)
2025-09-06 17:00:20 +03:00
query = """
INSERT INTO users (UserID, UserName, SteamName, Level, Awards, UserGames, WarnLevel, Banned, AdminLevel, SteamID, UserDiscord, UserPIN, FriendCode)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
"""
cursor.execute(query, tuple(user_data.values()))
connection.commit()
return jsonify(
{"message":"User Created",
"UserID": user_data['UserID']}, 201
)
except mysql.connector.Error as err:
connection.rollback()
if err.errno == 1062:
return jsonify({"error": "User already exists"}), 409
2025-09-06 02:57:46 +03:00
finally:
2025-09-06 17:00:20 +03:00
if connection.is_connected():
cursor.close()
connection.close()
2025-09-06 17:13:44 +03:00
return jsonify({"error": "An unexpected server error occurred."}), 500
2025-09-06 02:57:46 +03:00
2025-09-06 17:00:20 +03:00
if __name__ == '__main__':
app.run(host="0.0.0.0", port=5000, debug=True)