-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathget_github_data.py
More file actions
105 lines (94 loc) · 3.93 KB
/
get_github_data.py
File metadata and controls
105 lines (94 loc) · 3.93 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import json
import requests
import numpy as np
import pandas as pd
import requests
from requests.auth import HTTPBasicAuth
credentials = json.loads(open('credentials.json').read())
authentication = HTTPBasicAuth(credentials['username'], credentials['password'])
data = requests.get('https://api.github.com/users/' + credentials['username'], auth = authentication)
data = data.json()
print("Information about user {}:\n".format(credentials['username']))
print("Name: {}".format(data['name']))
print("Email: {}".format(data['email']))
print("Location: {}".format(data['location']))
print("Public repos: {}".format(data['public_repos']))
print("Public gists: {}".format(data['public_gists']))
print("About: {}\n".format(data['bio']))
print("Collecting repositories information")
url = data['repos_url']
page_no = 1
repos_data = []
while (True):
response = requests.get(url, auth = authentication)
response = response.json()
repos_data = repos_data + response
repos_fetched = len(response)
print("Total repositories fetched: {}".format(repos_fetched))
if (repos_fetched == 30):
page_no = page_no + 1
url = data['repos_url'] + '?page=' + str(page_no)
else:
break
repos_information = []
for i, repo in enumerate(repos_data):
data = []
data.append(repo['id'])
data.append(repo['name'])
data.append(repo['description'])
data.append(repo['created_at'])
data.append(repo['updated_at'])
data.append(repo['owner']['login'])
data.append(repo['license']['name'] if repo['license'] != None else None)
data.append(repo['has_wiki'])
data.append(repo['forks_count'])
data.append(repo['open_issues_count'])
data.append(repo['stargazers_count'])
data.append(repo['watchers_count'])
data.append(repo['url'])
data.append(repo['commits_url'].split("{")[0])
data.append(repo['url'] + '/languages')
repos_information.append(data)
repos_df = pd.DataFrame(repos_information, columns = ['Id', 'Name', 'Description', 'Created on', 'Updated on',
'Owner', 'License', 'Includes wiki', 'Forks count',
'Issues count', 'Stars count', 'Watchers count',
'Repo URL', 'Commits URL', 'Languages URL'])
print("Collecting language data")
for i in range(repos_df.shape[0]):
response = requests.get(repos_df.loc[i, 'Languages URL'], auth = authentication)
response = response.json()
if response != {}:
languages = []
for key, value in response.items():
languages.append(key)
languages = ', '.join(languages)
repos_df.loc[i, 'Languages'] = languages
else:
repos_df.loc[i, 'Languages'] = ""
print("Language data collection complete")
repos_df.to_csv('repos_info.csv', index = False)
print("Saved repositories information to repo_info.csv\n")
print("Collecting commits information")
commits_information = []
for i in range(repos_df.shape[0]):
url = repos_df.loc[i, 'Commits URL']
page_no = 1
while (True):
response = requests.get(url, auth = authentication)
response = response.json()
print("URL: {}, commits: {}".format(url, len(response)))
for commit in response:
commit_data = []
commit_data.append(repos_df.loc[i, 'Id'])
commit_data.append(commit['sha'])
commit_data.append(commit['commit']['committer']['date'])
commit_data.append(commit['commit']['message'])
commits_information.append(commit_data)
if (len(response) == 30):
page_no = page_no + 1
url = repos_df.loc[i, 'Commits URL'] + '?page=' + str(page_no)
else:
break
commits_df = pd.DataFrame(commits_information, columns = ['Repo Id', 'Commit Id', 'Date', 'Message'])
commits_df.to_csv('commits_info.csv', index = False)
print("Saved commits information to commits_info.csv")