-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.py
executable file
·271 lines (234 loc) · 8.06 KB
/
bot.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/usr/bin/env python3
from PIL import Image
from dataclasses import dataclass
from datetime import datetime
import hashlib
import json
from random import choice
import requests
import smtplib
API_PREFIX = "https://beta.interieur.gouv.fr/candilib/api/v2/"
# API_PREFIX = "https://candilib.ynerant.fr/candilib/api/v2/"
# API_PREFIX = "http://localhost/candilib/api/v2/"
CAPTCHA_IMAGES = {
"L'avion": "airplane",
"Les ballons": "balloons",
"L'appareil photo": "camera",
"La Voiture": "car",
"Le chat": "cat",
"La chaise": "chair",
"Le trombone": "clip",
"L'horloge": "clock",
"Le nuage": "cloud",
"L'ordinateur": "computer",
"L'enveloppe": "envelope",
"L'oeil": "eye",
"Le drapeau": "flag",
"Le dossier": "folder",
"Le pied": "foot",
"Le graphique": "graph",
"La maison": "house",
"La clef": "key",
"La feuille": "leaf",
"L'ampoule": "light-bulb",
"Le cadenas": "lock",
"La loupe": "magnifying-glass",
"L'homme": "man",
"La note de musique": "music-note",
"Le pantalon": "pants",
"Le crayon": "pencil",
"L'imprimante": "printer",
"Le robot": "robot",
"Les ciseaux": "scissors",
"Les lunettes de soleil": "sunglasses",
"L'étiquette": "tag",
"L'arbre": "tree",
"Le camion": "truck",
"Le T-Shirt": "t-shirt",
"Le parapluie": "umbrella",
"La femme": "woman",
"La planète": "world"
}
def calculate_checksums():
for name in CAPTCHA_IMAGES.values():
img = Image.open(f'captcha_images/{name}.png')
img.save(f'captcha_images/{name}.ppm')
with open(f'captcha_images/{name}.ppm', 'rb') as f:
with open(f'captcha_images/{name}.ppm.sum', 'w') as f_sum:
f_sum.write(hashlib.sha512(f.read()).hexdigest())
@dataclass
class Candidat:
codeNeph: str = ''
homeDepartement: str = ''
departement: str = '75'
email: str = ''
nomNaissance: str = ''
prenom: str = ''
portable: str = ''
adresse: str = ''
visibilityHour: str = '12H50'
dateETG: str = ''
isInRecentlyDept: bool = False
@dataclass
class Places:
_id: str = ''
centre: "Centre" = None
date: str = ''
lastDateToCancel: str = ''
canBookFrom: str = ''
timeOutToRetry: int = 0
dayToForbidCancel: int = 7
visibilityHour: str = '12H50'
@dataclass(order=True)
class Departement:
geoDepartement: str = ''
centres: list["Centre"] = None
count: int = None
@dataclass
class Centre:
_id: str
count: int = 0
longitude: float = 0.0
latitude: float = 0.0
nom: str = ""
departement: Departement = None
def api(path: str, token: str, user_id: str, app: str = 'candidat', **kwargs) -> dict:
return requests.get(
API_PREFIX + app + '/' + path,
data=kwargs,
headers={
'Authorization': f'Bearer {token}',
'Content-Type': 'application/json',
'X-USER-ID': user_id,
},
).json()
def send_mail(content: str, subject: str) -> None:
print('\n')
print(subject)
print(len(subject) * '-')
print()
return print(content)
smtp = smtplib.SMTP('localhost', 25)
content = f"""From: Ÿnérant <[email protected]>
To: Ÿnérant <[email protected]>
Subject: {subject}
""" + content
content = content.encode('UTF-8')
smtp.sendmail('[email protected]', ['[email protected]'], content)
def main(token: str) -> None:
response = requests.get(API_PREFIX + 'auth/candidat/verify-token?token=' + token)
try:
assert response.json()['auth']
except (AssertionError, KeyError):
raise ValueError(f"Une erreur est survenue lors de la connexion : {response.content.decode('utf-8')}")
user_id = response.headers['X-USER-ID']
me = Candidat(**api('me', token, user_id)['candidat'])
print(f'Salut {me.prenom} {me.nomNaissance} !')
departements = [Departement(**dpt) for dpt in api('departements', token, user_id)['geoDepartementsInfos']]
departements.sort()
for dpt in departements:
centres = api(f'centres?departement={dpt.geoDepartement}&end=2021-07-31T23:59:59.999', token, user_id)
dpt.centres = []
dpt.count = 0
for centre in centres:
centre = Centre(
_id=centre['centre']['_id'],
nom=centre['centre']['nom'],
count=centre['count'],
longitude=centre['centre']['geoloc']['coordinates'][0],
latitude=centre['centre']['geoloc']['coordinates'][1],
departement=dpt,
)
dpt.centres.append(centre)
dpt.count += centre.count
places = Places(**api('places', token, user_id))
if places.centre:
for dpt in departements:
for centre in dpt.centres:
if centre._id == places.centre['_id']:
places.centre = centre
break
else:
continue
break
if places.date:
print(f"Vous avez déjà une date d'examen, le {places.date}.")
exit(1)
print("\n")
for dpt in departements:
print(dpt.geoDepartement, dpt.count)
for dpt in departements:
for centre in dpt.centres:
dates = api(f"places?begin=2021-04-26T00:00:00.000&end=2021-07-31T23:59:59.999+02:00&geoDepartement={dpt.geoDepartement}&nomCentre={centre.nom}", token, user_id)
send_mail(json.dumps(dates, indent=2), centre.nom)
centre.dates = dates
PREFERRED_CENTRES = ["MASSY", "ANTONY", "RUNGIS", "MONTGERON", "CLAMART", "SAINT CLOUD", "EVRY",
"VILLABE", "ETAMPES", "VELIZY VILLACOUBLAY", "MAISONS ALFORT", "TRAPPES", "SAINT PRIEST"]
for name in PREFERRED_CENTRES:
for dpt in departements:
for centre in dpt.centres:
if centre.nom == name:
break
else:
continue
break
for date in centre.dates:
if name == "SAINT PRIEST" and ("T07:" in date or "T08:" in date):
continue
break
else:
continue
break
else:
print("Aucune date intéressante")
return
print("Centre :", centre.nom)
print("Date :", date)
# Resolve captcha
captcha_info = api('verifyzone/start', token, user_id)
print(captcha_info)
captcha_info = captcha_info['captcha']
field = captcha_info['imageFieldName']
image_name = captcha_info['imageName']
image_file = CAPTCHA_IMAGES[image_name]
captcha_images = captcha_info['values']
with open(f'captcha_images/{image_file}.ppm.sum') as f:
valid_checksum = f.read().strip()
for i in range(5):
response = requests.get(
f'{API_PREFIX}candidat/verifyzone/image/{i}',
headers={
'Accept': 'application/json',
'Authorization': f'Bearer {token}',
'X-USER-ID': user_id,
},
)
with open(f'/tmp/image_{i}.png', 'wb') as f:
f.write(response.content)
img = Image.open(f'/tmp/image_{i}.png')
img.save(f'/tmp/image_{i}.ppm')
with open(f'/tmp/image_{i}.ppm', 'rb') as f:
checksum = hashlib.sha512(f.read()).hexdigest()
if checksum == valid_checksum:
captcha_result = captcha_images[i]
print(requests.patch(
API_PREFIX + 'candidat/places',
headers={
'Content-Type': 'application/json',
'Authorization': f'Bearer {token}',
'X-USER-ID': user_id,
},
json={
'geoDepartement': centre.departement.geoDepartement,
'nomCentre': centre.nom,
'date': date,
'hasDualControlCar': True,
'isAccompanied': True,
'isModification': False,
field: captcha_result,
},
).content)
if __name__ == '__main__':
with open('/var/local/candibot/.token') as f:
token = f.read().strip()
main(token)