def get_login(self, school, **kwargs): ''' 登录入口 与 异常处理 ''' args = (school.login_url, school.exist_verify) try: res = self._get_api(*args, **kwargs) except OtherException as reqe: raise LoginException(self.code, to_text(str(reqe))) except RequestException: if school.proxies and not self.user.proxy_state: # 使用内网代理 if self._switch_proxy(): # 存在内网代理会话 return True try: res = self._get_api(*args, **kwargs) except RequestException: raise LoginException(self.code, '教务系统异常,切用代理登录失败') else: if self.user.proxy_state: raise LoginException(self.code, '教务系统异常,使用代理登录失败') else: raise LoginException(self.code, '教务系统外网异常') self._handle_login_result(res, *args, **kwargs)
def _get_login_result_tip(self, html): """ 获取获取html的弹框提示信息 """ tip = get_alert_tip(html) if tip: return tip raise LoginException(self.code, '教务系统请求异常')
def get_login(self, school, **kwargs): ''' 登录入口 与 异常处理 ''' args = (school.login_url, school.exist_verify) try: res = self._get_api(*args, **kwargs) except RequestException: if school.proxies and not school.use_proxy: # 使用内网代理 self._set_proxy() try: res = self._get_api(*args, **kwargs) except RequestException: raise LoginException(self.code, '教务系统异常,使用代理登录失败') else: raise LoginException(self.code, '教务系统外网异常') self._handle_login_result(res, *args, **kwargs)
def _handle_login_result(self, res): ''' 处理页面弹框信息 ''' if res is True: # 登录成功 return tip = get_alert_tip(res.text) if tip: if tip == '验证码不正确!!': raise CheckCodeException(self.code, tip) raise IdentityException(self.code, tip) raise LoginException(self.code, '教务系统请求异常')
def get_login(self, school, **kwargs): ''' 登录入口 与 异常处理 ''' args = (school.login_url, school.exist_verify) try: res = self._get_api(*args, **kwargs) except OtherException as reqe: raise LoginException(self.school_code, to_text(str(reqe))) except RequestException: if school.proxies and not self.user.proxy_state: # 使用内网代理 proxy_session = self._switch_proxy() if proxy_session: # 存在内网代理会话 return True try: res = self._get_api(*args, **kwargs) except RequestException: raise LoginException(self.school_code, '教务系统异常,切用代理登录失败') else: if self.user.proxy_state: raise LoginException(self.school_code, '教务系统异常,使用代理登录失败') else: raise LoginException(self.school_code, '教务系统外网异常') # 处理登录结果 try: self._handle_login_result(res) except CheckCodeException: try: # 验证码错误时,再次登录 res = self._get_api(*args, **kwargs) except RequestException: raise LoginException(self.school_code, '教务系统请求异常') else: self._handle_login_result(res) return True
def _handle_login_result(self, res, *args, **kwargs): # 登录成功之后,教务系统会返回 302 跳转 if res.status_code == 500: raise LoginException(self.code, '教务系统请求异常') elif res.status_code != 302: tip = self._get_login_result_tip(res.text) if tip == '验证码不正确!!': # 首次验证码错误,则再次登录 res = self._get_api(*args, **kwargs) if res.status_code != 302: tip = self._get_login_result_tip(res.text) if tip == '验证码不正确!!': raise CheckCodeException(self.code, tip) raise IdentityException(self.code, tip)