Implement Gitea data collection and README generation; enhance debug_request output

This commit is contained in:
2026-02-28 13:43:17 +00:00
parent 4f58ddfc98
commit 9ef07b5069

View File

@@ -35,7 +35,70 @@ def get_uptime():
return str(datetime.timedelta(seconds=int(seconds))) return str(datetime.timedelta(seconds=int(seconds)))
except: return "Running in Container" except: return "Running in Container"
# a function to collect all the necessary data from the Gitea API, this will be used to gather all the information needed to build the README file. This function will make multiple API calls to gather different pieces of information and then return it in a structured format that can be easily used to build the README file.
def collect_gitea_data():
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"
repos_info = do_request(f"{GITEA_URL}/api/v1/users/{USERNAME}/repos?type=owner", headers=headers)
repos = []
if repos_info is not None:
for repo in repos_info:
if repo.get("has_code", False):
repos.append({
"name": repo.get("name", "N/A"),
"id": repo.get("id", "N/A"),
"private": repo.get("private", False),
"archived": repo.get("archived", False),
"language": repo.get("language", "N/A"),
"size": repo.get("size", 0),
})
return {
"username": USERNAME,
"version": version_string,
"uptime": get_uptime(),
"repos": repos or []
}
def do_readme_parse():
data = collect_gitea_data() # does all the API calls and collects the data into a structured format, collected once to avoid multiple API calls during the README build process, this data will be used to populate the README template with the relevant information about the Gitea instance and the user's repositories.
storage_used = sum(repo["size"] for repo in data["repos"])
languages_used = set(repo["language"] for repo in data["repos"] if repo["language"] != "N/A")
md = f"# {data['username']}' Developer Hub\n\n"
md += f"## Welcome to {data['username']}'s Gitea Developer Hub! This is a collection of repositories and projects that I have created and maintained on my Gitea instance. Here you can find various projects that I have worked on, ranging from personal projects to open-source contributions.\n\n"
if data["repos"]:
md += f"### 📂 Repository Breakdown\n\n"
for repo in data["repos"]:
md += f"- [{repo['name']}]({GITEA_URL}/{USERNAME}/{repo['name']}) - {repo['language']}\n"
md += f"\n**Total Repositories:** {len(data['repos'])}\n\n"
md += f"**Total Storage Used:** {format_bytes(storage_used)}\n\n"
if languages_used:
md += f"**Languages Used:** {', '.join(languages_used)}\n\n"
md += "\n"
md += "### System Information\n\n"
md += f"**Gitea Version:** {data['version']}\n\n"
md += f"**System Uptime:** {data['uptime']}\n\n"
return md
def do_readme_build(): def do_readme_build():
try:
markdown = do_readme_parse().strip()
current_contents = open("README.md", "r", encoding="utf-8").read() if os.path.exists("README.md") else ""
if markdown == current_contents.strip():
print("README.md is already up to date. No changes made.")
return
with open("README.md", "w", encoding="utf-8") as f:
f.write(markdown)
print("README.md rebuilt successfully.")
except Exception as e:
print(f"Error: {e}")
pass # Placeholder for the actual README build logic 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. # 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.
@@ -59,7 +122,8 @@ def debug_request():
print(f"Repo '{repo.get('name', 'N/A')}' is private: {repo.get('private', 'N/A')}") 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')}' 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 language: {repo.get('language', 'N/A')}")
print(f"Repo '{repo.get('name', 'N/A')}' has size: {format_bytes(repo.get('size', 0))}") print(f"Repo '{repo.get('name', 'N/A')}' has size: {repo.get('size', 'N/A')}")
except Exception: except Exception:
@@ -69,4 +133,5 @@ def debug_request():
print("!"*30) print("!"*30)
if __name__ == "__main__": if __name__ == "__main__":
debug_request() #debug_request() # Uncomment this line to run the debug function and see the API responses and collected data
do_readme_build() # This will build the README file using the collected data from the Gitea API and the template defined in the do_readme_parse function.