Initialer Commit
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
import csv
|
||||
import re
|
||||
import urllib.request
|
||||
from fastapi import FastAPI
|
||||
import uvicorn
|
||||
import dl_csv
|
||||
import json
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"], # Erlaubt Zugriffe von allen Quellen/IPs im Netzwerk
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"], # Erlaubt alle HTTP-Methoden (GET, POST, etc.)
|
||||
allow_headers=["*"], # Erlaubt alle Header
|
||||
)
|
||||
|
||||
@app.get("/ninjiapi/all")
|
||||
def get_everything():
|
||||
try:
|
||||
return dl_csv.read_data()
|
||||
|
||||
except Exception as e:
|
||||
return {"success": False, "error": str(e)}
|
||||
|
||||
@app.get("/ninjiapi/user/{username}")
|
||||
def get_by_user(username: str):
|
||||
try:
|
||||
data = dl_csv.read_data()
|
||||
for player in data["leaderboard"]:
|
||||
if player["player"].lower() == username.lower():
|
||||
return player
|
||||
return username + "not found"
|
||||
|
||||
except Exception as e:
|
||||
return {"success": False, "error": str(e)}
|
||||
|
||||
@app.get("/ninjiapi/division/{division}")
|
||||
def get_all_by_division(division: str):
|
||||
try:
|
||||
data = dl_csv.read_data()
|
||||
return_data = []
|
||||
for player in data["leaderboard"]:
|
||||
if player["division"].lower() == division.lower():
|
||||
return_data.append(player)
|
||||
if not return_data == {}:
|
||||
return return_data
|
||||
return username + "not found"
|
||||
|
||||
except Exception as e:
|
||||
return {"success": False, "error": str(e)}
|
||||
|
||||
@app.get("/ninjiapi/division/user/{username}")
|
||||
def get_all_by_my_division(username: str):
|
||||
try:
|
||||
division = get_by_user(username)["division"]
|
||||
return get_all_by_division(division)
|
||||
|
||||
except Exception as e:
|
||||
return {"success": False, "error": str(e)}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# bindet an 0.0.0.0, damit es im gesamten Netzwerk erreichbar ist
|
||||
uvicorn.run("app:app", host="0.0.0.0", port=8000, reload=True)
|
||||
Reference in New Issue
Block a user