From 8678711ab1b20dd40c94e382ec0b6eaae2f6a33e Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Tue, 17 Mar 2026 15:07:22 +1100 Subject: [PATCH] feat: Add Links stats --- server.py | 17 +++++++++++++++++ templates/assets/js/index.js | 22 ++++++++++++++++++++++ tools/adventure.py | 35 ----------------------------------- tools/links.py | 23 +++++++++++++++++++++++ 4 files changed, 62 insertions(+), 35 deletions(-) delete mode 100644 tools/adventure.py create mode 100644 tools/links.py diff --git a/server.py b/server.py index 67c995a..4e23d86 100644 --- a/server.py +++ b/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 diff --git a/templates/assets/js/index.js b/templates/assets/js/index.js index 7f647ea..d4c9c9b 100644 --- a/templates/assets/js/index.js +++ b/templates/assets/js/index.js @@ -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); + }); + } }); \ No newline at end of file diff --git a/tools/adventure.py b/tools/adventure.py deleted file mode 100644 index 56b0d56..0000000 --- a/tools/adventure.py +++ /dev/null @@ -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 diff --git a/tools/links.py b/tools/links.py new file mode 100644 index 0000000..62cf5db --- /dev/null +++ b/tools/links.py @@ -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"}