-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitHubApiCaller.py
184 lines (135 loc) · 4.92 KB
/
gitHubApiCaller.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/python3
import requests
import json
import sys
from bs4 import BeautifulSoup
BASE_URL = "https://api.github.com"
COMMITS_URL = BASE_URL + "/repos/{owner}/{repo}/commits"
SECRETS_URL = BASE_URL + "/search/code?q=token+user:{username}"
BASE_HEADER = {"Accept": "application/vnd.github.v3+json"}
TMP_AUTH_HEADER = BASE_HEADER.copy()
TMP_AUTH_HEADER["Authorization"] = "token " + "Token falso por si la lio y lo subo a git"
DEFAULT_SECRETS_FILENAME = "./secrets.csv"
class CommitedRepo():
def __init__(self, csvLine):
self.top = csvLine[0]
self.author = csvLine[1]
self.name = csvLine[2]
self.commits = 0
def setCommits(self, commits):
self.commits = commits
def __repr__(self):
return str(self.top) + "," + self.author + "," + self.name + "," + str(self.commits)
def __str__(self):
return str(self.top) + "," + self.author + "," + self.name + "," + str(self.commits)
def parseHeaders(page1Headers):
links = page1Headers["Link"]
lastLink = ' <https://api.github.com/repositories/253601257/commits?per_page=1&page=-1>; rel="last"'
for link in links.split(","):
if link.endswith('rel="last"'):
lastLink = link
totalCommits = link.split(">;")[0].split("&page=")[-1]
return totalCommits
def getTotalCommits(user, rep):
answer = requests.get(COMMITS_URL.format(owner=user, repo=rep)+"?per_page=1", BASE_HEADER)
if answer.status_code != 200:
return -1
return parseHeaders(answer.headers)
def ListToFile(lista, outputFile):
outputFile.write("Top,Author,Name,Commits\n")
for i in lista:
outputFile.write(str(i) + "\n")
def SecretsToFile(secrets, outputFile):
outputFile.write("Repository,SearchKey,File,Line,Value\n")
for s in secrets:
outputFile.write(str(s) + "\n")
def findSecrets(user):
answer = requests.get(SECRETS_URL.format(username=user), BASE_HEADER)
jsonValue = json.loads(answer.text)
listJson = jsonValue["items"]
secretsList = []
for m in listJson:
listOfTuples = getInfoFromCode(m['html_url'])
for t in listOfTuples:
secretsList.append(m["repository"]["name"] + "," + "Token" + "," + m["path"] + "," + t[0] + "," + t[1])
#print(secretsList)
return secretsList
def parseLine(td):
line = ""
for x in td:
if str(type(x)) == "<class 'bs4.element.Tag'>":
line += x.get_text()
else:
line += str(x)
return line
def getValueFromTd(td):
line = parseLine(td)
if "token" in line.lower():
return line
return None
def getInfoFromCode(linkUrl):
page = requests.get(linkUrl)
soup = BeautifulSoup(page.text, 'html.parser')
lineList = soup.find_all('tr')
okLines = []
for line in lineList:
numInfo, genInfo = line.find_all('td')
value = getValueFromTd(genInfo)
if value is not None:
okLines.append( (numInfo['data-line-number'], value) )
return okLines
def main(inputFile, outputFile, secretsFile):
#lee todas las líneas
lines = inputFile.readlines()
#Lista con los repos y su número de commits
repoList = []
#while puedas leer
for line in lines[1:]:
#remove end \n on each line
line = line.strip()
#Create class
repo = CommitedRepo(line.split(",")[:3])
#coge el número de commits
repo.setCommits( getTotalCommits(repo.author, repo.name) )
#mete en una lista l.append(repo)
repoList.append(repo)
#escribe la lista al archivo de salida
ListToFile(repoList, outputFile)
if secretsFile is not None:
secrets = findSecrets("afdezfraga")
SecretsToFile(secrets, secretsFile)
if __name__ == "__main__":
try:
secretFile = open(sys.argv[3], "x")
except:
secretsFile = None
with open(sys.argv[1]) as inputFile :
with open(sys.argv[2], "x") as outputFile:
main(inputFile, outputFile, secretsFile)
if secretsFile is not None:
secretFile.close()
### DBG
'''
ans1 = requests.get("https://api.github.com/search/repositories?q=user:afdezfraga", headers=BASE_HEADER)
ans2 = requests.get("https://api.github.com/search/repositories?q=user:afdezfraga", headers=TMP_AUTH_HEADER)
ans3 = requests.get("https://api.github.com/search/code?q=token+user:afdezfraga", headers=BASE_HEADER)
print(json.dumps(json.loads(ans1.text), indent=4))
for i in range(10):
print("----------------------")
print(json.dumps(json.loads(ans2.text), indent=4))
'''
#NOTESE la diferencia en total_count
#la primera petición me devuelve
#{
# "total_count": 3,
# "incomplete_results": false,
# "items": [
# ...
#
#la segunda petición me devuelve
#{
# "total_count": 7,
# "incomplete_results": false,
# "items": [
# ...
#