Spaces:
Sleeping
Sleeping
File size: 1,259 Bytes
545ce24 |
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 36 37 |
import aiohttp
from urllib.parse import urlparse
class GitHubFetcher:
def __init__(self):
self.base_url = "https://raw.githubusercontent.com"
def parse_github_url(self, url: str) -> tuple[str, str, str, str]:
"""Parse GitHub URL into components: owner, repo, branch, file_path."""
parsed = urlparse(url)
if not parsed.scheme:
raise ValueError("URL must include 'https://'")
path_parts = parsed.path.strip("/").split("/")
if len(path_parts) < 2:
raise ValueError("Invalid GitHub URL")
owner = path_parts[0]
repo = path_parts[1]
branch = "main"
file_path = "README.md"
return owner, repo, branch, file_path
async def fetch_readme(self, url: str) -> str:
try:
owner, repo, branch, file_path = self.parse_github_url(url)
raw_url = f"{self.base_url}/{owner}/{repo}/{branch}/{file_path}"
async with aiohttp.ClientSession() as session:
async with session.get(raw_url) as response:
response.raise_for_status()
return await response.text()
except Exception as e:
raise Exception(f"Failed to fetch README: {str(e)}") |