This repository has been archived by the owner on May 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
github_api.py
98 lines (84 loc) · 3.36 KB
/
github_api.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
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
# Importing required libraries
import os
import sys
# sys.path.insert(1, os.path.join(os.path.dirname(os.path.realpath(__file__)), '..\\..\\'))
from k3y5 import DEV_GIT_KEY
import requests
import json
import itertools
import collections
import numpy as np
# Function to get information output in JSON format
def apiToJson(url):
payload = ''
headers = {
'Authorization': 'Bearer ' + DEV_GIT_KEY,
'Content-Type': 'application/json'
}
response = requests.request("GET", url, headers=headers, data = payload)
v = json.loads(response.text)
return (json.loads(response.text), response.headers)
# Function used to get Github data of the user
def getUserGitData(user):
try:
repoList = apiToJson("https://api.github.com/users/" + user + "/repos")[0]
repoLangData = []
repoContriData = []
for i in range (len(repoList)):
repoLangData.append (apiToJson(repoList[i]['languages_url'])[0])
repoContriData.append (apiToJson(repoList[i]["contributors_url"])[0])
thisUserResponse = apiToJson("https://api.github.com/users/"+user+"/events")
thisUser = thisUserResponse[0]
thisUserHeader = thisUserResponse[1]
if not (len(thisUser)>0):
print("Invalid Username")
return None
else:
last_event_url = thisUserHeader['Link'].split(', ')[1].split('>')[0][1:]
last_page_num = int(last_event_url.split('=')[-1])
lastPageActivity = apiToJson(last_event_url)[0]
thisUserContri = len(lastPageActivity) + (30 * (last_page_num - 1))
except:
print("ERR Fetching GitHub API Data")
return None
try:
sumThisUserCommits = 0
sumCommitPercent = 0
allLangList = []
for i in range(len(repoList)):
languageDict = repoLangData[i]
langList = list(languageDict.keys())
for lang in langList:
allLangList.append(lang)
allLangList = list(set(allLangList))
this_contri = repoContriData[i]
allCommits = 0
this_userCommits=0
for j in range(len(this_contri)):
allCommits = allCommits + this_contri[j]["contributions"]
if this_contri[j]["login"] == user:
this_userCommits = this_contri[j]["contributions"]
sumThisUserCommits = sumThisUserCommits + this_contri[j]["contributions"]
commit_percent = (this_userCommits / allCommits) * 100
sumCommitPercent = sumCommitPercent + commit_percent
gitStats = {
'avgContri': (sumCommitPercent / len(repoList)),
'langList': allLangList,
'recentContri': thisUserContri
}
return gitStats
except:
print("Invalid Username")
return None
# Function used to calculate the difference between two users regarding their average and recent contribution
def gitDistance(user1, user2):
try:
stat1 = getUserGitData(user1)
stat2 = getUserGitData(user2)
score = {
"avgContri": abs(stat1['avgContri'] - stat2['avgContri']),
"recentContri": abs(stat1['recentContri'] - stat2['recentContri'])
}
return (score['avgContri']/10 + score['recentContri']/100)/2
except:
return 0