def login(sess): try: login_url = base + 'loginpage.php' sess.get(login_url) vcode_resp = sess.get(base + 'vcode.php') img = Image.open(BytesIO(vcode_resp.content)) if config.USE_TESSERACT: res, vcode = utils.try_get_vcode(img) else: vcode = utils.prompt_vcode(img) if vcode == '': raise PermissionError if 'Verify Code Wrong!' in sess.post(base + 'login.php', data={ 'user_id': credentials.XDOJ_USERNAME, 'password': credentials.XDOJ_PASSWORD, 'vcode': vcode, 'submit': 'Submit' }).text: raise PermissionError return sess except PermissionError: return login(sess)
def login(r): vcode = '' while len(vcode) is not 4: soup = bs4.BeautifulSoup(r.get(BASE_URL).text, "lxml") img_url = BASE_URL + \ soup.find('img', id='loginform-verifycode-image').get('src') vcv = soup.find('input', type='hidden').get('value') img = Image.open(BytesIO(ses.get(img_url).content)) if config.USE_TESSERACT: # 使用了自定义的语言数据 res, vcode = utils.try_get_vcode(img) else: vcode = utils.prompt_vcode(img) try: if re.findall( r'请修复以下错误<\/p><ul><li>(.*?)<', r.post(BASE_URL + '/login', data={ "LoginForm[username]": USERNAME, "LoginForm[password]": PASSOWORD, "LoginForm[verifyCode]": vcode, "_csrf": vcv, "login-button": "" }).text)[0] == '验证码不正确。': login(r) except: pass
# # xidian-scripts is free software: you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as published by the # Free Software Foundation, either version 3 of the License, or (at your # option) any later version. # # xidian-scripts is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License # for more details. # # You should have received a copy of the GNU Lesser General Public License # along with xidian-scripts. If not, see <http://www.gnu.org/licenses/>. try: USE_TESSERACT = True import os import lib.utils as utils from PIL import Image assert (utils.try_get_vcode( Image.open( os.path.join(os.path.expanduser('~'), '.xidian_scripts', 'config', 'captcha.png')))[1] == '2238') except: USE_TESSERACT = False # export_timetable USE_LATEST_SEMESTER = True # 自动获取学期学年信息, 若为True, 可以不填写下方配置项 SCHOOL_YEAR = (2018, 2019) # 学年度 SEMESTER = '2' # 学期