-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch-repo-json.py
More file actions
56 lines (42 loc) · 1.45 KB
/
fetch-repo-json.py
File metadata and controls
56 lines (42 loc) · 1.45 KB
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
49
50
51
52
53
54
55
56
import sys
import os
import glob
import requests
REPO_DIR = "repo"
def clean_repo_dir():
json_files = glob.glob(os.path.join(REPO_DIR, "*.json"))
for path in json_files:
try:
os.remove(path)
except Exception as e:
print(f"# Failed to remove {path}: {e}", file=sys.stderr)
def fetch_and_save(repo_url):
if not repo_url.startswith("https://github.com/"):
return
parts = repo_url.strip().removeprefix("https://github.com/").split("/")
if len(parts) < 2:
return
user, repo = parts[:2]
for branch in ["main", "master"]:
raw_url = f"https://raw.githubusercontent.com/{user}/{repo}/{branch}/repo.json"
try:
response = requests.get(raw_url, timeout=5)
if response.status_code == 200:
filename = f"{repo}@{user}.json"
path = os.path.join(REPO_DIR, filename)
with open(path, "w", encoding="utf-8") as f:
f.write(response.text)
print(f"# Saved: {path}", file=sys.stderr)
return
except Exception as e:
print(f"# Error fetching {raw_url}: {e}", file=sys.stderr)
print(f"# repo.json not found: {repo_url}", file=sys.stderr)
def main():
clean_repo_dir()
for line in sys.stdin:
repo_url = line.strip()
if not repo_url:
continue
fetch_and_save(repo_url)
if __name__ == "__main__":
main()