-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.py
executable file
·70 lines (57 loc) · 2.04 KB
/
service.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
#!/usr/bin/env python3
import gi
gi.require_version('Notify', '0.7')
from gi.repository import Notify, GdkPixbuf
from imapclient import IMAPClient
import time
import subprocess
DEBUG = True
HOSTNAME = 'imap.gmail.com'
USERNAME = ''
PASSWORD = ''
MAILBOX = 'Inbox'
NEWMAIL_OFFSET = 0
MAIL_CHECK_FREQ = 2
NOTIFY_ON_NO_INT_CONN = True
NOTIFY_IMAGE = '/home/chaitanya/Documents/Github/email-notifier/favicon.ico'
def notifyUser(emailCount):
global NOTIFY_IMAGE
Notify.init("Email Notifier")
emailNotification = Notify.Notification.new("INIT", "INIT", NOTIFY_IMAGE)
# emailNotification.show()
if DEBUG:
print("Notifying")
checkTag = 'mail' if emailCount == 1 else 'mails'
headTag = "Hey there! New " + checkTag + " for you"
bodyTag = "You have " + str(emailCount) + " new " + checkTag + " in your mailbox @chavalichaithanyachinna"
emailNotification.update(headTag, bodyTag, NOTIFY_IMAGE)
emailNotification.show()
time.sleep(5)
Notify.uninit()
def startService():
try:
while True:
try:
gServer = IMAPClient(HOSTNAME, use_uid=True, ssl=True)
gServer.login(USERNAME, PASSWORD)
if DEBUG:
print('Logged in as ' + USERNAME)
select_info = gServer.select_folder(MAILBOX)
print('%d messages in INBOX' % select_info[b'EXISTS'])
folder_status = gServer.folder_status(MAILBOX, 'UNSEEN')
newmails = int(folder_status[b'UNSEEN'])
if DEBUG:
print("You have %d new emails" % newmails)
if newmails > 0:
notifyUser(newmails)
time.sleep(MAIL_CHECK_FREQ)
except Exception as e:
if NOTIFY_ON_NO_INT_CONN:
print(e)
else:
pass
except KeyboardInterrupt:
print("Service being closed by user")
# http://ibiblio.org/g2swap/byteofpython/read/module-name.html
if __name__ == '__main__':
startService()