Prepping work to start on the next release
[gc-dialer] / tests / gc_samples / dump_cookies.py
1 #!/usr/bin/env python
2
3 import os
4 import urllib
5 import urllib2
6 import traceback
7 import warnings
8
9 import sys
10 sys.path.append("../../src")
11
12 import browser_emu
13 import gc_backend
14
15 # Create Browser
16 browser = browser_emu.MozillaEmulator(1)
17 cookieFile = os.path.join(".", ".gc_cookies.txt")
18 browser.cookies.filename = cookieFile
19
20 # Login
21 username = sys.argv[1]
22 password = sys.argv[2]
23
24 loginPostData = urllib.urlencode({
25         'Email' : username,
26         'Passwd' : password,
27         'service': "grandcentral",
28         "ltmpl": "mobile",
29         "btmpl": "mobile",
30         "PersistentCookie": "yes",
31 })
32
33 try:
34         loginSuccessOrFailurePage = browser.download(gc_backend.GCDialer._loginURL, loginPostData)
35 except urllib2.URLError, e:
36         warnings.warn(traceback.format_exc())
37         raise RuntimeError("%s is not accesible" % gc_backend.GCDialer._loginURL)
38
39 forwardPage = browser.download(gc_backend.GCDialer._forwardselectURL)
40
41 tokenGroup = gc_backend.GCDialer._accessTokenRe.search(forwardPage)
42 if tokenGroup is None:
43         print forwardPage
44         raise RuntimeError("Could not extract authentication token from GrandCentral")
45 token = tokenGroup.group(1)
46
47
48 with open("cookies.txt", "w") as f:
49         f.writelines(
50                 "%s: %s\n" % (c.name, c.value)
51                 for c in browser.cookies
52         )