-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathrogers.py
85 lines (67 loc) · 2.66 KB
/
rogers.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
76
77
78
79
80
81
82
83
84
85
import urllib, urllib2, re
from cookies import Cookies
class Rogers:
"""
@class Rogers
MSO class to handle authorization with the Rogers MSO
"""
@staticmethod
def getID():
return 'Rogers'
@staticmethod
def authorize(streamProvider, username, password):
"""
Perform authorization with Rogers
@param streamProvider the stream provider object. Needs to handle the
getAuthURI.
@param username the username to authenticate with
@param password the password to authenticate with
"""
uri = streamProvider.getAuthURI('Rogers')
jar = Cookies.getCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))#,
#urllib2.HTTPHandler(debuglevel=1),
#urllib2.HTTPSHandler(debuglevel=1))
try:
resp = opener.open(uri)
except:
print "Unable get OAUTH location"
return None
Cookies.saveCookieJar(jar)
html = resp.read()
viewstate = re.search('<input.*__VIEWSTATE.*?value=\"(.*?)\".*?>', html, re.MULTILINE)
if not viewstate:
return None
viewstategen = re.search('<input.*__VIEWSTATEGENERATOR.*?value=\"(.*?)\".*?>', html, re.MULTILINE)
if not viewstategen:
return None
validation = re.search('<input.*__EVENTVALIDATION.*?value=\"(.*?)\".*?>', html, re.MULTILINE)
if not validation:
return None
return Rogers.getOAuthToken(username, password, viewstate.group(1),
viewstategen.group(1), validation.group(1),
resp.url)
@staticmethod
def getOAuthToken(username, password, viewstate, viewstategen, validation, url):
"""
Perform OAuth
@param username the username
@param password the password
@param viewstate the viewstate (form data)
@param validation the validation (form data)
@param url the OAuth URL
"""
jar = Cookies.getCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
values = {'__VIEWSTATE' : viewstate,
'__VIEWSTATEGENERATOR' : viewstategen,
'__EVENTVALIDATION' : validation,
'UserName' : username,
'UserPassword' : password,
'Login' : 'Sign in' }
try:
resp = opener.open(url, urllib.urlencode(values))
except urllib2.URLError, e:
print e.args
Cookies.saveCookieJar(jar)
return True