Most of the way there on notification support; can turn off led, can set and delete...
[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 = pprint.pformat(backend._grab_json(missedPage))
15         return missedJson
16
17
18 def get_voicemail(backend):
19         voicemailPage = backend._browser.download(backend._voicemailURL)
20         voicemailJson = pprint.pformat(backend._grab_json(voicemailPage))
21         return voicemailJson
22
23
24 def get_sms(backend):
25         smsPage = backend._browser.download(backend._smsURL)
26         smsJson = pprint.pformat(backend._grab_json(smsPage))
27         return smsJson
28
29
30 def is_changed(backend, type, get_material):
31         currentMaterial = get_material(backend)
32         previousSnapshotPath = os.path.join(constants._data_path_, "snapshot_%s.old.json" % type)
33         currentSnapshotPath = os.path.join(constants._data_path_, "snapshot_%s.json" % type)
34
35         try:
36                 os.remove(previousSnapshotPath)
37         except OSError, e:
38                 # check if failed purely because the old file didn't exist, which is fine
39                 if e.errno != 2:
40                         raise
41         try:
42                 os.rename(currentSnapshotPath, previousSnapshotPath)
43                 previousExists = True
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                 previousExists = False
49
50         currentSnapshot = file(currentSnapshotPath, "w")
51         try:
52                 currentSnapshot.write(currentMaterial)
53         finally:
54                 currentSnapshot.close()
55
56         if not previousExists:
57                 return True
58
59         seemEqual = filecmp.cmp(previousSnapshotPath, currentSnapshotPath)
60         return not seemEqual
61
62
63 def notify():
64         gvCookiePath = os.path.join(constants._data_path_, "gv_cookies.txt")
65         backend = gv_backend.GVDialer(gvCookiePath)
66
67         loggedIn = False
68
69         if not loggedIn:
70                 loggedIn = backend.is_authed()
71
72         config = ConfigParser.SafeConfigParser()
73         config.read(constants._user_settings_)
74         if not loggedIn:
75                 import base64
76                 try:
77                         blobs = (
78                                 config.get(constants.__pretty_app_name__, "bin_blob_%i" % i)
79                                 for i in xrange(2)
80                         )
81                         creds = (
82                                 base64.b64decode(blob)
83                                 for blob in blobs
84                         )
85                         username, password = tuple(creds)
86                         loggedIn = backend.login(username, password)
87                 except ConfigParser.NoOptionError, e:
88                         pass
89                 except ConfigParser.NoSectionError, e:
90                         pass
91
92         try:
93                 notifyOnMissed = config.getboolean("2 - Account Info", "notifyOnMissed")
94                 notifyOnVoicemail = config.getboolean("2 - Account Info", "notifyOnVoicemail")
95                 notifyOnSms = config.getboolean("2 - Account Info", "notifyOnSms")
96         except ConfigParser.NoOptionError, e:
97                 notifyOnMissed = False
98                 notifyOnVoicemail = False
99                 notifyOnSms = False
100         except ConfigParser.NoSectionError, e:
101                 notifyOnMissed = False
102                 notifyOnVoicemail = False
103                 notifyOnSms = False
104
105         assert loggedIn
106         notifySources = []
107         if notifyOnMissed:
108                 notifySources.append(("missed", get_missed))
109         if notifyOnVoicemail:
110                 notifySources.append(("voicemail", get_voicemail))
111         if notifyOnSms:
112                 notifySources.append(("sms", get_sms))
113
114         notifyUser = False
115         for type, get_material in (
116                 ("missed", get_missed),
117                 ("voicemail", get_voicemail),
118                 ("sms", get_sms),
119         ):
120                 if is_changed(backend, type, get_material):
121                         notifyUser = True
122
123         if notifyUser:
124                 import led_handler
125                 led = led_handler.LedHandler()
126                 led.on()
127
128
129 if __name__ == "__main__":
130         notify()