8 import logging.handlers
11 from util import linux as linux_utils
12 from backends.gvoice import gvoice
15 CACHE_PATH = linux_utils.get_resource_path("cache", constants.__app_name__)
18 def get_missed(backend):
19 missedPage = backend._browser.download(backend._XML_MISSED_URL)
20 missedJson = backend._grab_json(missedPage)
24 def get_voicemail(backend):
25 voicemailPage = backend._browser.download(backend._XML_VOICEMAIL_URL)
26 voicemailJson = backend._grab_json(voicemailPage)
31 smsPage = backend._browser.download(backend._XML_SMS_URL)
32 smsJson = backend._grab_json(smsPage)
36 def remove_reltime(data):
37 for messageData in data["messages"].itervalues():
49 if badPart in messageData:
50 del messageData[badPart]
51 for globalBad in ["unreadCounts", "totalSize", "resultsPerPage"]:
56 def is_type_changed(backend, type, get_material):
57 jsonMaterial = get_material(backend)
58 unreadCount = jsonMaterial["unreadCounts"][type]
60 previousSnapshotPath = os.path.join(CACHE_PATH, "snapshot_%s.old.json" % type)
61 currentSnapshotPath = os.path.join(CACHE_PATH, "snapshot_%s.json" % type)
64 os.remove(previousSnapshotPath)
66 # check if failed purely because the old file didn't exist, which is fine
70 os.rename(currentSnapshotPath, previousSnapshotPath)
73 # check if failed purely because the new old file didn't exist, which is fine
76 previousExists = False
78 remove_reltime(jsonMaterial)
79 textMaterial = pprint.pformat(jsonMaterial)
80 currentSnapshot = file(currentSnapshotPath, "w")
82 currentSnapshot.write(textMaterial)
84 currentSnapshot.close()
86 if unreadCount == 0 or not previousExists:
89 seemEqual = filecmp.cmp(previousSnapshotPath, currentSnapshotPath)
93 def create_backend(config):
94 gvCookiePath = os.path.join(CACHE_PATH, "gv_cookies.txt")
95 backend = gvoice.GVoiceBackend(gvCookiePath)
100 loggedIn = backend.refresh_account_info() is not None
106 config.get(constants.__pretty_app_name__, "bin_blob_%i" % i)
110 base64.b64decode(blob)
113 username, password = tuple(creds)
114 loggedIn = backend.login(username, password) is not None
115 except ConfigParser.NoOptionError, e:
117 except ConfigParser.NoSectionError, e:
124 def is_changed(config, backend):
126 notifyOnMissed = config.getboolean("2 - Account Info", "notifyOnMissed")
127 notifyOnVoicemail = config.getboolean("2 - Account Info", "notifyOnVoicemail")
128 notifyOnSms = config.getboolean("2 - Account Info", "notifyOnSms")
129 except ConfigParser.NoOptionError, e:
130 notifyOnMissed = False
131 notifyOnVoicemail = False
133 except ConfigParser.NoSectionError, e:
134 notifyOnMissed = False
135 notifyOnVoicemail = False
138 "Missed: %s, Voicemail: %s, SMS: %s" % (notifyOnMissed, notifyOnVoicemail, notifyOnSms)
143 notifySources.append(("missed", get_missed))
144 if notifyOnVoicemail:
145 notifySources.append(("voicemail", get_voicemail))
147 notifySources.append(("sms", get_sms))
150 for type, get_material in notifySources:
151 if is_type_changed(backend, type, get_material):
156 def notify_on_change():
157 settingsPath = linux_utils.get_resource_path("config", constants.__app_name__, "settings.ini")
159 config = ConfigParser.SafeConfigParser()
160 config.read(settingsPath)
161 backend = create_backend(config)
162 notifyUser = is_changed(config, backend)
165 logging.info("Changed")
167 led = led_handler.LedHandler()
170 logging.info("No Change")
173 if __name__ == "__main__":
174 notifierPath = os.path.join(CACHE_PATH, "notifier.log")
176 logFormat = '(%(relativeCreated)5d) %(levelname)-5s %(threadName)s.%(name)s.%(funcName)s: %(message)s'
177 logging.basicConfig(level=logging.DEBUG, format=logFormat)
178 rotating = logging.handlers.RotatingFileHandler(notifierPath, maxBytes=512*1024, backupCount=1)
179 rotating.setFormatter(logging.Formatter(logFormat))
180 root = logging.getLogger()
181 root.addHandler(rotating)
182 logging.info("Notifier %s-%s" % (constants.__version__, constants.__build__))
183 logging.info("OS: %s" % (os.uname()[0], ))
184 logging.info("Kernel: %s (%s) for %s" % os.uname()[2:])
185 logging.info("Hostname: %s" % os.uname()[1])
189 logging.exception("Error")