-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathic.py
75 lines (61 loc) · 2.41 KB
/
ic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
""" Helpers for Imperial based automation.
"""
import sys
import logging
import mechanize
Error = mechanize.URLError
def quote(s, safe=':/?='):
return mechanize._urllib2_fork.quote(s, safe)
class FormRedirectBrowser(mechanize.Browser):
def __init__(self, **kwds):
mechanize.Browser.__init__(self, **kwds)
self.log = logging.getLogger('Browser')
def add_password(self, url, user, passwd=None, **kwds):
""" Add Authorisation for url.
"""
# Get uri for auth
uri_parts = url.split('/')
if uri_parts[0].endswith(':') and uri_parts[1] is '':
host = uri_parts[2] # '/'.join(uri_parts[:3])
else:
host = uri_parts[0]
self.log.debug("Adding password for user: '{0}', hostname: '{1}'."
.format(user, host))
mechanize.Browser.add_password(self, host, user, passwd, **kwds)
def redirect_open(self, url, form_name=None, **kwds):
""" Opens the given URL, then will automaticall try and 'submit()' the
form given by 'form_name', until there is no such form.
Originated due to the habit of imperial EEE to use these forms to
attempt to prevent scraping.
"""
try:
self.log.debug("Browsing to url: '{0}'.".format(url))
responce = self.open(url, **kwds)
while self.viewing_html():
try:
if form_name:
self.select_form(name=form_name)
else:
self.select_form(nr=0)
self.log.debug("Submitting form '{0}' for redirect."
.format(form_name))
responce = self.submit()
except mechanize.FormNotFoundError:
break
except Error as err:
# If error, report and raise
self.log.error(str(err))
raise
# Return responce body
return responce
def formopen(url, user=None, passwd=None, form_name=None):
""" Convinience function to use the FormRedirectBrowser, adding
authentication as needed.
"""
browser = FormRedirectBrowser()
if user:
browser.add_password(url, user, passwd)
return browser.redirect_open(url, form_name=form_name)
if __name__ == '__main__':
sys.stderr.write("Library functions - import only.")
sys.exit(1)