Compare commits
2 Commits
ad087b9547
...
686e4ff477
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
686e4ff477 | ||
|
|
c7b3f95439 |
63
app.py
63
app.py
@@ -8,6 +8,9 @@ from ingress import IngressAPI
|
|||||||
from models import EventType, Plext
|
from models import EventType, Plext
|
||||||
from pymongo import MongoClient
|
from pymongo import MongoClient
|
||||||
from pymongo.errors import PyMongoError
|
from pymongo.errors import PyMongoError
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
|
load_dotenv()
|
||||||
|
|
||||||
# Timezone configuration
|
# Timezone configuration
|
||||||
TIMEZONE = ZoneInfo("Europe/Rome")
|
TIMEZONE = ZoneInfo("Europe/Rome")
|
||||||
@@ -49,6 +52,7 @@ def basic_auth_required(f):
|
|||||||
Returns:
|
Returns:
|
||||||
401 Unauthorized if authentication fails or is missing
|
401 Unauthorized if authentication fails or is missing
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@wraps(f)
|
@wraps(f)
|
||||||
def decorated(*args, **kwargs):
|
def decorated(*args, **kwargs):
|
||||||
auth = request.authorization
|
auth = request.authorization
|
||||||
@@ -58,7 +62,7 @@ def basic_auth_required(f):
|
|||||||
"Could not verify your access level for that URL.\n"
|
"Could not verify your access level for that URL.\n"
|
||||||
"You have to login with proper credentials",
|
"You have to login with proper credentials",
|
||||||
401,
|
401,
|
||||||
{"WWW-Authenticate": 'Basic realm="Login Required"'}
|
{"WWW-Authenticate": 'Basic realm="Login Required"'},
|
||||||
)
|
)
|
||||||
|
|
||||||
return f(*args, **kwargs)
|
return f(*args, **kwargs)
|
||||||
@@ -150,19 +154,22 @@ def plext_to_dict(plext: Plext) -> dict:
|
|||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@app.route("/plexts/from-db", methods=["GET", "OPTIONS"])
|
@app.route("/plexts/from-db", methods=["GET", "OPTIONS"])
|
||||||
@basic_auth_required
|
@basic_auth_required
|
||||||
def get_plexts_from_db():
|
def get_plexts_from_db():
|
||||||
"""
|
"""
|
||||||
Get plexts from MongoDB with optional filters.
|
Get plexts from MongoDB with optional filters.
|
||||||
|
Returns plexts sorted by timestamp (most recent first).
|
||||||
|
|
||||||
Query Parameters:
|
Query Parameters:
|
||||||
player_name: Filter by player name (optional)
|
player_name: Filter by player name (optional)
|
||||||
timestamp_from: Minimum timestamp in milliseconds (optional)
|
timestamp_from: Minimum timestamp in milliseconds (optional)
|
||||||
timestamp_to: Maximum timestamp in milliseconds (optional)
|
timestamp_to: Maximum timestamp in milliseconds (optional)
|
||||||
|
limit: Maximum number of results to return (optional, default: 100)
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
JSON response with list of plexts (without _id field)
|
JSON response with list of plexts (without _id field), limited by the limit parameter
|
||||||
"""
|
"""
|
||||||
# CORS headers
|
# CORS headers
|
||||||
cors_headers = {
|
cors_headers = {
|
||||||
@@ -180,19 +187,47 @@ def get_plexts_from_db():
|
|||||||
player_name = request.args.get("player_name")
|
player_name = request.args.get("player_name")
|
||||||
timestamp_from = request.args.get("timestamp_from")
|
timestamp_from = request.args.get("timestamp_from")
|
||||||
timestamp_to = request.args.get("timestamp_to")
|
timestamp_to = request.args.get("timestamp_to")
|
||||||
|
limit_param = request.args.get("limit")
|
||||||
|
|
||||||
# Validate and convert timestamp parameters to integers if provided
|
# Validate and convert timestamp parameters to integers if provided
|
||||||
if timestamp_from is not None:
|
if timestamp_from is not None:
|
||||||
try:
|
try:
|
||||||
timestamp_from = int(timestamp_from)
|
timestamp_from = int(timestamp_from)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return jsonify({"error": "timestamp_from must be an integer"}), 400, cors_headers
|
return (
|
||||||
|
jsonify({"error": "timestamp_from must be an integer"}),
|
||||||
|
400,
|
||||||
|
cors_headers,
|
||||||
|
)
|
||||||
|
|
||||||
if timestamp_to is not None:
|
if timestamp_to is not None:
|
||||||
try:
|
try:
|
||||||
timestamp_to = int(timestamp_to)
|
timestamp_to = int(timestamp_to)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return jsonify({"error": "timestamp_to must be an integer"}), 400, cors_headers
|
return (
|
||||||
|
jsonify({"error": "timestamp_to must be an integer"}),
|
||||||
|
400,
|
||||||
|
cors_headers,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Validate and convert limit parameter to integer if provided, otherwise default to 100
|
||||||
|
if limit_param is not None:
|
||||||
|
try:
|
||||||
|
limit = int(limit_param)
|
||||||
|
if limit <= 0:
|
||||||
|
return (
|
||||||
|
jsonify({"error": "limit must be a positive integer"}),
|
||||||
|
400,
|
||||||
|
cors_headers,
|
||||||
|
)
|
||||||
|
except ValueError:
|
||||||
|
return (
|
||||||
|
jsonify({"error": "limit must be an integer"}),
|
||||||
|
400,
|
||||||
|
cors_headers,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
limit = 100
|
||||||
|
|
||||||
# Build MongoDB filter query
|
# Build MongoDB filter query
|
||||||
filter_query = {}
|
filter_query = {}
|
||||||
@@ -221,19 +256,17 @@ def get_plexts_from_db():
|
|||||||
# Projection to exclude _id field
|
# Projection to exclude _id field
|
||||||
projection = {"_id": 0}
|
projection = {"_id": 0}
|
||||||
|
|
||||||
# Execute query with sorting (timestamp DESC - most recent first)
|
# Execute query with sorting (timestamp DESC - most recent first) and dynamic limit
|
||||||
cursor = collection.find(
|
cursor = (
|
||||||
filter=filter_query,
|
collection.find(filter=filter_query, projection=projection)
|
||||||
projection=projection
|
.sort("timestamp", -1)
|
||||||
).sort("timestamp", -1)
|
.limit(limit)
|
||||||
|
)
|
||||||
|
|
||||||
# Convert cursor to list
|
# Convert cursor to list
|
||||||
plexts = list(cursor)
|
plexts = list(cursor)
|
||||||
|
|
||||||
return jsonify({
|
return jsonify({"count": len(plexts), "plexts": plexts}), 200, cors_headers
|
||||||
"count": len(plexts),
|
|
||||||
"plexts": plexts
|
|
||||||
}), 200, cors_headers
|
|
||||||
|
|
||||||
except PyMongoError as e:
|
except PyMongoError as e:
|
||||||
logger.error(f"MongoDB error: {e}")
|
logger.error(f"MongoDB error: {e}")
|
||||||
@@ -245,6 +278,7 @@ def get_plexts_from_db():
|
|||||||
logger.exception("Unexpected error in get_plexts_from_db")
|
logger.exception("Unexpected error in get_plexts_from_db")
|
||||||
return jsonify({"error": "An error occurred"}), 500, cors_headers
|
return jsonify({"error": "An error occurred"}), 500, cors_headers
|
||||||
|
|
||||||
|
|
||||||
@app.route("/plexts/from-api", methods=["GET"])
|
@app.route("/plexts/from-api", methods=["GET"])
|
||||||
@basic_auth_required
|
@basic_auth_required
|
||||||
def get_plexts_from_api():
|
def get_plexts_from_api():
|
||||||
@@ -359,11 +393,12 @@ def index():
|
|||||||
},
|
},
|
||||||
"/plexts/from-db": {
|
"/plexts/from-db": {
|
||||||
"method": "GET",
|
"method": "GET",
|
||||||
"description": "Get plexts from MongoDB with optional filters",
|
"description": "Get plexts from MongoDB with optional filters. Returns plexts sorted by timestamp (most recent first).",
|
||||||
"parameters": {
|
"parameters": {
|
||||||
"player_name": "Filter by player name (optional)",
|
"player_name": "Filter by player name (optional)",
|
||||||
"timestamp_from": "Minimum timestamp in milliseconds (optional)",
|
"timestamp_from": "Minimum timestamp in milliseconds (optional)",
|
||||||
"timestamp_to": "Maximum timestamp in milliseconds (optional)",
|
"timestamp_to": "Maximum timestamp in milliseconds (optional)",
|
||||||
|
"limit": "Maximum number of results to return (optional, default: 100)",
|
||||||
},
|
},
|
||||||
"authentication": "Basic Auth required",
|
"authentication": "Basic Auth required",
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user