11import time
2- from io import BytesIO
32
4- from PIL import Image
53from bs4 import BeautifulSoup
64
75from ..AuthSession import AuthSession
86from ..utils .page import parse_form_hidden_inputs
97from ..utils .aes import encrypt
8+ from ..utils .vcode import get_solver
109
1110
1211class IDSSession (AuthSession ):
1312 cookie_name = 'ids'
1413
15- def __init__ (
16- self , target , username , password ,
17- * args , ** kwargs
18- ):
14+ def __init__ (self , target , username , password ):
1915 super ().__init__ (f'{ self .cookie_name } _{ username } ' )
2016 if self .is_logged_in ():
2117 return
@@ -25,34 +21,23 @@ def __init__(
2521 'http://ids.xidian.edu.cn/authserver/login' ,
2622 params = {'service' : target }
2723 ).text
28- is_need_captcha = self .get (
24+ while self .get (
2925 'https://ids.xidian.edu.cn/authserver/checkNeedCaptcha.htl' ,
3026 params = {'username' : username , '_' : str (int (time .time () * 1000 ))}
31- ).json ()['isNeed' ]
32- if is_need_captcha :
33- captcha = self .get (
34- 'https://ids.xidian.edu.cn/authserver/common/openSliderCaptcha.htl' ,
35- params = {'_' : str (int (time .time () * 1000 ))}
36- )
37- # 返回: {
38- # 'bigImage': ..., # 背景图(base64)
39- # 'smallImage': ..., # 滑块图(base64)
40- # 'tagWidth": 93, # 无用, 恒93
41- # 'yHeight': 0 # 无用, 恒0
42- # }
43- img = Image .open (BytesIO (captcha .json ()['bigImage' ]))
44- img .show ()
45- # move_len: 背景图左侧到滑块目标位置左侧的宽度
46- move_len = input ('滑块位移:' )
47- # canvasLength: canvas宽度, 硬编码280
48- # moveLength: 按比例缩放后的滑块位移, 有容错
49- verify = self .post (
27+ ).json ()['isNeed' ]:
28+ if self .post (
5029 'https://ids.xidian.edu.cn/authserver/common/verifySliderCaptcha.htl' ,
5130 data = {
52- 'canvasLength' : '280' ,
53- 'moveLength' : str (move_len * 280 // img .width )
31+ # canvasLength: canvas宽度, 硬编码280
32+ 'canvasLength' : '280' ,
33+ # moveLength: 按比例缩放后的滑块位移, 有容错
34+ 'moveLength' : str (get_solver ('ids.xidian.edu.cn' )(self .get (
35+ 'https://ids.xidian.edu.cn/authserver/common/openSliderCaptcha.htl' ,
36+ params = {'_' : str (int (time .time () * 1000 ))}
37+ ).json ()))
5438 }
55- )
39+ ).json ()['errorMsg' ] == 'success' :
40+ break
5641 # 返回: {
5742 # 'errorCode': ..., # 验证通过时为1
5843 # 'errorMsg': ... # 验证通过时为'success'
0 commit comments