Minor code re-arrangements
[gc-dialer] / src / gc_backend.py
index 9af4df7..67ca99b 100644 (file)
@@ -1,6 +1,6 @@
 #!/usr/bin/python
 
-# GC Dialer - Front end for Google's Grand Central service.
+# DialCentral - Front end for Google's Grand Central service.
 # Copyright (C) 2008  Eric Warnke ericew AT gmail DOT com
 # 
 # This library is free software; you can redistribute it and/or
@@ -18,7 +18,7 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
 """
-Grandcentral Dialer backend code
+Grandcentral backend code
 """
 
 
@@ -31,6 +31,11 @@ import warnings
 
 from browser_emu import MozillaEmulator
 
+import socket
+
+
+socket.setdefaulttimeout(5)
+
 
 class GCDialer(object):
        """
@@ -75,6 +80,8 @@ class GCDialer(object):
                self._callbackNumbers = {}
                self._lastAuthed = 0.0
 
+               self.__contacts = None
+
        def is_authed(self, force = False):
                """
                Attempts to detect a current session and pull the auth token ( a_t ) from the page.
@@ -82,7 +89,7 @@ class GCDialer(object):
                @returns If authenticated
                """
 
-               if time.time() - self._lastAuthed < 60 and not force:
+               if (time.time() - self._lastAuthed) < 60 and not force:
                        return True
 
                try:
@@ -122,6 +129,8 @@ class GCDialer(object):
                self._browser.cookies.clear()
                self._browser.cookies.save()
 
+               self.clear_caches()
+
        def dial(self, number):
                """
                This is the main function responsible for initating the callback
@@ -161,7 +170,7 @@ class GCDialer(object):
                return False
 
        def clear_caches(self):
-               pass
+               self.__contacts = None
 
        def is_valid_syntax(self, number):
                """
@@ -262,32 +271,44 @@ class GCDialer(object):
                """
                @returns Iterable of (Address Book Factory, Book Id, Book Name)
                """
-               yield self, "", "Grand Central"
-       
+               yield self, "", ""
+
        def open_addressbook(self, bookId):
                return self
 
        @staticmethod
-       def factory_name():
+       def contact_source_short_name(contactId):
                return "GC"
 
+       @staticmethod
+       def factory_name():
+               return "Grand Central"
+
        def get_contacts(self):
                """
                @returns Iterable of (contact id, contact name)
                """
-               contactsPagesUrls = [GCDialer._contactsURL]
-               for contactsPageUrl in contactsPagesUrls:
-                       contactsPage = self._browser.download(contactsPageUrl)
-                       for contact_match in self._contactsRe.finditer(contactsPage):
-                               contactId = contact_match.group(1)
-                               contactName = contact_match.group(2)
-                               yield contactId, contactName
-
-                       next_match = self._contactsNextRe.match(contactsPage)
-                       if next_match is not None:
-                               newContactsPageUrl = self._contactsURL + next_match.group(1)
-                               contactsPagesUrls.append(newContactsPageUrl)
-       
+               if self.__contacts is None:
+                       self.__contacts = []
+
+                       contactsPagesUrls = [GCDialer._contactsURL]
+                       for contactsPageUrl in contactsPagesUrls:
+                               contactsPage = self._browser.download(contactsPageUrl)
+                               for contact_match in self._contactsRe.finditer(contactsPage):
+                                       contactId = contact_match.group(1)
+                                       contactName = contact_match.group(2)
+                                       contact = contactId, contactName
+                                       self.__contacts.append(contact)
+                                       yield contact
+
+                               next_match = self._contactsNextRe.match(contactsPage)
+                               if next_match is not None:
+                                       newContactsPageUrl = self._contactsURL + next_match.group(1)
+                                       contactsPagesUrls.append(newContactsPageUrl)
+               else:
+                       for contact in self.__contacts:
+                               yield contact
+
        def get_contact_details(self, contactId):
                """
                @returns Iterable of (Phone Type, Phone Number)