72 lines
3.0 KiB
Python
72 lines
3.0 KiB
Python
import os
|
|
import requests
|
|
import traceback
|
|
import pprint
|
|
import datetime
|
|
|
|
# --- Configuration ---
|
|
GITEA_URL = "https://gitea.nathan-falvey.synology.me"
|
|
USERNAME = "nathan"
|
|
GITEA_TOKEN = os.getenv("GITEA_TOKEN")
|
|
|
|
def do_request(url, headers=None):
|
|
try:
|
|
response = requests.get(url, headers=headers)
|
|
response.raise_for_status() # Will raise an HTTPError for bad responses
|
|
return response.json()
|
|
except requests.exceptions.RequestException as e:
|
|
print(f"Request failed: {e}")
|
|
return None
|
|
|
|
def format_bytes(size_bytes):
|
|
"""Converts bytes to a human-readable string."""
|
|
if size_bytes == 0: return "0 B"
|
|
for unit in ['B', 'KB', 'MB', 'GB', 'TB']:
|
|
if size_bytes < 1024.0:
|
|
return f"{size_bytes:.2f} {unit}"
|
|
size_bytes /= 1024.0
|
|
return f"{size_bytes:.2f} PB"
|
|
|
|
def get_uptime():
|
|
"""Reads Linux system uptime."""
|
|
try:
|
|
with open("/proc/uptime", "r") as f:
|
|
seconds = float(f.readline().split()[0])
|
|
return str(datetime.timedelta(seconds=int(seconds)))
|
|
except: return "Running in Container"
|
|
|
|
def do_readme_build():
|
|
pass # Placeholder for the actual README build logic
|
|
|
|
# a simple function to test the API connection and print some debug information, this will be used for debugging purposes to ensure that the API connection is working correctly and to see what data is being returned from the API calls.
|
|
def debug_request():
|
|
headers = {"Authorization": f"token {GITEA_TOKEN}"}
|
|
version_info = do_request(f"{GITEA_URL}/api/v1/version", headers=headers)
|
|
version_string = version_info.get("version", "Unknown") if version_info else "Unknown"
|
|
if version_info:
|
|
print(f"Gitea Version: {version_string}")
|
|
print(f"System Uptime: {get_uptime()}")
|
|
try:
|
|
print(do_request(f"{GITEA_URL}/api/v1/version", headers=headers))
|
|
pprint.pprint(do_request(f"{GITEA_URL}/api/v1/user/repos?type=owner", headers=headers))
|
|
repos = do_request(f"{GITEA_URL}/api/v1/users/{USERNAME}/repos?type=owner", headers=headers)
|
|
if repos is not None:
|
|
print(f"Number of repos for user '{USERNAME}': {len(repos)}")
|
|
for repo in repos:
|
|
print(f"Repo Name: {repo.get('name', 'N/A')}, Repo ID: {repo.get('id', 'N/A')}")
|
|
if repo.get("has_code", False):
|
|
print(f"Repo '{repo.get('name', 'N/A')}' has code.")
|
|
print(f"Repo '{repo.get('name', 'N/A')}' is private: {repo.get('private', 'N/A')}")
|
|
print(f"Repo '{repo.get('name', 'N/A')}' is archived: {repo.get('archived', 'N/A')}")
|
|
print(f"Repo '{repo.get('name', 'N/A')}' has language: {repo.get('language', 'N/A')}")
|
|
print(f"Repo '{repo.get('name', 'N/A')}' has size: {format_bytes(repo.get('size', 0))}")
|
|
|
|
|
|
except Exception:
|
|
print("\n" + "!"*30)
|
|
print("STACK TRACE (Line Numbers):")
|
|
traceback.print_exc()
|
|
print("!"*30)
|
|
|
|
if __name__ == "__main__":
|
|
debug_request() |