-
Notifications
You must be signed in to change notification settings - Fork 10
/
github.py
68 lines (59 loc) · 1.82 KB
/
github.py
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
67
68
import pandas as pd
import urllib.request # https://stackoverflow.com/a/41217363
import json
import time
def pull_raw_df(org_dict):
# Initialise a dataframe
df = pd.DataFrame()
# Pull GitHub data from the API (note we can only make 60 calls per hour so
# if we have over 60 orgs would have to try a different strategy)
for org_name, org_id in org_dict.items():
data = [1]
page = 1
while bool(data):
url = (
"https://api.github.com/orgs/"
+ org_id
+ "/repos?page="
+ str(page)
+ "&per_page=100"
)
response = urllib.request.urlopen(url)
data = json.loads(response.read())
flat_data = pd.json_normalize(data)
flat_data["org"] = org_name
df = pd.concat([df,flat_data])
page = page + 1
time.sleep(0.2) # Avoid unauthenticated requests limit (10 per sec)
return df
def tidy_raw_df(df):
# Add a column of 1s to sum for open_repos (this enables us to use sum() on
# all columns later)
df["open_repos"] = 1
# Filter and rename columns
df = df[
[
"org",
"owner.html_url",
"full_name",
"created_at",
"open_repos",
"stargazers_count",
"forks_count",
"open_issues_count",
"license.name",
"language",
"topics"
]
].rename(
columns={
"owner.html_url": "link",
"created_at": "date",
"stargazers_count": "stargazers",
"forks_count": "forks",
"open_issues_count": "open_issues",
"license.name": "license",
"topics": "topics"
}
)
return df