-
Notifications
You must be signed in to change notification settings - Fork 1
/
gmailnotification.py
executable file
·143 lines (121 loc) · 4.77 KB
/
gmailnotification.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python
import os
import sys
import dbus
import time
import urllib2
import subprocess
import ConfigParser
from bs4 import BeautifulSoup
from os.path import expanduser
item = "org.freedesktop.Notifications"
path = "/org/freedesktop/Notifications"
interface = "org.freedesktop.Notifications"
app_name = "Gmail Plugin"
id_num_to_replace = 0
icon = os.path.join(sys.path[0], 'gmail.png')
title = "Gmail"
actions_list = ''
hint = ''
bus = dbus.SessionBus()
notif = bus.get_object(item, path)
notify = dbus.Interface(notif, interface)
FEED_URL = 'https://mail.google.com/mail/feed/atom'
def ConfigSectionMap(section):
values = {}
options = Config.options(section)
for option in options:
try:
values[option] = Config.get(section, option)
if values[option] == -1:
DebugPrint("skip: %s" % option)
except:
print("exception on %s!" % option)
values[option] = None
return values
def internet_on():
return subprocess.call(['/bin/ping', '-c1', 'google.com'])
def updatecredentials():
home = expanduser("~")
homefile = os.path.join(home, '.gmailnotf.ini')
if os.path.exists(homefile):
updateconfig = ConfigParser.RawConfigParser()
updateconfig.read(homefile)
username = updateconfig.get(updateconfig.sections()[0], updateconfig.options(updateconfig.sections()[0])[0])
password = updateconfig.get(updateconfig.sections()[0], updateconfig.options(updateconfig.sections()[0])[1])
changeconfig = ConfigParser.RawConfigParser()
conpath = sys.path[0]
configpath = os.path.join(conpath, 'config.ini')
changeconfig.read(configpath)
changeconfig.sections()
changeconfig.set('SectionOne', 'username', username)
changeconfig.set('SectionOne', 'password', password)
with open(configpath, 'wb') as configfile:
changeconfig.write(configfile)
else:
return
class Gmailnotification:
def __init__(self, user, passwd, previousnumber):
self.getnumberofmessage(user, passwd, previousnumber)
def getnumberofmessage(self, user, passwd, previousnumber):
auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password(
realm='New mail feed',
uri='https://mail.google.com',
user='{user}@gmail.com'.format(user=user),
passwd=passwd
)
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
feed = urllib2.urlopen(FEED_URL)
self.parsingfullcount(feed, previousnumber)
def parsingfullcount(self, feed, previousnumber):
soup = BeautifulSoup(feed.read())
number = soup.fullcount.string
number = int(number)
unreadmessages = "You have %d unread mails" % int(number)
self.sendmessage(unreadmessages, number, previousnumber)
def sendmessage(self, message, number, previousnumber):
diff = int(number) - int(previousnumber)
if int(diff) == 0:
self.dontshowpopup(message, number, previousnumber)
else:
self.showpopup(number, message)
def dontshowpopup(self, message, number, previousnumber):
self.value = number
def showpopup(self, number, message):
nomessage = "No unread mails"
if number == 0:
text = nomessage
time = 5000
notify.Notify(app_name, id_num_to_replace, icon, title, text, actions_list, hint, time)
self.updateconfig(number)
else:
text = message
time = 5000
notify.Notify(app_name, id_num_to_replace, icon, title, text, actions_list, hint, time)
self.updateconfig(number)
def updateconfig(self, number):
self.cwd = sys.path[0]
self.basefile = os.path.join(self.cwd, 'config.ini')
self.editconfig = ConfigParser.RawConfigParser()
self.editconfig.read(self.basefile)
self.editconfig.set('SectionOne', 'previousnumber', number)
with open(self.basefile, 'wb') as configfile:
self.editconfig.write(configfile)
return
if __name__ == "__main__":
updatecredentials()
while True:
if internet_on() == 0:
cwd = sys.path[0]
basefile = os.path.join(cwd, 'config.ini')
Config = ConfigParser.ConfigParser()
Config.read(basefile)
user = ConfigSectionMap("SectionOne")['username']
passwd = ConfigSectionMap("SectionOne")['password']
previousnumber = ConfigSectionMap("SectionOne")['previousnumber']
d = Gmailnotification(user, passwd, previousnumber)
time.sleep(300)
else:
time.sleep(30)