-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe-mail.py
executable file
·60 lines (46 loc) · 1.52 KB
/
e-mail.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
#!/usr/bin/env python
## By Davoud Arsalani
## https://github.com/davoudarsalani/scripts
## https://github.com/davoudarsalani/scripts/blob/master/e-mail.py
## https://raw.githubusercontent.com/davoudarsalani/scripts/master/e-mail.py
## https://davoudarsalani.ir
## https://stackoverflow.com/questions/953561/check-unread-count-of-gmail-messages-with-python
from imaplib import IMAP4_SSL
from os import getenv
from re import search
from sys import argv
from gp import set_widget, refresh_icon, save_error
arg = argv[1]
if arg == 'ymail':
prefix = 'YA'
server = 'imap.mail.yahoo.com'
username = getenv('email1')
password = getenv('email1_password2')
elif arg == 'gmail':
prefix = 'GM'
server = 'imap.gmail.com'
username = getenv('email2')
password = getenv('email2_password2')
else:
exit()
port = 993
dest_widget = arg
error_file = f'{getenv("HOME")}/main/scripts/.error/{dest_widget}'
set_widget(dest_widget, 'fg', 'reset')
set_widget(dest_widget, 'markup', refresh_icon())
try:
conn = IMAP4_SSL(server, port)
conn.login(username, password)
count = int(search('UNSEEN (\d+)', conn.status('INBOX', '(UNSEEN)')[1][0].decode('utf-8')).group(1))
if count > 0:
text = f'{prefix}:{count}'
set_widget(dest_widget, 'fg', getenv('red'))
else:
text = prefix
except Exception as exc:
text = prefix
## save error
save_error(error_file, f'{exc!r}')
set_widget(dest_widget, 'fg', getenv('red'))
finally:
set_widget(dest_widget, 'markup', text)