feat: Add Links stats
This commit is contained in:
17
server.py
17
server.py
@@ -234,6 +234,23 @@ def api_immich_stats():
|
||||
return jsonify(stats)
|
||||
|
||||
|
||||
@app.route("/api/v1/links", methods=["GET"])
|
||||
def api_links_stats():
|
||||
"""
|
||||
API endpoint to get the user's Links stats.
|
||||
"""
|
||||
|
||||
user = session.get("user")
|
||||
if not user:
|
||||
return jsonify({"error": "Unauthorized"}), 401
|
||||
from tools.links import get_links_stats
|
||||
|
||||
stats = get_links_stats(user["email"])
|
||||
if "error" in stats:
|
||||
return jsonify(stats), 500
|
||||
return jsonify(stats)
|
||||
|
||||
|
||||
# endregion
|
||||
|
||||
|
||||
|
||||
@@ -42,4 +42,26 @@ document.addEventListener("DOMContentLoaded", () => {
|
||||
console.error("Error fetching Immich stats:", error);
|
||||
});
|
||||
}
|
||||
|
||||
// Fetch Links stats and display them
|
||||
const linksLink = document.getElementById("links");
|
||||
if (linksLink) {
|
||||
fetch("/api/v1/links")
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (!data.error) {
|
||||
// Create a new span element to display the stats
|
||||
const statsLabel = document.createElement("p");
|
||||
statsLabel.classList.add("service-note");
|
||||
statsLabel.textContent = `Links: ${data.links_count}`;
|
||||
// Append the stats span to the Links link
|
||||
linksLink.appendChild(statsLabel);
|
||||
} else {
|
||||
console.error("Error fetching Links stats:", data.error);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error("Error fetching Links stats:", error);
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -1,35 +0,0 @@
|
||||
import os
|
||||
import requests
|
||||
|
||||
IMMICH_API_KEY = os.getenv("IMMICH_API_KEY")
|
||||
|
||||
|
||||
def get_immich_stats(user_sub: str) -> dict[str, int | str]:
|
||||
"""
|
||||
Get the user's Immich stats from the API.
|
||||
"""
|
||||
if not IMMICH_API_KEY:
|
||||
return {"error": "IMMICH_API_KEY environment variable not set"}
|
||||
headers = {"x-api-key": IMMICH_API_KEY, "Accept": "application/json"}
|
||||
response = requests.get(
|
||||
"https://immich.woodburn.au/api/admin/users", headers=headers
|
||||
)
|
||||
if response.status_code != 200:
|
||||
return {"error": f"Failed to fetch Immich stats: {response.status_code}"}
|
||||
data = response.json()
|
||||
user_id = None
|
||||
for user in data:
|
||||
if user.get("oauthId") == user_sub:
|
||||
user_id = user.get("id")
|
||||
break
|
||||
if not user_id:
|
||||
return {"error": "User not found in Immich"}
|
||||
# Get user stats
|
||||
response = requests.get(
|
||||
f"https://immich.woodburn.au/api/admin/users/{user_id}/statistics",
|
||||
headers=headers,
|
||||
)
|
||||
if response.status_code != 200:
|
||||
return {"error": f"Failed to fetch Immich user stats: {response.status_code}"}
|
||||
stats = response.json()
|
||||
return stats
|
||||
23
tools/links.py
Normal file
23
tools/links.py
Normal file
@@ -0,0 +1,23 @@
|
||||
import os
|
||||
import requests
|
||||
|
||||
LINKS_API_KEY = os.getenv("LINKS_API_KEY")
|
||||
|
||||
|
||||
def get_links_stats(user_email: str) -> dict[str, int | str]:
|
||||
"""
|
||||
Get the user's Immich stats from the API.
|
||||
"""
|
||||
if not LINKS_API_KEY:
|
||||
return {"error": "LINKS_API_KEY environment variable not set"}
|
||||
headers = {"x-api-key": LINKS_API_KEY, "Accept": "application/json"}
|
||||
response = requests.get("https://l.woodburn.au/api/v2/users/admin", headers=headers)
|
||||
if response.status_code != 200:
|
||||
return {"error": f"Failed to fetch Links stats: {response.status_code}"}
|
||||
data = response.json()
|
||||
if not data.get("data"):
|
||||
return {"error": "User not found in Links"}
|
||||
for user in data["data"]:
|
||||
if user.get("email") == user_email:
|
||||
return user
|
||||
return {"error": "User not found in Links"}
|
||||
Reference in New Issue
Block a user