-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
84 lines (61 loc) · 2.11 KB
/
main.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
import imaplib
import email
import re
from models import Message, session
from datetime import datetime
from dateutil import parser
class Imap:
mail = None
def __init__(self, date=None, origin=None, subject=None):
self.date = date
self.origin = origin
self.subject = subject
def get_date(self):
return self.date
def get_origin(self):
return self.origin
def get_subject(self):
return self.subject
def create_connection(self):
self.mail = imaplib.IMAP4_SSL('imap.gmail.com')
user_email, user_pass = self.get_user_info()
self.mail.login(user_email, user_pass)
self.mail.select("inbox")
def get_user_info(self):
user_email = raw_input('Informe seu email (@gmail.com) : ')
user_pass = raw_input('Informe sua senha : ')
return [user_email, user_pass]
def get_messages_list(self, word):
self.create_connection()
result, data = self.mail.search(None, '(OR BODY %s SUBJECT %s)' % (word, word))
return data[0].split()
def fetch_message(self, position):
result, data = self.mail.fetch(int(position), '(RFC822)')
return email.message_from_string(data[0][1])
def create_message_object(self, message):
return Imap(
date=message['Date'],
origin=email.utils.parseaddr(message['From'])[1],
subject=message['Subject']
)
def get_messages_by_body_subject(self, word):
collection = [
self.create_message_object(
self.fetch_message(i)
) for i in self.get_messages_list(word)
]
return collection
if __name__ == '__main__':
obj = Imap()
messages = obj.get_messages_by_body_subject('devops')
for m in messages:
try:
mess = Message()
data = parser.parse(m.get_date())
mess.date = data
mess.origin = m.get_origin()
mess.origin = m.get_subject()
session.add(mess)
session.commit()
except Exception as e:
print(e)