~caravone/+junk/data

« back to all changes in this revision

Viewing changes to fetcher.py

  • Committer: Curtis Caravone
  • Date: 2012-08-21 22:51:30 UTC
  • Revision ID: curtis.caravone@canonical.com-20120821225130-er7x5smsq7e3tu24
Added scripts for generating graphs of filesync performance

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import cookielib
 
2
import getpass
 
3
import os
 
4
import stat
 
5
import sys
 
6
 
 
7
import mechanize
 
8
 
 
9
class Fetcher(object):
 
10
    # url = 'https://graphite.ubunet.canonical.com/render/?target=statsd.production.*.resources.sys.cpu.*.%7Buser,system,iowait,idle%7D.value&format=raw&from=-1days'
 
11
    # cookie_file = "cookies.dat"
 
12
 
 
13
    def __init__(self):
 
14
        self.browser = mechanize.Browser()
 
15
 
 
16
        self.browser.set_handle_equiv(True)
 
17
        self.browser.set_handle_gzip(True)
 
18
        self.browser.set_handle_redirect(True)
 
19
        self.browser.set_handle_referer(True)
 
20
        self.browser.set_handle_robots(False)
 
21
        self.browser.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(),
 
22
                                        max_time=1)
 
23
 
 
24
    def fetch(self, url, cookie_file):
 
25
        cookie_jar = cookielib.LWPCookieJar(cookie_file)
 
26
        if os.path.exists(cookie_file):
 
27
            cookie_jar.load(ignore_discard=True)
 
28
        self.browser.set_cookiejar(cookie_jar)
 
29
 
 
30
        self.browser.open(url)
 
31
        if self.browser.geturl() == \
 
32
                          "https://graphite.ubunet.canonical.com/openid/+login":
 
33
        # if self.browser.response().info()['Content-Type'] != 'text/plain':
 
34
            self.auth(url)
 
35
        cookie_jar.save(ignore_discard=True)
 
36
        os.chmod(cookie_file, stat.S_IRUSR | stat.S_IWUSR)
 
37
 
 
38
        return self.browser.response()
 
39
 
 
40
    def auth(self, url):
 
41
        br = self.browser
 
42
        url = br.geturl()
 
43
        assert url == 'https://graphite.ubunet.canonical.com/openid/+login', url
 
44
        assert br.title() == 'OpenID Authentication Required', br.title()
 
45
 
 
46
        br.select_form(nr=0)
 
47
        br.submit()
 
48
        url = br.geturl()
 
49
        assert url == 'https://graphite.ubunet.canonical.com/openid/+login', url
 
50
        assert br.title() == 'OpenID Authentication Required', br.title()
 
51
 
 
52
        br.select_form(nr=0)
 
53
        br.submit()
 
54
        if not br.title().startswith("Authenticate to "):
 
55
            url = br.geturl()
 
56
            assert url.startswith('https://login.ubuntu.com/'), url
 
57
            assert url.endswith('/+decide'), url
 
58
            assert br.title() == 'Log in', br.title()
 
59
 
 
60
            br.select_form(nr=0)
 
61
            br.form['email'] = raw_input         ("Email address: ")
 
62
            br.form['password'] = getpass.getpass("     Password: ")
 
63
            br.submit()
 
64
        if not br.title().startswith("Authenticate to "):
 
65
            response = br.response().read()
 
66
            assert 'To continue use 2 factor authentication' in response
 
67
            twofer = raw_input("2-factor auth: ")
 
68
            br.select_form(nr=0)
 
69
            br.form['oath_token'] = twofer
 
70
            br.submit()
 
71
        url = br.geturl()
 
72
        assert url.startswith('https://login.ubuntu.com/'), url
 
73
        assert url.endswith('/+decide'), url
 
74
        assert br.title().startswith("Authenticate to "), br.title()
 
75
 
 
76
        br.select_form(nr=0)
 
77
        br.submit()
 
78
 
 
79
        # assert br.geturl() == url, br.geturl()
 
80
        # content_type = br.response().info()['Content-Type']
 
81
        # assert content_type == 'text/plain', content_type
 
82
 
 
83
fetch = Fetcher().fetch
 
84
 
 
85
if __name__ == '__main__':
 
86
    if len(sys.argv) != 3:
 
87
        print "Usage:  %s url cookie_file" % sys.argv[0]
 
88
        sys.exit(1)
 
89
    print fetch(sys.argv[1], sys.argv[2]).read()