import re import requests MAX_SOURCE = 50000 def github_to_readme(url: str) -> str: url = url.strip() m = re.match(r"https?://github\.com/([^/]+)/([^/#?]+)", url) if not m: raise ValueError("Enter a valid public GitHub repository URL.") owner, repo = m.group(1), m.group(2).removesuffix(".git") api = f"https://api.github.com/repos/{owner}/{repo}/readme" r = requests.get(api, headers={"Accept": "application/vnd.github.raw+json"}, timeout=20) if r.ok: return r.text[:MAX_SOURCE] raw_candidates = [ f"https://raw.githubusercontent.com/{owner}/{repo}/HEAD/README.md", f"https://raw.githubusercontent.com/{owner}/{repo}/main/README.md", f"https://raw.githubusercontent.com/{owner}/{repo}/master/README.md", ] for candidate in raw_candidates: rr = requests.get(candidate, timeout=20) if rr.ok: return rr.text[:MAX_SOURCE] raise ValueError(f"Could not retrieve README from {url}.") def resolve_source(readme_text: str, github_url: str) -> str: if github_url.strip(): return github_to_readme(github_url) if readme_text.strip(): return readme_text.strip()[:MAX_SOURCE] raise ValueError("Provide a README/technical description or a GitHub URL.")