-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_api.py
More file actions
45 lines (32 loc) · 1.34 KB
/
Copy pathgithub_api.py
File metadata and controls
45 lines (32 loc) · 1.34 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
import requests
BASE_URL = "https://api.github.com"
HEADERS = {
"Accept": "application/vnd.github+json"
}
def get_user(username):
"""Fetch basic profile info for a GitHub user."""
url = f"{BASE_URL}/users/{username}"
response = requests.get(url, headers=HEADERS)
if response.status_code == 404:
return None, "User not found. Check the username and try again."
if response.status_code == 403:
return None, "GitHub API rate limit hit. Wait a minute and try again."
if response.status_code != 200:
return None, f"GitHub API error: {response.status_code}"
return response.json(), None
def get_repos(username):
"""Fetch all public repos for a GitHub user (up to 100)."""
url = f"{BASE_URL}/users/{username}/repos"
params = {"per_page": 100, "sort": "updated"}
response = requests.get(url, headers=HEADERS, params=params)
if response.status_code != 200:
return [], f"Could not fetch repos: {response.status_code}"
return response.json(), None
def get_events(username):
"""Fetch recent public activity/events for a GitHub user."""
url = f"{BASE_URL}/users/{username}/events/public"
params = {"per_page": 100}
response = requests.get(url, headers=HEADERS, params=params)
if response.status_code != 200:
return [], None
return response.json(), None