Adding orientation to the account dialog
[gc-dialer] / src / alarm_notify.py
index d71b030..bc6240e 100755 (executable)
@@ -4,32 +4,49 @@ import os
 import filecmp
 import ConfigParser
 import pprint
+import logging
+import logging.handlers
 
 import constants
-from backends import gv_backend
+from backends.gvoice import gvoice
 
 
 def get_missed(backend):
-       missedPage = backend._browser.download(backend._missedCallsURL)
+       missedPage = backend._browser.download(backend._XML_MISSED_URL)
        missedJson = backend._grab_json(missedPage)
        return missedJson
 
 
 def get_voicemail(backend):
-       voicemailPage = backend._browser.download(backend._voicemailURL)
+       voicemailPage = backend._browser.download(backend._XML_VOICEMAIL_URL)
        voicemailJson = backend._grab_json(voicemailPage)
        return voicemailJson
 
 
 def get_sms(backend):
-       smsPage = backend._browser.download(backend._smsURL)
+       smsPage = backend._browser.download(backend._XML_SMS_URL)
        smsJson = backend._grab_json(smsPage)
        return smsJson
 
 
 def remove_reltime(data):
        for messageData in data["messages"].itervalues():
-               del messageData["relativeStartTime"]
+               for badPart in [
+                       "relTime",
+                       "relativeStartTime",
+                       "time",
+                       "star",
+                       "isArchived",
+                       "isRead",
+                       "isSpam",
+                       "isTrash",
+                       "labels",
+               ]:
+                       if badPart in messageData:
+                               del messageData[badPart]
+       for globalBad in ["unreadCounts", "totalSize", "resultsPerPage"]:
+               if globalBad in data:
+                       del data[globalBad]
 
 
 def is_type_changed(backend, type, get_material):
@@ -71,12 +88,12 @@ def is_type_changed(backend, type, get_material):
 
 def create_backend(config):
        gvCookiePath = os.path.join(constants._data_path_, "gv_cookies.txt")
-       backend = gv_backend.GVDialer(gvCookiePath)
+       backend = gvoice.GVoiceBackend(gvCookiePath)
 
        loggedIn = False
 
        if not loggedIn:
-               loggedIn = backend.is_authed()
+               loggedIn = backend.refresh_account_info() is not None
 
        if not loggedIn:
                import base64
@@ -90,7 +107,7 @@ def create_backend(config):
                                for blob in blobs
                        )
                        username, password = tuple(creds)
-                       loggedIn = backend.login(username, password)
+                       loggedIn = backend.login(username, password) is not None
                except ConfigParser.NoOptionError, e:
                        pass
                except ConfigParser.NoSectionError, e:
@@ -113,6 +130,9 @@ def is_changed(config, backend):
                notifyOnMissed = False
                notifyOnVoicemail = False
                notifyOnSms = False
+       logging.debug(
+               "Missed: %s, Voicemail: %s, SMS: %s" % (notifyOnMissed, notifyOnVoicemail, notifyOnSms)
+       )
 
        notifySources = []
        if notifyOnMissed:
@@ -136,10 +156,27 @@ def notify_on_change():
        notifyUser = is_changed(config, backend)
 
        if notifyUser:
+               logging.info("Changed")
                import led_handler
                led = led_handler.LedHandler()
                led.on()
+       else:
+               logging.info("No Change")
 
 
 if __name__ == "__main__":
-       notify_on_change()
+       logFormat = '(%(relativeCreated)5d) %(levelname)-5s %(threadName)s.%(name)s.%(funcName)s: %(message)s'
+       logging.basicConfig(level=logging.DEBUG, format=logFormat)
+       rotating = logging.handlers.RotatingFileHandler(constants._notifier_logpath_, maxBytes=512*1024, backupCount=1)
+       rotating.setFormatter(logging.Formatter(logFormat))
+       root = logging.getLogger()
+       root.addHandler(rotating)
+       logging.info("Notifier %s-%s" % (constants.__version__, constants.__build__))
+       logging.info("OS: %s" % (os.uname()[0], ))
+       logging.info("Kernel: %s (%s) for %s" % os.uname()[2:])
+       logging.info("Hostname: %s" % os.uname()[1])
+       try:
+               notify_on_change()
+       except:
+               logging.exception("Error")
+               raise