From 82d3b4a7ae787eaaf1ac5ff07bc60aa25c84d50a Mon Sep 17 00:00:00 2001 From: Ed Page Date: Sat, 19 Jun 2010 13:28:35 -0500 Subject: [PATCH] Speeding up login through cookies --- src/connection.py | 4 ++-- src/gvoice/backend.py | 5 +++++ src/gvoice/browser_emu.py | 8 +++++--- src/gvoice/session.py | 45 ++++++++++++++++++++++++++++++++++++--------- 4 files changed, 48 insertions(+), 14 deletions(-) diff --git a/src/connection.py b/src/connection.py index 1ad2487..080c621 100644 --- a/src/connection.py +++ b/src/connection.py @@ -98,7 +98,7 @@ class TheOneRingConnection( # Connection init must come first self.__options = TheOneRingOptions(parameters) self.__session = gvoice.session.Session( - cookiePath = None, + cookiePath = os.path.join(constants._data_path_, "%s.cookies" % account), defaults = { "contacts": (self.__options.contactsPollPeriodInHours, "hours"), "voicemail": (self.__options.voicemailPollPeriodInMinutes, "minutes"), @@ -331,7 +331,7 @@ class TheOneRingConnection( self.manager.disconnected(self) self.session.save(self.__cachePath) - self.session.logout() + self.session.shutdown() self.session.close() # In case one of the above items takes too long (which it should never diff --git a/src/gvoice/backend.py b/src/gvoice/backend.py index 72eda38..0661d0c 100755 --- a/src/gvoice/backend.py +++ b/src/gvoice/backend.py @@ -304,6 +304,11 @@ class GVoiceBackend(object): self._lastAuthed = time.time() return True + def shutdown(self): + self._browser.save_cookies() + self._token = None + self._lastAuthed = 0.0 + def logout(self): self._browser.clear_cookies() self._browser.save_cookies() diff --git a/src/gvoice/browser_emu.py b/src/gvoice/browser_emu.py index 1ef4b2a..9617f78 100644 --- a/src/gvoice/browser_emu.py +++ b/src/gvoice/browser_emu.py @@ -48,10 +48,11 @@ class MozillaEmulator(object): self.trycount = trycount self._cookies = cookielib.LWPCookieJar() self._loadedFromCookies = False + self._usingCookies = False def load_cookies(self, path): assert not self._loadedFromCookies, "Load cookies only once" - if path is None: + if not path: return self._cookies.filename = path @@ -66,14 +67,15 @@ class MozillaEmulator(object): else: self._loadedFromCookies = True + self._usingCookies = True return self._loadedFromCookies def save_cookies(self): - if self._loadedFromCookies: + if self._usingCookies: self._cookies.save() def clear_cookies(self): - if self._loadedFromCookies: + if self._usingCookies: self._cookies.clear() def download(self, url, diff --git a/src/gvoice/session.py b/src/gvoice/session.py index 8151c05..5a825f0 100644 --- a/src/gvoice/session.py +++ b/src/gvoice/session.py @@ -159,19 +159,46 @@ class Session(object): def _login(self, username, password, on_success, on_error): self._username = username self._password = password - try: - isLoggedIn = yield ( - self._backend.login, - (self._username, self._password), - {}, - ) - except Exception, e: - on_error(e) - return + + isLoggedIn = False + + if not isLoggedIn and self._backend.is_quick_login_possible(): + try: + isLoggedIn = yield ( + self._backend.is_authed, + (), + {}, + ) + except Exception, e: + on_error(e) + return + if isLoggedIn: + _moduleLogger.info("Logged in through cookies") + + if not isLoggedIn: + try: + isLoggedIn = yield ( + self._backend.login, + (self._username, self._password), + {}, + ) + except Exception, e: + on_error(e) + return + if isLoggedIn: + _moduleLogger.info("Logged in through credentials") self._masterStateMachine.start() on_success(isLoggedIn) + def shutdown(self): + self._asyncPool.stop() + self._masterStateMachine.stop() + self._backend.shutdown() + + self._username = None + self._password = None + def logout(self): self._asyncPool.stop() self._masterStateMachine.stop() -- 1.7.9.5