Bump to 1.0.10-2; and now that I am in Extras, adding an upgrade description
[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 from backends 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 create_backend(config):
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         if not loggedIn:
82                 import base64
83                 try:
84                         blobs = (
85                                 config.get(constants.__pretty_app_name__, "bin_blob_%i" % i)
86                                 for i in xrange(2)
87                         )
88                         creds = (
89                                 base64.b64decode(blob)
90                                 for blob in blobs
91                         )
92                         username, password = tuple(creds)
93                         loggedIn = backend.login(username, password)
94                 except ConfigParser.NoOptionError, e:
95                         pass
96                 except ConfigParser.NoSectionError, e:
97                         pass
98
99         assert loggedIn
100         return backend
101
102
103 def is_changed(config, backend):
104         try:
105                 notifyOnMissed = config.getboolean("2 - Account Info", "notifyOnMissed")
106                 notifyOnVoicemail = config.getboolean("2 - Account Info", "notifyOnVoicemail")
107                 notifyOnSms = config.getboolean("2 - Account Info", "notifyOnSms")
108         except ConfigParser.NoOptionError, e:
109                 notifyOnMissed = False
110                 notifyOnVoicemail = False
111                 notifyOnSms = False
112         except ConfigParser.NoSectionError, e:
113                 notifyOnMissed = False
114                 notifyOnVoicemail = False
115                 notifyOnSms = False
116
117         notifySources = []
118         if notifyOnMissed:
119                 notifySources.append(("missed", get_missed))
120         if notifyOnVoicemail:
121                 notifySources.append(("voicemail", get_voicemail))
122         if notifyOnSms:
123                 notifySources.append(("sms", get_sms))
124
125         notifyUser = False
126         for type, get_material in notifySources:
127                 if is_type_changed(backend, type, get_material):
128                         notifyUser = True
129         return notifyUser
130
131
132 def notify_on_change():
133         config = ConfigParser.SafeConfigParser()
134         config.read(constants._user_settings_)
135         backend = create_backend(config)
136         notifyUser = is_changed(config, backend)
137
138         if notifyUser:
139                 import led_handler
140                 led = led_handler.LedHandler()
141                 led.on()
142                 print "Notify!"
143         else:
144                 print "Nothing to report"
145
146
147 if __name__ == "__main__":
148         import logging
149         logging.basicConfig(level=logging.DEBUG)
150         notify_on_change()