Backend support for partial history update
authorEd Page <eopage@byu.net>
Fri, 18 Mar 2011 00:32:08 +0000 (19:32 -0500)
committerEd Page <eopage@byu.net>
Tue, 19 Apr 2011 23:49:27 +0000 (18:49 -0500)
src/backends/gv_backend.py
src/backends/gvoice/gvoice.py
src/session.py

index a4fadde..17bbc90 100644 (file)
@@ -44,10 +44,18 @@ class GVDialer(object):
        MESSAGE_VOICEMAILS = "Voicemail"
        MESSAGE_ALL = "All"
 
+       HISTORY_RECEIVED = "Received"
+       HISTORY_MISSED = "Missed"
+       HISTORY_PLACED = "Placed"
+       HISTORY_ALL = "All"
+
        def __init__(self, cookieFile = None):
                self._gvoice = gvoice.GVoiceBackend(cookieFile)
                self._texts = []
                self._voicemails = []
+               self._received = []
+               self._missed = []
+               self._placed = []
 
        def is_quick_login_possible(self):
                """
@@ -66,6 +74,11 @@ class GVDialer(object):
                return self._gvoice.login(username, password)
 
        def logout(self):
+               self._texts = []
+               self._voicemails = []
+               self._received = []
+               self._missed = []
+               self._placed = []
                return self._gvoice.logout()
 
        def persist(self):
@@ -144,11 +157,39 @@ class GVDialer(object):
                """
                return self._gvoice.get_callback_number()
 
-       def get_recent(self):
+       def get_call_history(self, historyType):
                """
                @returns Iterable of (personsName, phoneNumber, exact date, relative date, action)
                """
-               return list(self._gvoice.get_recent())
+               history = list(self._get_call_history(historyType))
+               history.sort(key=lambda item: item["time"])
+               return history
+
+       def _get_call_history(self, historyType):
+               """
+               @returns Iterable of (personsName, phoneNumber, exact date, relative date, action)
+               """
+               if historyType in [self.HISTORY_RECEIVED, self.HISTORY_ALL] or not self._received:
+                       self._received = list(self._gvoice.get_received_calls())
+                       for item in self._received:
+                               item["action"] = self.HISTORY_RECEIVED
+               if historyType in [self.HISTORY_MISSED, self.HISTORY_ALL] or not self._missed:
+                       self._missed = list(self._gvoice.get_missed_calls())
+                       for item in self._missed:
+                               item["action"] = self.HISTORY_MISSED
+               if historyType in [self.HISTORY_PLACED, self.HISTORY_ALL] or not self._placed:
+                       self._placed = list(self._gvoice.get_placed_calls())
+                       for item in self._placed:
+                               item["action"] = self.HISTORY_PLACED
+               received = self._received
+               missed = self._missed
+               placed = self._placed
+               for item in received:
+                       yield item
+               for item in missed:
+                       yield item
+               for item in placed:
+                       yield item
 
        def get_messages(self, messageType):
                messages = list(self._get_messages(messageType))
@@ -158,9 +199,9 @@ class GVDialer(object):
        def _get_messages(self, messageType):
                if messageType in [self.MESSAGE_VOICEMAILS, self.MESSAGE_ALL] or not self._voicemails:
                        self._voicemails = list(self._gvoice.get_voicemails())
-               voicemails = self._voicemails
                if messageType in [self.MESSAGE_TEXTS, self.MESSAGE_ALL] or not self._texts:
                        self._texts = list(self._gvoice.get_texts())
+               voicemails = self._voicemails
                smss = self._texts
 
                conversations = itertools.chain(voicemails, smss)
index f73600e..b0825ef 100755 (executable)
@@ -306,6 +306,7 @@ class GVoiceBackend(object):
                self._browser.save_cookies()
                self._token = None
                self._lastAuthed = 0.0
+               self._callbackNumbers = {}
 
        def is_dnd(self):
                """
@@ -464,20 +465,26 @@ class GVoiceBackend(object):
                """
                return self._callbackNumber
 
-       def get_recent(self):
+       def get_received_calls(self):
                """
                @returns Iterable of (personsName, phoneNumber, exact date, relative date, action)
                @blocks
                """
-               recentPages = [
-                       (action, self._get_page(url))
-                       for action, url in (
-                               ("Received", self._XML_RECEIVED_URL),
-                               ("Missed", self._XML_MISSED_URL),
-                               ("Placed", self._XML_PLACED_URL),
-                       )
-               ]
-               return self._parse_recent(recentPages)
+               return self._parse_recent(self._get_page(self._XML_RECEIVED_URL))
+
+       def get_missed_calls(self):
+               """
+               @returns Iterable of (personsName, phoneNumber, exact date, relative date, action)
+               @blocks
+               """
+               return self._parse_recent(self._get_page(self._XML_MISSED_URL))
+
+       def get_placed_calls(self):
+               """
+               @returns Iterable of (personsName, phoneNumber, exact date, relative date, action)
+               @blocks
+               """
+               return self._parse_recent(self._get_page(self._XML_PLACED_URL))
 
        def get_csv_contacts(self):
                data = {
@@ -570,13 +577,11 @@ class GVoiceBackend(object):
                        raise ValueError('Number is not valid: "%s"' % number)
                return number
 
-       def _parse_recent(self, recentPages):
-               for action, flatXml in recentPages:
-                       allRecentHtml = self._grab_html(flatXml)
-                       allRecentData = self._parse_history(allRecentHtml)
-                       for recentCallData in allRecentData:
-                               recentCallData["action"] = action
-                               yield recentCallData
+       def _parse_recent(self, recentPage):
+               allRecentHtml = self._grab_html(recentPage)
+               allRecentData = self._parse_history(allRecentHtml)
+               for recentCallData in allRecentData:
+                       yield recentCallData
 
        def _parse_history(self, historyHtml):
                splitVoicemail = self._seperateVoicemailsRegex.split(historyHtml)
index 8a97f4e..7061f09 100644 (file)
@@ -694,8 +694,8 @@ class Session(QtCore.QObject):
                        assert self.state == self.LOGGEDIN_STATE, "History requires being logged in (currently %s" % self.state
                        with qui_utils.notify_busy(self._errorLog, "Updating History"):
                                self._history = yield (
-                                       self._backend[0].get_recent,
-                                       (),
+                                       self._backend[0].get_call_history,
+                                       (self._backend[0].HISTORY_ALL, ),
                                        {},
                                )
                except Exception, e: