forked from SubhrajitPrusty/codex-github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
userdata.py
123 lines (99 loc) · 2.73 KB
/
userdata.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import requests
import json
import sys
import os
from dotenv import load_dotenv
load_dotenv()
cid = os.environ.get('CLIENT_ID')
csecret = os.environ.get('CLIENT_SECRET')
class Member():
def __init__(self, u):
self.username = u
self.name = ""
self.avatar = ""
self.bio = ""
self.followers = 0
self.following = 0
self.REPOS_URL = f"https://api.github.com/users/{u}/repos"
self.repos = []
self.nRepos = 0
self.totalCommits = 0
def fetch(self):
self.getUser()
self.getAllCommits()
def printData(self):
print("Name :", self.name)
print("Username :", self.username)
print("Avatar :", self.avatar)
print("Bio :", self.bio)
print("Public Repos :", self.nRepos)
print("Followers :", self.followers)
print("Following :", self.following)
print("Total Commits :", self.totalCommits)
def getUser(self):
payload = {
"client_id": cid,
"client_secret": csecret
}
USER_API = "https://api.github.com/users/{}".format(self.username)
r = requests.get(USER_API, params=payload)
print(r, f"FETCHING {self.username}", USER_API)
if r.status_code == 404:
print("USER NOT FOUND. CHECK LIST")
else:
userdata = json.loads(r.text)
self.avatar = userdata['avatar_url']
self.name = userdata['name']
self.REPOS_URL = userdata['repos_url']
self.nRepos = userdata['public_repos']
self.bio = userdata['bio']
self.followers = userdata['followers']
self.following = userdata['following']
payload['per_page'] = 100
page_count = (self.nRepos//100) + 1
for i in range(1,page_count+1):
payload['page'] = i
r = requests.get(self.REPOS_URL, params=payload)
print(r, f"FETCHING {self.REPOS_URL}")
rep = r.json()
for rs in rep:
print(rs['name'])
self.repos.append(rs['name'])
return self.avatar, self.name, self.REPOS_URL, self.repos, self.nRepos
def getRepoData(self, repo):
payload = {
"client_id": cid,
"client_secret": csecret
}
STATS_URL = "https://api.github.com/repos/{}/{}/stats/contributors".format(self.username, repo)
r = requests.get(STATS_URL, params=payload)
if r.status_code == 202:
r = requests.get(STATS_URL, params=payload)
# print(r)
if r.status_code == 403:
print("RATE LIMITED")
sys.exit(1)
if len(r.text) == 0:
return -1
stats = json.loads(r.text)
total = [0]
for st in stats:
if st['author']:
if st['author']['login'].lower() == self.username.lower():
total.append(int(st['total']))
break
if sum(total) != 0:
return sum(total)
else:
return -1
def getAllCommits(self):
totalCont = [0]
for rs in self.repos:
c = self.getRepoData(rs)
if c != -1:
totalCont.append(c)
self.totalCommits = sum(totalCont)
if self.totalCommits != 0:
return self.totalCommits
else:
return -1