-
Notifications
You must be signed in to change notification settings - Fork 0
/
gmail_checker.py
62 lines (49 loc) · 1.81 KB
/
gmail_checker.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
#!/usr/bin/env python
import sys
from gi.repository import Gtk, GObject
import appindicator
#from gi.repository import AppIndicator3 as appindicator
import imaplib
import re
PING_FREQUENCY = 10 # seconds
class CheckGMail:
def __init__(self):
self.ind = appindicator.Indicator("new-gmail-indicator",
"indicator-messages",
appindicator.CATEGORY_APPLICATION_STATUS)
self.ind.set_status(appindicator.STATUS_ACTIVE)
self.ind.set_attention_icon("new-messages-red")
self.menu_setup()
self.ind.set_menu(self.menu)
def menu_setup(self):
self.menu = Gtk.Menu()
self.quit_item = Gtk.MenuItem("Quit")
self.quit_item.connect("activate", self.quit)
self.quit_item.show()
self.menu.append(self.quit_item)
def main(self):
self.check_mail()
Gtk.timeout_add(PING_FREQUENCY * 1000, self.check_mail)
Gtk.main()
def quit(self, widget):
sys.exit(0)
def check_mail(self):
messages, unread = self.gmail_checker('myaddress@gmail.com','mypassword')
if unread > 0:
self.ind.set_status(appindicator.STATUS_ATTENTION)
else:
self.ind.set_status(appindicator.STATUS_ACTIVE)
return True
def gmail_checker(self, username, password):
i = imaplib.IMAP4_SSL('imap.gmail.com')
try:
i.login(username, password)
x, y = i.status('INBOX', '(MESSAGES UNSEEN)')
messages = int(re.search('MESSAGES\s+(\d+)', y[0]).group(1))
unseen = int(re.search('UNSEEN\s+(\d+)', y[0]).group(1))
return (messages, unseen)
except:
return False, 0
if __name__ == "__main__":
indicator = CheckGMail()
indicator.main()