Various bug fixes
[gc-dialer] / src / alarm_notify.py
1 #!/usr/bin/env python
2
3 import os
4 import filecmp
5 import ConfigParser
6 import pprint
7
8 import constants
9 import gv_backend
10
11
12 def get_missed(backend):
13         missedPage = backend._browser.download(backend._missedCallsURL)
14         missedJson = backend._grab_json(missedPage)
15         return missedJson
16
17
18 def get_voicemail(backend):
19         voicemailPage = backend._browser.download(backend._voicemailURL)
20         voicemailJson = backend._grab_json(voicemailPage)
21         return voicemailJson
22
23
24 def get_sms(backend):
25         smsPage = backend._browser.download(backend._smsURL)
26         smsJson = backend._grab_json(smsPage)
27         return smsJson
28
29
30 def remove_reltime(data):
31         for messageData in data["messages"].itervalues():
32                 del messageData["relativeStartTime"]
33
34
35 def is_type_changed(backend, type, get_material):
36         jsonMaterial = get_material(backend)
37         unreadCount = jsonMaterial["unreadCounts"][type]
38
39         previousSnapshotPath = os.path.join(constants._data_path_, "snapshot_%s.old.json" % type)
40         currentSnapshotPath = os.path.join(constants._data_path_, "snapshot_%s.json" % type)
41
42         try:
43                 os.remove(previousSnapshotPath)
44         except OSError, e:
45                 # check if failed purely because the old file didn't exist, which is fine
46                 if e.errno != 2:
47                         raise
48         try:
49                 os.rename(currentSnapshotPath, previousSnapshotPath)
50                 previousExists = True
51         except OSError, e:
52                 # check if failed purely because the new old file didn't exist, which is fine
53                 if e.errno != 2:
54                         raise
55                 previousExists = False
56
57         remove_reltime(jsonMaterial)
58         textMaterial = pprint.pformat(jsonMaterial)
59         currentSnapshot = file(currentSnapshotPath, "w")
60         try:
61                 currentSnapshot.write(textMaterial)
62         finally:
63                 currentSnapshot.close()
64
65         if unreadCount == 0 or not previousExists:
66                 return False
67
68         seemEqual = filecmp.cmp(previousSnapshotPath, currentSnapshotPath)
69         return not seemEqual
70
71
72 def is_changed():
73         gvCookiePath = os.path.join(constants._data_path_, "gv_cookies.txt")
74         backend = gv_backend.GVDialer(gvCookiePath)
75
76         loggedIn = False
77
78         if not loggedIn:
79                 loggedIn = backend.is_authed()
80
81         config = ConfigParser.SafeConfigParser()
82         config.read(constants._user_settings_)
83         if not loggedIn:
84                 import base64
85                 try:
86                         blobs = (
87                                 config.get(constants.__pretty_app_name__, "bin_blob_%i" % i)
88                                 for i in xrange(2)
89                         )
90                         creds = (
91                                 base64.b64decode(blob)
92                                 for blob in blobs
93                         )
94                         username, password = tuple(creds)
95                         loggedIn = backend.login(username, password)
96                 except ConfigParser.NoOptionError, e:
97                         pass
98                 except ConfigParser.NoSectionError, e:
99                         pass
100
101         try:
102                 notifyOnMissed = config.getboolean("2 - Account Info", "notifyOnMissed")
103                 notifyOnVoicemail = config.getboolean("2 - Account Info", "notifyOnVoicemail")
104                 notifyOnSms = config.getboolean("2 - Account Info", "notifyOnSms")
105         except ConfigParser.NoOptionError, e:
106                 notifyOnMissed = False
107                 notifyOnVoicemail = False
108                 notifyOnSms = False
109         except ConfigParser.NoSectionError, e:
110                 notifyOnMissed = False
111                 notifyOnVoicemail = False
112                 notifyOnSms = False
113
114         assert loggedIn
115         notifySources = []
116         if notifyOnMissed:
117                 notifySources.append(("missed", get_missed))
118         if notifyOnVoicemail:
119                 notifySources.append(("voicemail", get_voicemail))
120         if notifyOnSms:
121                 notifySources.append(("sms", get_sms))
122
123         notifyUser = False
124         for type, get_material in notifySources:
125                 if is_type_changed(backend, type, get_material):
126                         notifyUser = True
127         return notifyUser
128
129
130 def notify_on_change():
131         notifyUser = is_changed()
132
133         if notifyUser:
134                 import led_handler
135                 led = led_handler.LedHandler()
136                 led.on()
137
138
139 if __name__ == "__main__":
140         notify_on_change()