-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenge2.py
166 lines (149 loc) · 6.51 KB
/
challenge2.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
from __future__ import print_function
import pickle
import os.path
import json
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from apiclient import errors
import mysql.connector
import webbrowser
#connecting with mysql
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="Meli2019#"
)
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']
def ListMessagesMatchingQuery(service, user_id, query=''):
try:
response = service.users().messages().list(userId=user_id,
q=query).execute()
messages = []
if 'messages' in response:
messages.extend(response['messages'])
while 'nextPageToken' in response:
page_token = response['nextPageToken']
response = service.users().messages().list(userId=user_id, q=query,
pageToken=page_token).execute()
messages.extend(response['messages'])
return messages
except errors.HttpError as error:
print ('An error occurred: %s' % error)
def GetMessage(service, user_id, msg_id):
try:
message = service.users().messages().get(userId=user_id, id=msg_id).execute()
return message
except errors.HttpError as error:
print ('An error occurred: %s' % error)
def CreateDB(ndb):
try:
mycursor = mydb.cursor()
mycursor.execute("DROP DATABASE IF EXISTS devopsmails;") #Elimino la db "devosmails" en caso de que exista
mycursor.execute("CREATE DATABASE " + ndb )
mycursor.execute("USE " + ndb)
mycursor.execute("CREATE TABLE datos (subject VARCHAR(80), date VARCHAR (80), remitente VARCHAR (80));")
print ("La base de datos fue creada")
return mycursor
except:
print ("Error al crear la base de datos")
def SearchData (mycursor,service,userId,word):
try:
a =("")
b =("")
c =("")
idMails = ListMessagesMatchingQuery(service, userId, word) #se realiza la query
for id in idMails: #search data about from, date and subject
mensaje = GetMessage(service, userId, id["id"]) #obtejngo todos los ID de correos con la palabra que busco
for header in mensaje["payload"]["headers"]:
if header["name"] == "Subject":
a = header["value"]
if header["name"] == "Date":
b = header["value"]
if header["name"] == "From":
c = header["value"]
if a != "" and b != "" and c != "" :
try:
sql = ("INSERT INTO datos (subject, date, remitente) VALUES (%s,%s,%s)")
var = ( a [0:79] , b , c [0:79] ) #truncar datos para evitar problemas con la longitud de la base de datos
mycursor.execute(sql,var)
mydb.commit()
print("1 registro insertado")
a =("")
b =("")
c =("")
except: #ante algun error en la escritura va a continuar el proceso
print ("Error al insertar valores")
a =("")
b =("")
c =("")
except:
print ("cuenta de correo no valida, por favor verifique")
def words ():
try:
words = input ("Ingrese palabra a buscar y presione Enter (Por default la palabra a buscar sera DevOPS):\n")
if words == "":
words = "Devops"
return words
except:
return "Devops"
def db (): #se define una db persistente o no.
try:
db = input ("Si desea guardar la base de datos de manera permanente ingrese un nombre para la misma sino Enter (Por defualt la base de datos es Devopsmails): ")
if db == "":
db = "devopsmails"
return db
except:
return db
def UpdateCred (userId,lastmail): #modifico la cuenta de correo por default, ingreso al codigo y hago un swap de variables.
s = open("challenge2.py").read() #en la proxima ejecucion el default va a ser el ultimo ejecutado(y funcionado).
s = s.replace(lastmail,userId)
f = open("challenge2.py", 'w')
f.write(s)
f.close()
def main():
"""Shows basic usage of the Gmail API. Lists the user's Gmail labels."""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server()
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('gmail', 'v1', credentials=creds)
lastmail = "[email protected]" #cuenta de correo por default
userId = input ("Ingrese una cuenta de correo gmail o presiona enter para continuar con la cuenta por default ("+lastmail+"): ")
#print (lastmail)
if userId == "":
userId = lastmail
word = words()
ndb = db ()
print ("La palabra a buscar es: " + word)
print ("El nombre de la base de datos es: " + ndb)
mycursor = CreateDB(ndb + ";") #Se le agrega el ";" para que se ejecute el comando en sql
SearchData (mycursor,service,userId,word)
if userId != lastmail: #actualizar la cuenta por default
DefaultCred = input ('¿Confirma hacer esta cuenta "'+ userId+'" como default? y/n')
if DefaultCred == "y" or DefaultCred == "Y" or DefaultCred == "yes" or DefaultCred == "YES" or DefaultCred == "Yes":
UpdateCred (userId,lastmail)
if __name__ == '__main__':
try: #Verifico que esten las credenciales
s = open("credentials.json").read()
except:
input ("Falta descargar las credenciales y almacenarlas en la misma carpeta\nPresiona enter para acceder a la web y reiniciar el programa ")
webbrowser.open_new("https://developers.google.com/gmail/api/quickstart/python")
main()
os.remove ("token.pickle") #por motivo de seguridad borro el token
print ("Ejecucion terminada")