10 from backends.gvoice 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():
44 if badPart in messageData:
45 del messageData[badPart]
46 for globalBad in ["unreadCounts", "totalSize", "resultsPerPage"]:
51 def is_type_changed(backend, type, get_material):
52 jsonMaterial = get_material(backend)
53 unreadCount = jsonMaterial["unreadCounts"][type]
55 previousSnapshotPath = os.path.join(constants._data_path_, "snapshot_%s.old.json" % type)
56 currentSnapshotPath = os.path.join(constants._data_path_, "snapshot_%s.json" % type)
59 os.remove(previousSnapshotPath)
61 # check if failed purely because the old file didn't exist, which is fine
65 os.rename(currentSnapshotPath, previousSnapshotPath)
68 # check if failed purely because the new old file didn't exist, which is fine
71 previousExists = False
73 remove_reltime(jsonMaterial)
74 textMaterial = pprint.pformat(jsonMaterial)
75 currentSnapshot = file(currentSnapshotPath, "w")
77 currentSnapshot.write(textMaterial)
79 currentSnapshot.close()
81 if unreadCount == 0 or not previousExists:
84 seemEqual = filecmp.cmp(previousSnapshotPath, currentSnapshotPath)
88 def create_backend(config):
89 gvCookiePath = os.path.join(constants._data_path_, "gv_cookies.txt")
90 backend = gvoice.GVoiceBackend(gvCookiePath)
95 loggedIn = backend.refresh_account_info() is not None
101 config.get(constants.__pretty_app_name__, "bin_blob_%i" % i)
105 base64.b64decode(blob)
108 username, password = tuple(creds)
109 loggedIn = backend.login(username, password) is not None
110 except ConfigParser.NoOptionError, e:
112 except ConfigParser.NoSectionError, e:
119 def is_changed(config, backend):
121 notifyOnMissed = config.getboolean("2 - Account Info", "notifyOnMissed")
122 notifyOnVoicemail = config.getboolean("2 - Account Info", "notifyOnVoicemail")
123 notifyOnSms = config.getboolean("2 - Account Info", "notifyOnSms")
124 except ConfigParser.NoOptionError, e:
125 notifyOnMissed = False
126 notifyOnVoicemail = False
128 except ConfigParser.NoSectionError, e:
129 notifyOnMissed = False
130 notifyOnVoicemail = False
133 "Missed: %s, Voicemail: %s, SMS: %s" % (notifyOnMissed, notifyOnVoicemail, notifyOnSms)
138 notifySources.append(("missed", get_missed))
139 if notifyOnVoicemail:
140 notifySources.append(("voicemail", get_voicemail))
142 notifySources.append(("sms", get_sms))
145 for type, get_material in notifySources:
146 if is_type_changed(backend, type, get_material):
151 def notify_on_change():
152 config = ConfigParser.SafeConfigParser()
153 config.read(constants._user_settings_)
154 backend = create_backend(config)
155 notifyUser = is_changed(config, backend)
158 logging.info("Changed")
160 led = led_handler.LedHandler()
163 logging.info("No Change")
166 if __name__ == "__main__":
167 logging.basicConfig(level=logging.DEBUG, filename=constants._notifier_logpath_)
168 logging.info("Notifier %s-%s" % (constants.__version__, constants.__build__))
169 logging.info("OS: %s" % (os.uname()[0], ))
170 logging.info("Kernel: %s (%s) for %s" % os.uname()[2:])
171 logging.info("Hostname: %s" % os.uname()[1])
175 logging.exception("Error")