From c011b10dee9ddac3d21a8a4d9b4d298624b32260 Mon Sep 17 00:00:00 2001 From: epage Date: Thu, 15 Oct 2009 02:13:01 +0000 Subject: [PATCH] Moving some debug stuff into dialcentral git-svn-id: file:///svnroot/gc-dialer/trunk@529 c39d3808-3fe2-4d86-a59f-b7f623ee9f21 --- src/constants.py | 2 +- src/gv_backend.py | 103 +++++++++++++++++++++++++++---- support/builddeb.py | 1 + tests/gv_samples/dump_cookies.py | 52 ---------------- tests/gv_samples/generate_gv_samples.py | 90 +-------------------------- 5 files changed, 93 insertions(+), 155 deletions(-) delete mode 100755 tests/gv_samples/dump_cookies.py diff --git a/src/constants.py b/src/constants.py index 286d9fa..9db52b1 100644 --- a/src/constants.py +++ b/src/constants.py @@ -3,7 +3,7 @@ import os __pretty_app_name__ = "DialCentral" __app_name__ = "dialcentral" __version__ = "1.0.7" -__build__ = 10 +__build__ = 11 __app_magic__ = 0xdeadbeef _data_path_ = os.path.join(os.path.expanduser("~"), ".dialcentral") _user_settings_ = "%s/settings.ini" % _data_path_ diff --git a/src/gv_backend.py b/src/gv_backend.py index 8aa3d89..2ecbb58 100644 --- a/src/gv_backend.py +++ b/src/gv_backend.py @@ -25,6 +25,7 @@ Resources http://posttopic.com/topic/google-voice-add-on-development """ +from __future__ import with_statement import os import re @@ -123,6 +124,8 @@ class GVDialer(object): self._callbackNumber = "" self._callbackNumbers = {} + _forwardURL = "https://www.google.com/voice/mobile/phones" + def is_authed(self, force = False): """ Attempts to detect a current session @@ -147,13 +150,7 @@ class GVDialer(object): _loginURL = "https://www.google.com/accounts/ServiceLoginAuth" _galxRe = re.compile(r"""""", re.MULTILINE | re.DOTALL) - def login(self, username, password): - """ - Attempt to login to GoogleVoice - @returns Whether login was successful or not - """ - self.logout() - + def _get_token(self): try: tokenPage = self._browser.download(self._tokenURL) except urllib2.URLError, e: @@ -165,7 +162,9 @@ class GVDialer(object): else: galxToken = "" _moduleLogger.debug("Could not grab GALX token") + return galxToken + def _login(self, username, password, token): loginPostData = urllib.urlencode({ 'Email' : username, 'Passwd' : password, @@ -173,7 +172,7 @@ class GVDialer(object): "ltmpl": "mobile", "btmpl": "mobile", "PersistentCookie": "yes", - "GALX": galxToken, + "GALX": token, "continue": self._forwardURL, }) @@ -182,6 +181,16 @@ class GVDialer(object): except urllib2.URLError, e: _moduleLogger.exception("Translating error: %s" % str(e)) raise NetworkError("%s is not accesible" % self._loginURL) + return loginSuccessOrFailurePage + + def login(self, username, password): + """ + Attempt to login to GoogleVoice + @returns Whether login was successful or not + """ + self.logout() + galxToken = self._get_token() + loginSuccessOrFailurePage = self._login(username, password, galxToken) try: self._grab_account_info(loginSuccessOrFailurePage) @@ -270,8 +279,6 @@ class GVDialer(object): return {} return self._callbackNumbers - _setforwardURL = "https://www.google.com//voice/m/setphone" - def set_callback_number(self, callbacknumber): """ Set the number that GoogleVoice calls @@ -417,7 +424,6 @@ class GVDialer(object): _tokenRe = re.compile(r"""""") _accountNumRe = re.compile(r"""(.{14})""") _callbackRe = re.compile(r"""\s+(.*?):\s*(.*?)\s*$""", re.M) - _forwardURL = "https://www.google.com/voice/mobile/phones" def _grab_account_info(self, page): tokenGroup = self._tokenRe.search(page) @@ -667,7 +673,7 @@ def test_backend(username, password): print "Login?: ", backend.login(username, password) print "Authenticated: ", backend.is_authed() - #print "Token: ", backend._token + print "Token: ", backend._token #print "Account: ", backend.get_account_number() #print "Callback: ", backend.get_callback_number() #print "All Callback: ", @@ -696,7 +702,78 @@ def test_backend(username, password): return backend +_TEST_WEBPAGES = [ + ("forward", GVDialer._forwardURL), + ("token", GVDialer._tokenURL), + ("login", GVDialer._loginURL), + ("contacts", GVDialer._contactsURL), + + ("voicemail", GVDialer._voicemailURL), + ("sms", GVDialer._smsURL), + + ("recent", GVDialer._recentCallsURL), + ("placed", GVDialer._placedCallsURL), + ("recieved", GVDialer._receivedCallsURL), + ("missed", GVDialer._missedCallsURL), +] + + +def grab_debug_info(username, password): + cookieFile = os.path.join(".", "raw_cookies.txt") + try: + os.remove(cookieFile) + except OSError: + pass + + backend = GVDialer(cookieFile) + browser = backend._browser + + # Get Pages + print "Grabbing pre-login pages" + for name, url in _TEST_WEBPAGES: + try: + page = browser.download(url) + except StandardError, e: + print e.message + continue + print "\tWriting to file" + with open("not_loggedin_%s.txt" % name, "w") as f: + f.write(page) + + # Login + print "Attempting login" + galxToken = backend._get_token() + loginSuccessOrFailurePage = backend._login(username, password, galxToken) + with open("loggingin.txt", "w") as f: + print "\tWriting to file" + f.write(loginSuccessOrFailurePage) + backend._grab_account_info(loginSuccessOrFailurePage) + assert backend.is_authed() + + # Get Pages + print "Grabbing post-login pages" + for name, url in _TEST_WEBPAGES: + try: + page = browser.download(url) + except StandardError, e: + print e.message + continue + print "\tWriting to file" + with open("loggedin_%s.txt" % name, "w") as f: + f.write(page) + + # Cookies + browser.cookies.save() + print "\tWriting cookies to file" + with open("cookies.txt", "w") as f: + f.writelines( + "%s: %s\n" % (c.name, c.value) + for c in browser.cookies + ) + + if __name__ == "__main__": import sys logging.basicConfig(level=logging.DEBUG) - test_backend(sys.argv[1], sys.argv[2]) + #test_backend(sys.argv[1], sys.argv[2]) + grab_debug_info(sys.argv[1], sys.argv[2]) diff --git a/support/builddeb.py b/support/builddeb.py index 8b4929b..0bcc219 100755 --- a/support/builddeb.py +++ b/support/builddeb.py @@ -44,6 +44,7 @@ __changelog__ = """ * Bug Fix: Not properly hildonizing some code * Debugging: Improved logging output * Debugging: Printing page when can't get a callback number +* Debugging: Included stuff to create all the debug files 1.0.6 * Fremantle Prep: Simplified menus in prep for no menu or the Fremantle App Menu diff --git a/tests/gv_samples/dump_cookies.py b/tests/gv_samples/dump_cookies.py deleted file mode 100755 index 748826b..0000000 --- a/tests/gv_samples/dump_cookies.py +++ /dev/null @@ -1,52 +0,0 @@ -#!/usr/bin/env python - -import os -import urllib -import urllib2 -import traceback -import warnings - -import sys -sys.path.append("../../src") - -import browser_emu -import gv_backend - -# Create Browser -browser = browser_emu.MozillaEmulator(1) -cookieFile = os.path.join(".", ".gv_cookies.txt") -browser.cookies.filename = cookieFile - -# Login -username = sys.argv[1] -password = sys.argv[2] - -loginPostData = urllib.urlencode({ - 'Email' : username, - 'Passwd' : password, - 'service': "grandcentral", - "ltmpl": "mobile", - "btmpl": "mobile", - "PersistentCookie": "yes", -}) - -try: - loginSuccessOrFailurePage = browser.download(gv_backend.GVDialer._loginURL, loginPostData) -except urllib2.URLError, e: - warnings.warn(traceback.format_exc()) - raise RuntimeError("%s is not accesible" % gv_backend.GVDialer._loginURL) - -forwardPage = browser.download(gv_backend.GVDialer._forwardURL) - -tokenGroup = gv_backend.GVDialer._tokenRe.search(forwardPage) -if tokenGroup is None: - print forwardPage - raise RuntimeError("Could not extract authentication token from GoogleVoice") -token = tokenGroup.group(1) - - -with open("cookies.txt", "w") as f: - f.writelines( - "%s: %s\n" % (c.name, c.value) - for c in browser.cookies - ) diff --git a/tests/gv_samples/generate_gv_samples.py b/tests/gv_samples/generate_gv_samples.py index 8850252..0b2ce8d 100755 --- a/tests/gv_samples/generate_gv_samples.py +++ b/tests/gv_samples/generate_gv_samples.py @@ -2,107 +2,19 @@ from __future__ import with_statement -import os -import urllib -import urllib2 -import re -import traceback -import warnings import logging -import pprint import sys sys.path.append("/usr/lib/dialcentral") sys.path.append("../../src") -import browser_emu import gv_backend def main(): - webpages = [ - #("login", gv_backend.GVDialer._loginURL), - ("contacts", gv_backend.GVDialer._contactsURL), - ("voicemail", gv_backend.GVDialer._voicemailURL), - ("sms", gv_backend.GVDialer._smsURL), - ("forward", gv_backend.GVDialer._forwardURL), - ("recent", gv_backend.GVDialer._recentCallsURL), - ("placed", gv_backend.GVDialer._placedCallsURL), - ("recieved", gv_backend.GVDialer._receivedCallsURL), - ("missed", gv_backend.GVDialer._missedCallsURL), - ] - - - # Create Browser - browser = browser_emu.MozillaEmulator(1) - cookieFile = os.path.join(".", ".gv_cookies.txt") - browser.cookies.filename = cookieFile - - # Get Pages - for name, url in webpages: - try: - page = browser.download(url) - except StandardError, e: - print e.message - continue - print "Writing to file" - with open("not_loggedin_%s.txt" % name, "w") as f: - f.write(page) - - loginPage = browser.download("http://www.google.com/voice/m") - with open("login.txt", "w") as f: - print "Writing to file" - f.write(loginPage) - glxRe = re.compile(r"""""", re.MULTILINE | re.DOTALL) - glxTokens = glxRe.search(loginPage) - glxToken = glxTokens.group(1) - - # Login username = sys.argv[1] password = sys.argv[2] - - loginData = { - 'Email' : username, - 'Passwd' : password, - 'service': "grandcentral", - "ltmpl": "mobile", - "btmpl": "mobile", - "PersistentCookie": "yes", - "rmShown": "1", - "GALX": glxToken, - "continue": gv_backend.GVDialer._forwardURL, - } - pprint.pprint(loginData) - loginPostData = urllib.urlencode(loginData) - pprint.pprint(loginPostData) - - try: - loginSuccessOrFailurePage = browser.download(gv_backend.GVDialer._loginURL, loginPostData) - except urllib2.URLError, e: - warnings.warn(traceback.format_exc()) - raise RuntimeError("%s is not accesible" % gv_backend.GVDialer._loginURL) - with open("loggingin.txt", "w") as f: - print "Writing to file" - f.write(loginSuccessOrFailurePage) - - forwardPage = loginSuccessOrFailurePage - - tokenGroup = gv_backend.GVDialer._tokenRe.search(forwardPage) - if tokenGroup is None: - print forwardPage - raise RuntimeError("Could not extract authentication token from GoogleVoice") - token = tokenGroup.group(1) - - # Get Pages - for name, url in webpages: - try: - page = browser.download(url) - except StandardError, e: - warnings.warn(traceback.format_exc()) - continue - print "Writing to file" - with open("loggedin_%s.txt" % name, "w") as f: - f.write(page) + gv_backend.grab_debug_info(username, password) if __name__ == "__main__": -- 1.7.9.5