-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfind_correction.py
executable file
·220 lines (198 loc) · 6.53 KB
/
find_correction.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import os, sys, requests
import time
import simplejson as json
import argparse
import pickle
import nltk
from operator import itemgetter
from progress.bar import ChargingBar
from tabulate import tabulate
def connection_api():
args = [
'grant_type=client_credentials',
'client_id=' + os.environ["FT42_UID"],
'client_secret=' + os.environ["FT42_SECRET"],
]
status = requests.post("https://api.intra.42.fr/oauth/token?%s" % ("&".join(args)))
token = status.json()
if not status.status_code == 200:
print("You are not connecting to the 42 API please check README.md")
sys.exit()
return status.json()
def clear():
sys.stdout.write("\033[F")
sys.stdout.write("\033[K")
def get_all_projects(token):
page = 0
lst_projects = []
while (1):
args = [
'access_token=%s' % (token['access_token']),
'token_type=bearer',
'page[size]=100',
'page[number]={}'.format(str(page)),
]
status = requests.get("https://api.intra.42.fr/v2/cursus/21/projects?%s" % ("&".join(args)))
if not status.status_code == 200:
print("Error during projects search.")
sys.exit()
response = status.json()
if not response:
break
lst_projects += [x['name'] for x in response]
page += 1
return lst_projects
def get_id_project(name_project, token):
print("Get id of the project")
clear()
args = [
'access_token=%s' % (token['access_token']),
'token_type=bearer',
'filter[name]=' + str(name_project),
]
status = requests.get("https://api.intra.42.fr/v2/projects?%s" % ("&".join(args)))
if not status.status_code == 200:
print("Error during project search.")
sys.exit()
response = status.json()
try:
return response[0]['id']
except:
lst_projects = get_all_projects(token)
print("Project {} not find. Please check your spelling.".format(name_project))
if lst_projects:
lst_project_sorted = sorted([[nltk.edit_distance(x, name_project), x] for x in lst_projects], key=itemgetter(0))
print("(Suggestion for 42cursus) The most similar projects are : \n")
#print(lst_project_sorted)
for prj in lst_project_sorted[:5]:
print(prj[1])
sys.exit()
def get_user_who_make_the_project(id, token, argument):
page = 0
all_user = get_buffer_file(argument)
if not all_user or argument.update:
print("Get all users who register the project {} (Can take some time the first time...)".format(argument.name_project))
bar = None
while (1):
args = [
'access_token=%s' % (token['access_token']),
'token_type=bearer',
'page[size]=100',
'page[number]={}'.format(str(page)),
]
status = requests.get("https://api.intra.42.fr/v2/projects/" + str(id) + "/projects_users?%s" % ("&".join(args)))
if not status.status_code == 200:
print("Error during projects users search.")
sys.exit()
if not bar:
bar = ChargingBar('Call API 42', max=(int(status.headers['X-Total']) // 100) + 2)
response = status.json()
if not response:
break
for projet in response:
for team in projet['teams']:
for user in team['users']:
all_user[user['login']] = [projet['status'], projet['validated?'], projet['final_mark']]
page += 1
bar.next()
time.sleep(1)
bar.finish()
clear()
clear()
create_buffer_file(argument, all_user)
return all_user
def create_buffer_file(args, people_we_want):
try:
with open(".{}.txt".format(args.name_project.replace(" ", "_")), "wb") as fp: #Pickling
pickle.dump(people_we_want, fp)
except:
print("Error")
exit(0)
def get_buffer_file(args):
b = {}
try :
with open(".{}.txt".format(args.name_project.replace(" ", "_")), "rb") as fp: # Unpickling
b = pickle.load(fp)
except:
pass
return b
def get_id_campus(arguments):
print("Get id of the campus")
clear()
args = [
'access_token=%s' % (token['access_token']),
'token_type=bearer',
'filter[name]=' + str(arguments.campus),
]
status = requests.get("https://api.intra.42.fr/v2/campus?%s" % ("&".join(args)))
if not status.status_code == 200:
print("Error during campus search.")
sys.exit()
response = status.json()
time.sleep(1)
try:
return response[0]['id']
except:
print("Campus {} not find. Please check your spelling.".format(arguments.campus))
sys.exit()
def get_all_people_connected(token, args):
all_user = []
page = 0
if args.campus == "Paris":
id_campus = 1
else:
id_campus = get_id_campus(args)
print("Get all peaple who is actually connected in {}".format(args.campus))
clear()
bar = None
while (1):
args = [
'access_token=%s' % (token['access_token']),
'token_type=bearer',
'page[size]=100',
'page[number]={}'.format(str(page)),
'filter[active]=true',
]
status = requests.get("https://api.intra.42.fr/v2/campus/" + str(id_campus) + "/locations?%s" % ("&".join(args)))
if not status.status_code == 200:
print("Error during people connected search.")
sys.exit()
if not bar:
bar = ChargingBar('Call API 42', max=(int(status.headers['X-Total']) // 100) + 2)
response = status.json()
if not response:
break
for poste in response:
all_user.append([poste['user']['login'], poste['host']])
page += 1
bar.next()
time.sleep(1)
bar.finish()
clear()
return all_user
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("name_project", help="Name of the project your search for correction", type=str)
parser.add_argument("--campus", help="Name of your Campus. Default is Paris", type=str, default="Paris")
parser.add_argument("--update", help="Update user who validate the current project.", action="store_true")
args = parser.parse_args()
if not os.environ["FT42_UID"] or not os.environ["FT42_SECRET"]:
print("You need to export environnement variable FT42_UID and FT42_SECRET. See README.md .")
sys.exit()
token = connection_api()
id_project = get_id_project(args.name_project, token)
people_we_want = get_user_who_make_the_project(id_project, token, args)
people_here = get_all_people_connected(token, args)
possible_corrector = []
for here in people_here:
if here[0] in people_we_want:
possible_corrector.append([here[0], str(people_we_want[here[0]][0]), str(people_we_want[here[0]][1]), str(people_we_want[here[0]][2]), here[1]])
possible_corrector_no_duplicate_value = [] # Remove duplicate value
for corrector in possible_corrector:
if not corrector[0] in [x[0] for x in possible_corrector_no_duplicate_value]:
possible_corrector_no_duplicate_value.append(corrector)
possible_corrector = possible_corrector_no_duplicate_value
if not possible_corrector:
print("No corrector found for this project")
else:
print(tabulate(possible_corrector, headers=['Login', 'Project status', 'Validated', 'Final Mark', 'Position']))