File size: 1,284 Bytes
a189123
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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.")