10 from backends import gvoice
13 def get_missed(backend):
14 missedPage = backend._browser.download(backend._XML_MISSED_URL)
15 missedJson = backend._grab_json(missedPage)
19 def get_voicemail(backend):
20 voicemailPage = backend._browser.download(backend._XML_VOICEMAIL_URL)
21 voicemailJson = backend._grab_json(voicemailPage)
26 smsPage = backend._browser.download(backend._XML_SMS_URL)
27 smsJson = backend._grab_json(smsPage)
31 def remove_reltime(data):
32 for messageData in data["messages"].itervalues():
33 del messageData["relativeStartTime"]
34 del messageData["labels"]
35 del messageData["isRead"]
36 del messageData["isSpam"]
37 del messageData["isTrash"]
38 del messageData["star"]
41 def is_type_changed(backend, type, get_material):
42 jsonMaterial = get_material(backend)
43 unreadCount = jsonMaterial["unreadCounts"][type]
45 previousSnapshotPath = os.path.join(constants._data_path_, "snapshot_%s.old.json" % type)
46 currentSnapshotPath = os.path.join(constants._data_path_, "snapshot_%s.json" % type)
49 os.remove(previousSnapshotPath)
51 # check if failed purely because the old file didn't exist, which is fine
55 os.rename(currentSnapshotPath, previousSnapshotPath)
58 # check if failed purely because the new old file didn't exist, which is fine
61 previousExists = False
63 remove_reltime(jsonMaterial)
64 textMaterial = pprint.pformat(jsonMaterial)
65 currentSnapshot = file(currentSnapshotPath, "w")
67 currentSnapshot.write(textMaterial)
69 currentSnapshot.close()
71 if unreadCount == 0 or not previousExists:
74 seemEqual = filecmp.cmp(previousSnapshotPath, currentSnapshotPath)
78 def create_backend(config):
79 gvCookiePath = os.path.join(constants._data_path_, "gv_cookies.txt")
80 backend = gvoice.GVoiceBackend(gvCookiePath)
85 loggedIn = backend.is_authed()
91 config.get(constants.__pretty_app_name__, "bin_blob_%i" % i)
95 base64.b64decode(blob)
98 username, password = tuple(creds)
99 loggedIn = backend.login(username, password)
100 except ConfigParser.NoOptionError, e:
102 except ConfigParser.NoSectionError, e:
109 def is_changed(config, backend):
111 notifyOnMissed = config.getboolean("2 - Account Info", "notifyOnMissed")
112 notifyOnVoicemail = config.getboolean("2 - Account Info", "notifyOnVoicemail")
113 notifyOnSms = config.getboolean("2 - Account Info", "notifyOnSms")
114 except ConfigParser.NoOptionError, e:
115 notifyOnMissed = False
116 notifyOnVoicemail = False
118 except ConfigParser.NoSectionError, e:
119 notifyOnMissed = False
120 notifyOnVoicemail = False
123 "Missed: %s, Voicemail: %s, SMS: %s" % (notifyOnMissed, notifyOnVoicemail, notifyOnSms)
128 notifySources.append(("missed", get_missed))
129 if notifyOnVoicemail:
130 notifySources.append(("voicemail", get_voicemail))
132 notifySources.append(("sms", get_sms))
135 for type, get_material in notifySources:
136 if is_type_changed(backend, type, get_material):
141 def notify_on_change():
142 config = ConfigParser.SafeConfigParser()
143 config.read(constants._user_settings_)
144 backend = create_backend(config)
145 notifyUser = is_changed(config, backend)
148 logging.info("Changed")
150 led = led_handler.LedHandler()
153 logging.info("No Change")
156 if __name__ == "__main__":
157 logging.basicConfig(level=logging.WARNING, filename=constants._notifier_logpath_)
158 logging.info("Notifier %s-%s" % (constants.__version__, constants.__build__))
159 logging.info("OS: %s" % (os.uname()[0], ))
160 logging.info("Kernel: %s (%s) for %s" % os.uname()[2:])
161 logging.info("Hostname: %s" % os.uname()[1])
165 logging.exception("Error")