Files
API5005/main.py
T

50 lines
1.5 KiB
Python
Raw Normal View History

2026-10-17 02:27:56 +02:00
import threading
import time
import fastapi
import requests
2026-10-17 02:32:18 +02:00
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.errors import RateLimitExceeded
from slowapi.middleware import SlowAPIMiddleware
from slowapi.util import get_remote_address
2026-10-17 02:27:56 +02:00
2026-10-17 02:32:18 +02:00
limiter = Limiter(key_func=get_remote_address, default_limits=["50/second"])
2026-10-17 02:27:56 +02:00
app = fastapi.FastAPI()
2026-10-17 02:32:18 +02:00
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
app.add_middleware(SlowAPIMiddleware)
2026-10-17 02:27:56 +02:00
lanyard_link = "https://api.lanyard.rest/v1/users/1558056824387805300"
CACHE_TTL = 1.0
_cache = {"data": None, "time": 0.0}
_lock = threading.Lock()
def get_lanyard():
if _cache["data"] is not None and time.monotonic() - _cache["time"] < CACHE_TTL:
return _cache["data"]
with _lock:
if _cache["data"] is not None and time.monotonic() - _cache["time"] < CACHE_TTL:
return _cache["data"]
try:
response = requests.get(lanyard_link, timeout=5)
response.raise_for_status()
_cache["data"] = response.json()
_cache["time"] = time.monotonic()
except requests.RequestException:
if _cache["data"] is None:
raise fastapi.HTTPException(status_code=502, detail="Lanyard indisponible")
return _cache["data"]
@app.get("/")
def read_root():
return {"message": "Welcome to IKAVOID API (5005)"}
@app.get("/discord")
def read_lanyard():
return get_lanyard()
if __name__ == "__main__":
import uvicorn
2026-10-17 03:16:45 +02:00
uvicorn.run(app, host="::", port=5005)