forked from SubhrajitPrusty/codex-github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
userdata.py
137 lines (114 loc) · 3.44 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import os
import sys
import json
import requests
from loguru import logger
from dotenv import load_dotenv
from logentries import LogentriesHandler
load_dotenv()
cid = os.environ.get('CLIENT_ID')
csecret = os.environ.get('CLIENT_SECRET')
LOGENTRIES_TOKEN = os.environ.get('LOGENTRIES_TOKEN')
LH = LogentriesHandler(LOGENTRIES_TOKEN)
logger.add(LH, level='DEBUG', format='{name}:{function}:{line} - {message}')
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):
logger.debug("Name :", self.name)
logger.debug("Username :", self.username)
logger.debug("Avatar :", self.avatar)
logger.debug("Bio :", self.bio)
logger.debug("Public Repos :", self.nRepos)
logger.debug("Followers :", self.followers)
logger.debug("Following :", self.following)
logger.debug("Total Commits :", self.totalCommits)
@logger.catch
def getUser(self):
try:
payload = {
"client_id": cid,
"client_secret": csecret
}
USER_API = "https://api.github.com/users/{}".format(self.username)
r = requests.get(USER_API, params=payload)
logger.debug(f"{r} FETCHED {self.username} {USER_API}")
if r.status_code == 404:
raise NameError
elif r.status_code == 403:
raise Exception("Rate limit exceeded")
else:
userdata = json.loads(r.text)
self.avatar = userdata['avatar_url']
self.name = userdata['name'] if userdata['name'] is not None else self.username
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)
if r.status_code == 200:
logger.debug(f"{r} FETCHING {self.REPOS_URL}")
rep = r.json()
for rs in rep:
# logger.debug(rs['name'])
self.repos.append(rs['name'])
else:
logger.error(f'Request failed {r.reason}')
break
self.nRepos = len(self.repos)
logger.debug(f"no of repos : {len(self.repos)}")
except NameError:
logger.error("User not found")
except Exception as e:
logger.error("Error: ", e)
raise e
@logger.catch
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:
logger.debug(f'Request redirected, retrying: {STATS_URL}')
r = requests.get(STATS_URL, params=payload)
# logger.debug(r)
if r.status_code == 403:
logger.error("RATE LIMITED")
sys.exit(1)
if len(r.text) == 0:
logger.error(f'No response {STATS_URL}')
return 0
stats = r.json()
total = [0]
for st in stats:
if st['author']:
if st['author']['login'].lower() == self.username.lower():
total.append(int(st['total']))
break
logger.debug(total)
if sum(total) != 0:
return sum(total)
else:
return 0
def getAllCommits(self):
totalCont = [self.getRepoData(repo) for repo in self.repos]
self.totalCommits = sum(totalCont)