http://posttopic.com/topic/google-voice-add-on-development
"""
+from __future__ import with_statement
import os
import re
self._callbackNumber = ""
self._callbackNumbers = {}
+ _forwardURL = "https://www.google.com/voice/mobile/phones"
+
def is_authed(self, force = False):
"""
Attempts to detect a current session
_loginURL = "https://www.google.com/accounts/ServiceLoginAuth"
_galxRe = re.compile(r"""<input.*?name="GALX".*?value="(.*?)".*?/>""", 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:
else:
galxToken = ""
_moduleLogger.debug("Could not grab GALX token")
+ return galxToken
+ def _login(self, username, password, token):
loginPostData = urllib.urlencode({
'Email' : username,
'Passwd' : password,
"ltmpl": "mobile",
"btmpl": "mobile",
"PersistentCookie": "yes",
- "GALX": galxToken,
+ "GALX": token,
"continue": self._forwardURL,
})
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)
return {}
return self._callbackNumbers
- _setforwardURL = "https://www.google.com//voice/m/setphone"
-
def set_callback_number(self, callbacknumber):
"""
Set the number that GoogleVoice calls
_tokenRe = re.compile(r"""<input.*?name="_rnr_se".*?value="(.*?)"\s*/>""")
_accountNumRe = re.compile(r"""<b class="ms\d">(.{14})</b></div>""")
_callbackRe = re.compile(r"""\s+(.*?):\s*(.*?)<br\s*/>\s*$""", re.M)
- _forwardURL = "https://www.google.com/voice/mobile/phones"
def _grab_account_info(self, page):
tokenGroup = self._tokenRe.search(page)
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: ",
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])
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"""<input.*?name="GALX".*?value="(.*?)".*?/>""", 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__":