-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_email.py
46 lines (33 loc) · 1.13 KB
/
read_email.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
from os import getenv
from time import sleep
from imap_tools import AND, MailBox
from post_tweet import twitter_connect
attempts = 0
def mail_connect():
mailbox = MailBox(getenv('IMAP')).login(getenv('MAIL'), getenv('PASS'), initial_folder='INBOX')
print('\nIMAP: conexão bem-sucedida!')
check_email(mailbox)
def check_email(mailbox):
read_mail(mailbox) if mailbox.uids() else trials()
def read_mail(mailbox):
posts = []
for msg in mailbox.fetch(AND(from_='[email protected]')):
posts = prepare_mail(msg) # TODO: trabalhar com 'msg.html' futuramente
archive_message(mailbox, msg.uid)
twitter_connect(posts)
def prepare_mail(msg):
posts = msg.text.replace('*', '').split('\r\n\r\n')
return posts[1:-3]
def archive_message(mailbox, msg_uid):
mailbox.move(msg_uid, 'Tweeted')
print('E-mail arquivado.')
def trials():
global attempts
attempts += 1
print('\nNenhum e-mail encontrado.', end=' ')
if attempts == 3:
print('Tentativas excedidas.')
return
print('Tentando novamente em 15 minutos...')
sleep(900)
mail_connect()