import json from datetime import datetime, timezone from pathlib import Path from urllib.parse import urlparse import requests def build_registry_index(channels_file: Path, output_file: Path) -> dict: if not channels_file.exists(): return {} with open(channels_file, "r", encoding="utf-8") as f: channels = json.load(f) applets_catalog = {} for repo_url in channels: # Parse owner and repo name from url (e.g. https://git.luxhebros.com/beerslayer/lex.git) clean_url = repo_url.rstrip("/") if clean_url.endswith(".git"): clean_url = clean_url[:-4] parsed = urlparse(clean_url) parts = [p for p in parsed.path.split("/") if p] if len(parts) < 2: continue owner, repo_name = parts[-2], parts[-1] base_api_url = f"{parsed.scheme}://{parsed.netloc}/api/v1/repos/{owner}/{repo_name}/raw/manifest.json" try: resp = requests.get(base_api_url, timeout=5) if resp.status_code == 200: manifest_data = resp.json() applet_id = manifest_data.get("id", repo_name.lower()) manifest_data["repo_url"] = repo_url manifest_data["clone_url"] = f"{parsed.scheme}://{parsed.netloc}/{owner}/{repo_name}.git" manifest_data["download_url"] = f"{parsed.scheme}://{parsed.netloc}/{owner}/{repo_name}/archive/main.zip" manifest_data["owner"] = owner applets_catalog[applet_id] = manifest_data except Exception: pass index_data = { "schema_version": "1.0.0", "updated_at": datetime.now(timezone.utc).isoformat(), "total_applets": len(applets_catalog), "applets": applets_catalog, } output_file.parent.mkdir(parents=True, exist_ok=True) with open(output_file, "w", encoding="utf-8") as f: json.dump(index_data, f, indent=2, ensure_ascii=False) return index_data if __name__ == "__main__": base_dir = Path(__file__).resolve().parent.parent channels_path = base_dir / "registry" / "channels.json" index_path = base_dir / "registry" / "index.json" result = build_registry_index(channels_path, index_path) print(f"Registry index built successfully with {len(result.get('applets', {}))} applets.")