Baseline for work is Dialcentral 1.0.6-10
[theonering] / tests / gc_samples / generate_gc_samples.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 webpages = [
16         ("forward", gc_backend.GCDialer._forwardselectURL),
17         ("login", gc_backend.GCDialer._loginURL),
18         ("setforward", gc_backend.GCDialer._setforwardURL),
19         ("clicktocall", gc_backend.GCDialer._clicktocallURL),
20         ("recent", gc_backend.GCDialer._inboxallURL),
21         ("contacts", gc_backend.GCDialer._contactsURL),
22 ]
23
24
25 # Create Browser
26 browser = browser_emu.MozillaEmulator(1)
27 cookieFile = os.path.join(".", ".gc_cookies.txt")
28 browser.cookies.filename = cookieFile
29
30 # Get Pages
31 for name, url in webpages:
32         try:
33                 page = browser.download(url)
34         except StandardError, e:
35                 print e.message
36                 continue
37         with open("not_loggedin_%s.txt" % name, "w") as f:
38                 f.write(page)
39
40 # Login
41 username = sys.argv[1]
42 password = sys.argv[2]
43
44 loginPostData = urllib.urlencode({
45         'username' : username,
46         'password' : password,
47 })
48
49 try:
50         loginSuccessOrFailurePage = browser.download(gc_backend.GCDialer._loginURL, loginPostData)
51 except urllib2.URLError, e:
52         warnings.warn(traceback.format_exc())
53         raise RuntimeError("%s is not accesible" % gc_backend.GCDialer._loginURL)
54
55 forwardPage = browser.download(gc_backend.GCDialer._forwardselectURL)
56
57 tokenGroup = gc_backend.GCDialer._accessTokenRe.search(forwardPage)
58 if tokenGroup is None:
59         print "="*60
60         print forwardPage
61         print "="*60
62         raise RuntimeError("Could not extract authentication token from GrandCentral")
63 token = tokenGroup.group(1)
64
65 # Get Pages
66 for name, url in webpages:
67         try:
68                 page = browser.download(url)
69         except StandardError, e:
70                 warnings.warn(traceback.format_exc())
71                 continue
72         print "Writing to file"
73         with open("loggedin_%s.txt" % name, "w") as f:
74                 f.write(page)