8 import logging.handlers
11 from backends.gvoice import gvoice
14 def get_missed(backend):
15 missedPage = backend._browser.download(backend._XML_MISSED_URL)
16 missedJson = backend._grab_json(missedPage)
20 def get_voicemail(backend):
21 voicemailPage = backend._browser.download(backend._XML_VOICEMAIL_URL)
22 voicemailJson = backend._grab_json(voicemailPage)
27 smsPage = backend._browser.download(backend._XML_SMS_URL)
28 smsJson = backend._grab_json(smsPage)
32 def remove_reltime(data):
33 for messageData in data["messages"].itervalues():
45 if badPart in messageData:
46 del messageData[badPart]
47 for globalBad in ["unreadCounts", "totalSize", "resultsPerPage"]:
52 def is_type_changed(backend, type, get_material):
53 jsonMaterial = get_material(backend)
54 unreadCount = jsonMaterial["unreadCounts"][type]
56 previousSnapshotPath = os.path.join(constants._data_path_, "snapshot_%s.old.json" % type)
57 currentSnapshotPath = os.path.join(constants._data_path_, "snapshot_%s.json" % type)
60 os.remove(previousSnapshotPath)
62 # check if failed purely because the old file didn't exist, which is fine
66 os.rename(currentSnapshotPath, previousSnapshotPath)
69 # check if failed purely because the new old file didn't exist, which is fine
72 previousExists = False
74 remove_reltime(jsonMaterial)
75 textMaterial = pprint.pformat(jsonMaterial)
76 currentSnapshot = file(currentSnapshotPath, "w")
78 currentSnapshot.write(textMaterial)
80 currentSnapshot.close()
82 if unreadCount == 0 or not previousExists:
85 seemEqual = filecmp.cmp(previousSnapshotPath, currentSnapshotPath)
89 def create_backend(config):
90 gvCookiePath = os.path.join(constants._data_path_, "gv_cookies.txt")
91 backend = gvoice.GVoiceBackend(gvCookiePath)
96 loggedIn = backend.refresh_account_info() is not None
102 config.get(constants.__pretty_app_name__, "bin_blob_%i" % i)
106 base64.b64decode(blob)
109 username, password = tuple(creds)
110 loggedIn = backend.login(username, password) is not None
111 except ConfigParser.NoOptionError, e:
113 except ConfigParser.NoSectionError, e:
120 def is_changed(config, backend):
122 notifyOnMissed = config.getboolean("2 - Account Info", "notifyOnMissed")
123 notifyOnVoicemail = config.getboolean("2 - Account Info", "notifyOnVoicemail")
124 notifyOnSms = config.getboolean("2 - Account Info", "notifyOnSms")
125 except ConfigParser.NoOptionError, e:
126 notifyOnMissed = False
127 notifyOnVoicemail = False
129 except ConfigParser.NoSectionError, e:
130 notifyOnMissed = False
131 notifyOnVoicemail = False
134 "Missed: %s, Voicemail: %s, SMS: %s" % (notifyOnMissed, notifyOnVoicemail, notifyOnSms)
139 notifySources.append(("missed", get_missed))
140 if notifyOnVoicemail:
141 notifySources.append(("voicemail", get_voicemail))
143 notifySources.append(("sms", get_sms))
146 for type, get_material in notifySources:
147 if is_type_changed(backend, type, get_material):
152 def notify_on_change():
153 config = ConfigParser.SafeConfigParser()
154 config.read(constants._user_settings_)
155 backend = create_backend(config)
156 notifyUser = is_changed(config, backend)
159 logging.info("Changed")
161 led = led_handler.LedHandler()
164 logging.info("No Change")
167 if __name__ == "__main__":
168 logFormat = '(%(relativeCreated)5d) %(levelname)-5s %(threadName)s.%(name)s.%(funcName)s: %(message)s'
169 logging.basicConfig(level=logging.DEBUG, format=logFormat)
170 rotating = logging.handlers.RotatingFileHandler(constants._notifier_logpath_, maxBytes=512*1024, backupCount=1)
171 rotating.setFormatter(logging.Formatter(logFormat))
172 root = logging.getLogger()
173 root.addHandler(rotating)
174 logging.info("Notifier %s-%s" % (constants.__version__, constants.__build__))
175 logging.info("OS: %s" % (os.uname()[0], ))
176 logging.info("Kernel: %s (%s) for %s" % os.uname()[2:])
177 logging.info("Hostname: %s" % os.uname()[1])
181 logging.exception("Error")