Spaces:
Paused
Paused
File size: 1,529 Bytes
9be5d6e b4bfea5 9be5d6e b4bfea5 9be5d6e | 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 38 39 40 41 42 43 44 45 46 47 48 | import asyncio
import os
import requests
def parse_github_repo_id(input_str):
if not input_str: return "", ""
input_str = input_str.strip().strip("/")
if "github.com/" in input_str:
import re
match = re.search(r'github\.com/([^/\s]+)/([^/\s.]+)', input_str)
if match:
repo = match.group(2)
if repo.endswith(".git"): repo = repo[:-4]
return match.group(1), repo
if "/" in input_str:
parts = input_str.split("/")
if len(parts) >= 2:
repo = parts[-1]
if repo.endswith(".git"): repo = repo[:-4]
return parts[-2], repo
return "", input_str.replace(".git", "")
async def fetch_readme(repo_url_input, branch="main"):
owner, repo = parse_github_repo_id(repo_url_input)
headers = {"Accept": "application/vnd.github.v3.raw"}
token = os.environ.get("GITHUB_TOKEN")
if token:
headers["Authorization"] = f"token {token}"
url = f"https://api.github.com/repos/{owner}/{repo}/contents/README.md?ref={branch}"
print(f"Fetching {url}...")
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.text
else:
print(f"Status: {response.status_code}, Text: {response.text[:100]}")
return ""
async def main():
content = await fetch_readme("langchain-ai/langchain", branch="master")
if content:
print("Success!")
else:
print("Failed.")
if __name__ == "__main__":
asyncio.run(main())
|