-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathfetch_top_repos.py
More file actions
66 lines (47 loc) · 1.85 KB
/
Copy pathfetch_top_repos.py
File metadata and controls
66 lines (47 loc) · 1.85 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
57
58
59
60
61
62
63
64
65
66
import os
import requests
def get_top_repositories(username, token, top_n=5):
"""
Fetch the top N repositories for a user sorted by stargazers count.
"""
url = f"https://api.github.com/users/{username}/repos"
headers = {
"Authorization": f"token {token}",
"Accept": "application/vnd.github.v3+json"
}
params = {
"per_page": 100
}
all_repos = []
page = 1
while True:
params["page"] = page
response = requests.get(url, headers=headers, params=params)
if response.status_code != 200:
raise Exception(f"API Error {response.status_code}: {response.json().get('message')}")
repos = response.json()
if not repos:
break
all_repos.extend(repos)
page += 1
# Sort by stargazers_count in descending order
sorted_repos = sorted(all_repos, key=lambda x: x["stargazers_count"], reverse=True)
return sorted_repos[:top_n]
def main():
# Get credentials from environment variables
username = os.environ.get("GITHUB_USERNAME", "shinybrightstar")
token = os.environ.get("GITHUB_TOKEN")
if not token:
raise ValueError("GITHUB_TOKEN environment variable is not set!")
print(f"🔍 Fetching top 5 repositories for {username}...\n")
top_repos = get_top_repositories(username, token)
print(f"{'#':<4}{'Repository':<35}{'⭐ Stars':<12}{'Language':<15}{'Description'}")
print("=" * 100)
for i, repo in enumerate(top_repos, 1):
name = repo["name"][:33]
stars = repo["stargazers_count"]
language = repo["language"] or "N/A"
description = (repo["description"] or "No description")[:35]
print(f"{i:<4}{name:<35}{stars:<12}{language:<15}{description}")
if __name__ == "__main__":
main()