Improving support for a worker thread and moving more code over to it
[theonering] / src / gvoice / backend.py
index 9996e74..72eda38 100755 (executable)
@@ -473,18 +473,15 @@ class GVoiceBackend(object):
                @returns Iterable of (personsName, phoneNumber, exact date, relative date, action)
                @blocks
                """
-               for action, url in (
-                       ("Received", self._XML_RECEIVED_URL),
-                       ("Missed", self._XML_MISSED_URL),
-                       ("Placed", self._XML_PLACED_URL),
-               ):
-                       flatXml = self._get_page(url)
-
-                       allRecentHtml = self._grab_html(flatXml)
-                       allRecentData = self._parse_history(allRecentHtml)
-                       for recentCallData in allRecentData:
-                               recentCallData["action"] = action
-                               yield recentCallData
+               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)
 
        def get_contacts(self):
                """
@@ -492,14 +489,7 @@ class GVoiceBackend(object):
                @blocks
                """
                page = self._get_page(self._XML_CONTACTS_URL)
-               contactsBody = self._contactsBodyRe.search(page)
-               if contactsBody is None:
-                       raise RuntimeError("Could not extract contact information")
-               accountData = _fake_parse_json(contactsBody.group(1))
-               for contactId, contactDetails in accountData["contacts"].iteritems():
-                       # A zero contact id is the catch all for unknown contacts
-                       if contactId != "0":
-                               yield contactId, contactDetails
+               return self._process_contacts(page)
 
        def get_voicemails(self):
                """
@@ -584,6 +574,24 @@ class GVoiceBackend(object):
                        raise RuntimeError("Not Authenticated")
                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 _process_contacts(self, page):
+               contactsBody = self._contactsBodyRe.search(page)
+               if contactsBody is None:
+                       raise RuntimeError("Could not extract contact information")
+               accountData = _fake_parse_json(contactsBody.group(1))
+               for contactId, contactDetails in accountData["contacts"].iteritems():
+                       # A zero contact id is the catch all for unknown contacts
+                       if contactId != "0":
+                               yield contactId, contactDetails
+
        def _parse_history(self, historyHtml):
                splitVoicemail = self._seperateVoicemailsRegex.split(historyHtml)
                for messageId, messageHtml in itergroup(splitVoicemail[1:], 2):