-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.py
68 lines (58 loc) · 2.36 KB
/
auth.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
import requests
from PIL import Image
from io import BytesIO
from erya import EryaSession
import time
from utils import HEADERS
LOGIN_URL = 'http://passport2.chaoxing.com/login?fid={fid}&refer=http://i.mooc.chaoxing.com/space/index.shtml'
CAPTCHA_URL = 'http://passport2.chaoxing.com/num/code?{ts}'
ERYA_S = 'a7b6c438cbba8ac6bdd24a7c60844b93' # copied from capture, what is that?
def validate_cookies(cookies: dict, init_url):
session = requests.session()
session.cookies.update(cookies)
session.headers.update(HEADERS)
response = session.get(init_url, allow_redirects=False)
if response.status_code == 200:
return True
return False
def erya_login(username, password, school_id) -> dict:
session = requests.session()
session.get(LOGIN_URL.format(fid=school_id))
captcha = ''
while not captcha:
captcha_image_content = session.get(CAPTCHA_URL.format(ts=int(time.time() * 1000))).content
captcha_image = Image.open(BytesIO(captcha_image_content))
captcha_image.show()
captcha = input('Enter code (Press enter to get a new code): ')
data = {
'uname': username,
'password': password,
'numcode': captcha,
'fid': school_id,
'fidName': '',
'vercode': '',
'refer_0x001': 'http%253A%252F%252Fi.mooc.chaoxing.com%252Fspace%252Findex.shtml',
'pid': '-1',
'pidName': '',
'allowJoin': '0',
'isCheckNumCode': '1',
'f': '0',
'productid': '',
'verCode': '',
}
login_resp = session.post(
'http://passport2.chaoxing.com/login?refer=http%3A%2F%2Fi.mooc.chaoxing.com%2Fspace%2Findex.shtml', data=data,
allow_redirects=False, headers=HEADERS)
if login_resp.status_code == 302:
# set cookies
session.get('http://i.mooc.chaoxing.com/space/index.shtml', headers=HEADERS)
session.get('http://www.fanya.chaoxing.com/passport/allHead.shtml', headers=HEADERS)
session.get('http://passport2.chaoxing.com/mooc.jsp?v=0&s={}'.format(ERYA_S), headers=HEADERS)
return session.cookies.get_dict()
else:
# TODO: login failure information
raise RuntimeError('Login failed')
class EryaAuth(EryaSession):
def __init__(self, username, password, school_id):
cookies = erya_login(username, password, school_id)
EryaSession.__init__(self, cookies)