-
-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathgithub_rest_api.py
More file actions
39 lines (32 loc) · 1.13 KB
/
Copy pathgithub_rest_api.py
File metadata and controls
39 lines (32 loc) · 1.13 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
import requests
import os
def get_from_github(url, expected=0, pages=False):
token = os.environ.get('MY_GITHUB_TOKEN')
if not token:
print('Missing MY_GITHUB_TOKEN. Not collecting data from Github')
return
headers = {
'Accept': 'application/vnd.github+json',
'Authorization': f'Bearer {token}',
'X-GitHub-Api-Version': '2022-11-28',
}
if pages:
per_page = 100 # default is 30 max is 100
page = 1
all_data = []
while True:
real_url = f"{url}?per_page={per_page}&page={page}"
print(f"Fetching from {real_url}")
data = requests.get(real_url, headers=headers).json()
all_data.extend(data)
if expected:
print(f"Received {len(data)} Total {len(all_data)} out of an expected {expected}")
else:
print(f"Received {len(data)} Total {len(all_data)}")
page += 1
if len(data) < per_page:
break
else:
print(f"Fetching from {url}")
all_data = requests.get(url, headers=headers).json()
return all_data