-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActualizarBD.py
369 lines (343 loc) · 13.9 KB
/
ActualizarBD.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import mysql.connector
from mysql.connector import errorcode
from genregex import getRegexId
import re
import string
import itertools
import time
separador = " # "
def ConectarBaseDeDatos():
cnx = ""
curs = ""
try:
cnx = mysql.connector.connect(user='obtenerRegex', password='', host='',
database='Validator', auth_plugin='mysql_native_password')
curs = cnx.cursor()
print("Conectado a la BD")
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
cnx.close()
return cnx, curs
def InsertBD():
cnx = ""
curs =""
try:
cnx = mysql.connector.connect(user='addCorreo', password='', host='', database='Validator', auth_plugin='mysql_native_password')
curs = cnx.cursor()
print("Conectado a la BD")
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
cnx.close()
return cnx, curs
def getRegexMSGID(regex, valores):
aux = []
for line in valores:
if re.match(regex, line) is not None:
aux.append(line)
for ele in aux:
valores.remove(ele)
return valores
def MSGID(line):
valores = []
regexes = []
query = f"SELECT MSGID FROM Validator.Coleccion WHERE FROMM = \"{line}\" "
curs.execute(query)
resultadosFROMM = curs.fetchall()
for linea in resultadosFROMM:
linea = linea[0]
valores.append(linea.replace("]","").replace("[",""))
while len(valores) > 0:
regex = getRegexId(valores)
valores = getRegexMSGID(regex, valores)
regexes.append(regex)
stringa = ""
for a in list(dict.fromkeys(regexes)):
stringa += a + separador
stringa = stringa[:-3]
return stringa
def getRegexReceived(FROMM, Received):
global valores
valores = []
query = f"SELECT {Received} FROM Validator.Coleccion WHERE FROMM = \"{FROMM}\" "
curs.execute(query)
resultados = curs.fetchall()
auxfrom = []
auxby = []
regexes = []
for line in resultados:
if line[0] is not None:
linea = line[0].split(" ")
for i,ele in enumerate(linea):
if ele == "from":
auxfrom.append(linea[i+1].replace("[","").replace("]",""))
if ele == "by" and not linea[i+1] == "mx.google.com":
auxby.append(linea[i+1].replace("[","").replace("]",""))
valores = auxfrom
while len(valores) > 0:
regex = getRegexId(valores)
valores = getRegexMSGID(regex, valores)
regexes.append(regex)
valores = auxby
while len(valores) > 0:
regex = getRegexId(valores)
valores = getRegexMSGID(regex, valores)
regexes.append(regex)
stringa = ""
for a in list(dict.fromkeys(regexes)):
stringa += a + separador
stringab = stringa[:-3]
regexprimer = stringab
return regexprimer
def AuthCantidad(FROMM):
query = f"SELECT AuthenticationResult FROM Validator.Coleccion WHERE FROMM = \"{FROMM}\" "
curs.execute(query)
resultados = curs.fetchall()
max = 0
min = 100
for line in resultados:
pattern = "(pass)"
a = re.findall(pattern, line[0])
actual = len(a)
if actual > max:
max = actual
if actual < min:
min = actual
if min == " ":
min = 0
valor = str(max) + separador + str(min)
return valor
def get_UTC(FROMM):
query = f"SELECT PrimerReceived FROM Validator.Coleccion WHERE FROMM = \"{FROMM}\" "
curs.execute(query)
resultados = curs.fetchall()
aux = []
for line in resultados:
linea = line[0].split(" ")
del linea[0]
maximo = len(linea)
for i ,ele in enumerate(linea):
pattern = "[\-|\+]\d\d\d\d"
if re.fullmatch(pattern, ele):
if i+1 < maximo:
pattern = "(\(\w\w\w\))"
if re.match(pattern,linea[i+1]):
valor = ele + linea[i+1]
aux.append(valor)
else:
aux.append(ele)
valor = ""
for ele in list(dict.fromkeys(aux)):
valor += ele + separador
valor = valor[:-3]
return valor
def get_Regexes(FROMM):
regexMessageID = MSGID(FROMM)
auth = AuthCantidad(FROMM)
UTC = get_UTC(FROMM)
primerR = getRegexReceived(FROMM, "PrimerReceived")
penUlti = getRegexReceived(FROMM, "PenultimoReceived")
return regexMessageID, auth, UTC, primerR, penUlti
def obtener_Valores(correos):
tmsgid = 0
tprimero = 0
tpen = 0
tutc = 0
tauth = 0
for i, ele in enumerate(correos[0]):
if i == 0:
rgexesMSGID = ele.split(separador)
qry = f"SELECT MSGID FROM Coleccion WHERE Fromm = \"{line[0]}\""
CURSSS.execute(qry)
valoresMSGID = CURSSS.fetchall()
for elemento in valoresMSGID:
for regex in rgexesMSGID:
if re.match(regex, elemento[0].replace("[","").replace("]","")):
tmsgid += 1
break
elif i == 2:
primero = ele.split(separador)
qry = f"SELECT PrimerReceived FROM Coleccion WHERE Fromm = \"{line[0]}\""
CURSSS.execute(qry)
valoresPrimero = CURSSS.fetchall()
for elemento in valoresPrimero:
for regex in primero:
if re.findall(regex, elemento[0]):
tprimero += 1
break
elif i == 3:
if ele != "":
pen = ele.split(separador)
qry = f"SELECT PenultimoReceived FROM Coleccion WHERE Fromm = \"{line[0]}\""
CURSSS.execute(qry)
valoresPrimero = CURSSS.fetchall()
for elemento in valoresPrimero:
if elemento[0] is not None:
for regex in pen:
if re.findall(regex, elemento[0]):
tpen += 1
break
else:
tpen += 1
else:
tpen = tprimero
elif i == 4:
if ele != "":
utc = ele.split(separador)
qry = f"SELECT AuthenticationResult FROM Coleccion WHERE Fromm = \"{line[0]}\""
CURSSS.execute(qry)
valoresAUTH = CURSSS.fetchall()
for elemento in valoresAUTH:
pattern = "([\-|\+]\d\d\d\d|(\(\w\w\w\)))"
if re.findall(pattern, elemento[0]):
for regex in utc:
try:
regex = "[" + regex + "]"
if re.findall(regex, elemento[0]):
tutc += 1
break
except:
regex = "(" + regex + ")"
if re.findall(regex, elemento[0]):
tutc += 1
break
else:
tutc += 1
else:
tutc += tpen
elif i == 5:
auth = ele.split(separador)
qry = f"SELECT AuthenticationResult FROM Coleccion WHERE Fromm = \"{line[0]}\""
CURSSS.execute(qry)
valoresAUTH = CURSSS.fetchall()
for elemento in valoresAUTH:
maxi = int(auth[0])
mini = int(auth[1])
pattern = "(pass)"
a = re.findall(pattern, elemento[0])
if int(len(a)) in range(mini, maxi + 1):
tauth += 1
if tmsgid == tprimero == tpen == tutc == tauth:
qquery = f"DELETE FROM Coleccion WHERE Fromm = \"{line[0]}\""
CURSSS.execute(qquery)
CNXXX.commit()
return tmsgid, tprimero, tpen, tutc, tauth
cnx, curs = ConectarBaseDeDatos()
CNXXX, CURSSS = ConectarBaseDeDatos()
query = f"SELECT DISTINCT FROMM FROM Validator.Coleccion WHERE FROMM IN (SELECT DISTINCT FROMM FROM Validator.Coleccion) "
#query = f"SELECT FROMM FROM Validator.Coleccion WHERE FROMM = \"[email protected]\""
curs.execute(query)
resultados = curs.fetchall()
CNXX, CURSS = InsertBD()
maxlen = 0
for i, line in enumerate(resultados):
valores = []
FROMM = line[0]
qry = f"SELECT * FROM Validator.Correo WHERE Fromm = \"{FROMM}\""
CURSSS.execute(qry)
correos = CURSSS.fetchall()
if correos:
tmsgid, tprimero, tpen, tutc, tauth = obtener_Valores(correos)
if not tmsgid == tprimero == tpen == tutc == tauth:
maxi = max(tmsgid, tprimero, tpen, tutc, tauth)
if maxi != tmsgid:
qry = f"SELECT RegexID FROM Correo WHERE Fromm = \"{FROMM}\""
curs.execute(qry)
msgidBD = curs.fetchall()
msgidBD = msgidBD[0][0].split(separador)
regexmsgid = MSGID(FROMM).split(separador)
aux = ""
for ele in regexmsgid:
msgidBD.append(ele)
for ele in list(dict.fromkeys(msgidBD)):
aux += ele + separador
msgidBD = aux[:-3]
query = f"UPDATE Correo SET RegexID = \"{msgidBD}\" WHERE FROMM = \"{FROMM}\""
curs.execute(query)
cnx.commit()
if maxi != tprimero:
qry = f"SELECT Primero FROM Correo WHERE Fromm = \"{FROMM}\""
curs.execute(qry)
primerReceivedBD = curs.fetchall()
primerReceivedBD = primerReceivedBD[0][0].split(separador)
regexprimer = getRegexReceived(FROMM, "PrimerReceived").split(separador)
aux = ""
for ele in regexprimer:
primerReceivedBD.append(ele)
for ele in list(dict.fromkeys(primerReceivedBD)):
aux += ele + separador
primerReceivedBD = aux[:-3]
query = f"UPDATE Correo SET Primero = \"{primerReceivedBD}\" WHERE FROMM = \"{FROMM}\""
curs.execute(query)
cnx.commit()
if maxi != tpen:
qry = f"SELECT Penultimo FROM Correo WHERE Fromm = \"{FROMM}\""
curs.execute(qry)
penultimoReceivedBD = curs.fetchall()
if not penultimoReceivedBD[0][0] == "":
penultimoReceivedBD = penultimoReceivedBD[0][0].split(separador)
regexpen = getRegexReceived(FROMM, "PenultimoReceived").split(separador)
aux = ""
for ele in regexpen:
penultimoReceivedBD.append(ele)
for ele in list(dict.fromkeys(penultimoReceivedBD)):
aux = ele + separador
penultimoReceivedBD = aux[:-3]
query = f"UPDATE Correo SET Penultimo = \"{penultimoReceivedBD}\" WHERE FROMM = \"{FROMM}\""
curs.execute(query)
cnx.commit()
if maxi != tutc:
qry = f"SELECT UTC FROM Correo WHERE Fromm = \"{FROMM}\""
curs.execute(qry)
utcBD = curs.fetchall()
if not utcBD[0][0] == "":
utcBD = utcBD[0][0].split(separador)
regexutcBD =get_UTC(FROMM).split(separador)
aux = ""
for ele in regexutcBD:
utcBD.append(ele)
for ele in list(dict.fromkeys(utcBD)):
aux = ele + separador
utcBD = aux[:-3]
query = f"UPDATE Correo SET UTC = \"{utcBD}\" WHERE FROMM = \"{FROMM}\""
curs.execute(query)
cnx.commit()
if maxi != tauth:
qry = f"SELECT AUTH FROM Correo WHERE Fromm = \"{FROMM}\""
curs.execute(qry)
authBD = curs.fetchall()
authBD = authBD[0][0].split(separador)
auth = AuthCantidad(FROMM)
auth = auth.split(separador)
aux = ""
if int(auth[0]) >= int(authBD[0]):
authBD[0] = auth[0]
if authBD[1] != " ":
if int(auth[1]) <= int(authBD[1]):
authBD[1]= auth[1]
else:
authBD[1] = str(0)
for ele in authBD:
aux += ele + separador
authBD = aux[:-3]
query = f"UPDATE Correo SET AUTH = \"{authBD}\" WHERE FROMM = \"{FROMM}\""
curs.execute(query)
cnx.commit()
else:
regexMessageID, auth, UTC, primerR, penUlti = get_Regexes(FROMM)
try:
query = f"INSERT INTO Validator.Correo (RegexID, Fromm, Primero, Penultimo, UTC, AUTH) VALUES ( %s, %s, %s, %s, %s, %s) "
values = (regexMessageID,FROMM, primerR, penUlti, UTC, auth)
CURSS.execute(query, values)
CNXX.commit()
except mysql.connector.Error as error:
print(error)