feat: Add nextcloud and immich api integration
This commit is contained in:
35
tools/adventure.py
Normal file
35
tools/adventure.py
Normal file
@@ -0,0 +1,35 @@
|
||||
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
|
||||
49
tools/cloud.py
Normal file
49
tools/cloud.py
Normal file
@@ -0,0 +1,49 @@
|
||||
import os
|
||||
import requests
|
||||
|
||||
login = os.getenv("WOODBURN_USER")
|
||||
|
||||
|
||||
def getUserQuota(user: str) -> dict[str, int | str]:
|
||||
"""
|
||||
Get the user's quota information from the environment variable.
|
||||
Returns a dictionary with 'used' and 'total' as integers.
|
||||
"""
|
||||
headers = {"OCS-APIRequest": "true", "Accept": "application/json"}
|
||||
if not login:
|
||||
return {
|
||||
"used": 0,
|
||||
"total": 5,
|
||||
"percentage": "0.0",
|
||||
"error": "WOODBURN_USER environment variable not set",
|
||||
}
|
||||
# curl -u user https://cloud.woodburn.au/ocs/v1.php/cloud/users/nathan OCS-APIRequest:true
|
||||
response = requests.get(
|
||||
f"https://cloud.woodburn.au/ocs/v1.php/cloud/users/{user}",
|
||||
headers=headers,
|
||||
auth=(login.split(":")[0], login.split(":")[1]),
|
||||
)
|
||||
if response.status_code != 200:
|
||||
return {
|
||||
"used": 0,
|
||||
"total": 5,
|
||||
"percentage": "0.0",
|
||||
"error": f"Failed to fetch quota: {response.status_code}",
|
||||
}
|
||||
data = response.json()
|
||||
# If the request failed
|
||||
if data.get("ocs", {}).get("meta", {}).get("status") != "ok":
|
||||
return {
|
||||
"used": 0,
|
||||
"total": 5,
|
||||
"percentage": "0.0",
|
||||
"error": data.get("ocs", {})
|
||||
.get("meta", {})
|
||||
.get("message", "Unknown error"),
|
||||
}
|
||||
|
||||
quota = data.get("ocs", {}).get("data", {}).get("quota", {})
|
||||
# Convert to GB
|
||||
used = int(quota.get("used", 0)) // (1024 * 1024 * 1024)
|
||||
total = int(quota.get("total", 0)) // (1024 * 1024 * 1024)
|
||||
return {"used": used, "total": total, "percentage": quota.get("relative", "0.0")}
|
||||
35
tools/immich.py
Normal file
35
tools/immich.py
Normal file
@@ -0,0 +1,35 @@
|
||||
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
|
||||
Reference in New Issue
Block a user