def _login(self, login_data=None): login_data = { 'number': login_data['student_id'], 'passwd': login_data['password'], 'captcha': self._get_captcha(), 'select': 'cert_no', 'returnUrl': '' } resp = req(self.URLS['LOGIN'], 'post', data=login_data, cookies=self.cookies) if resp is None: return Config.SERVER_MSG['SERVER_ERROR'] if resp.url == self.URLS['LOGIN_SUCCESS']: api.logger.info('[+] ID: %s login lib successfully.' % login_data['number']) return Config.SERVER_MSG['LOGIN_SUCCESS'] elif self.URLS['WRONG_PASS_FINGER'].encode('utf8') in resp.content: api.logger.warning('[+] ID: %s login lib failed.' % login_data['number']) return Config.SERVER_MSG['WRONG_PASSWORD'] elif self.URLS['WRONG_CAPTCHA_FINGER'] in resp.content: # 验证码错误,重试 api.logger.critical('[+] ID: %s crack captcha failed.' % login_data['number']) return Config.SERVER_MSG['WRONG_CAPTCHA'] else: api.logger.error('[-] unknown error.') return Config.SERVER_MSG['SERVER_ERROR']
def _get_score(self, cookies, student_id): """ 获取成绩信息 """ url = ZfParser.get_zf_urls(self.ZF_URLS['SCORE'], student_id) viewstate = self._get_viewstate(url, cookies=cookies) score_data = { '__VIEWSTATE': viewstate, 'ddlXN': '', 'ddlXQ': '', 'Button2': '在校学习成绩查询' } resp = req(url, 'post', data=score_data, referer=self.ZF_URLS['LOGIN'], cookies=cookies, host=self.host) if resp is None or resp.text is None: api.logger.warning('[+] got %s cert score failed.' % student_id) return "[]" content = resp.text res = ZfParser.parse_zf_score(content) api.logger.info('[+] got %s score successfully.' % student_id) return json.dumps(res, ensure_ascii=False)
def _get_search(self): resp = req(self.URLS['SEARCH'], 'get', cookies=self.cookies) if resp is None: return "[]" content = resp.content res = LibParser.parse_lib_search(content) api.logger.info('[+] ID: %s got lib search record.' % login_data['number']) return json.dumps(res, ensure_ascii=False)
def _get_payment_detail(self): resp = req(self.URLS['FINE_DETAIL'], 'get', cookies=self.cookies) if resp is None: return "[]" content = resp.content res = LibParser.parse_lib_common(content, tr_start=1, tr_end=-1) api.logger.info('[+] ID: %s got lib payment details.' % login_data['number']) return json.dumps(res, ensure_ascii=False)
def _get_book_shelf(self): resp = req(self.URLS['BOOKSHELF'], 'get', cookies=self.cookies) if resp is None: return "[]" content = resp.content res = LibParser.parse_lib_shelf(content) api.logger.info('[+] ID: %s got lib book shelf.' % login_data['number']) return json.dumps(res, ensure_ascii=False)
def _get_payment(self): resp = req(self.URLS['FINE'], 'get', cookies=self.cookies) if resp is None: return "[]" content = resp.content res = LibParser.parse_lib_common(content, tr_start=1, tr_end=-1) api.logger.info('[+] ID: %s got lib payment.' % login_data['number']) return json.dumps(res, ensure_ascii=False)
def _get_info(self): resp = req(self.URLS['INFO'], 'get', cookies=self.cookies) if resp is None: return "[]" content = resp.content res = LibParser.parse_lib_info(content) api.logger.info('[+] ID: %s got lib info.' % login_data['number']) return json.dumps(res, ensure_ascii=False)
def _get_current_list(self): resp = req(self.URLS['CURRENT'], 'get', cookies=self.cookies) if resp is None: return "[]" content = resp.content res = LibParser.parse_lib_curlst(content) api.logger.info('[+] ID: %s got lib current list.' % login_data['number']) return json.dumps(res, ensure_ascii=False)
def _get_recommend(self): resp = req(self.URLS['RECOMMEND'], 'get', cookies=self.cookies) if resp is None: return "[]" content = resp.content res = LibParser.parse_lib_common(content, tr_start=1) api.logger.info('[+] ID: %s got lib recommend.' % login_data['number']) return json.dumps(res, ensure_ascii=False)
def _get_captcha(self, crack=True): resp = req(self.URLS['CAPTCHA'], 'get') self.cookies = resp.cookies i = Image.open(StringIO(resp.content)) if self.debug: i.save('lib_captcha.png') if crack: guess = pytesseract.image_to_string(i) # i.close() return guess else: return resp.content
def _get_viewstate(self, url, cookies=None): """ 获取表单viewstate """ resp = req(url, 'get', referer=self.ZF_URLS['LOGIN'], cookies=cookies) if resp is None: return Config.SERVER_MSG['SERVER_ERROR'] res = re.search(self.vs_regex, resp.text, re.S) if res is None: return Config.SERVER_MSG['SERVER_ERROR'] viewstate = res.group(1) return viewstate
def _get_history(self): data = {'para_string': 'all', 'topage': 1} resp = req(self.URLS['HISTORY'], 'post', data=data, cookies=self.cookies) if resp is None: return "[]" content = resp.content res = LibParser.parse_lib_common(content, tr_start=1, td_start=1) api.logger.info('[+] ID: %s got lib history.' % login_data['number']) return json.dumps(res, ensure_ascii=False)
def _login(self, login_data): resp = req(self.URLS['COOKIE'], 'get', host=self.host) if resp is None: return Config.SERVER_MSG['SERVER_ERROR'] api.logger.info('[+] ID: %s got ehome cookies.' % login_data['number']) self.cookies = resp.cookies payload = { 'email': login_data['student_id'], 'password': login_data['password'] } resp = req(self.URLS['LOGIN'], 'post', data=payload, cookies=self.cookies) # 校园系统bug 无需这一步 # if resp.url != self.URLS['LOGIN_SUCCESS']: # return Config.SERVER_MSG['WRONG_PASSWORD'] if resp is None: return Config.SERVER_MSG['SERVER_ERROR'] api.logger.info('[+] ID: %s login ehome.' % login_data['student_id']) self.iplanet = resp.history[0].cookies self.session.cookies = self.iplanet
def _get_history(self): data = { 'para_string': 'all', 'topage': 1 } resp = req(self.URLS['HISTORY'], 'post', data=data, cookies=self.cookies) if resp is None: return "[]" content = resp.content res = LibParser.parse_lib_common(content, tr_start=1, td_start=1) api.logger.info('[+] ID: %s got lib history.' % login_data['number']) return json.dumps(res, ensure_ascii=False)
def _get_cert_score(self, cookies, student_id): """ 获取等级考试成绩信息 """ url = ZfParser.get_zf_urls(self.ZF_URLS['CERT_SCORE'], student_id) resp = req(url, 'get', cookies=cookies, referer=self.ZF_URLS['LOGIN']) if resp is None or resp.text is None: api.logger.warning('[+] got %s cert score failed.' % student_id) return "[]" content = resp.text res = ZfParser.parse_zf_cert_score(content) api.logger.info('[+] got %s cert score successfully.' % student_id) return json.dumps(res, ensure_ascii=False)
def _get_personal_info(self, cookies, student_id): """ 获取个人信息 """ url = ZfParser.get_zf_urls(self.ZF_URLS['INFO'], student_id) resp = req(url, 'get', referer=self.ZF_URLS['LOGIN'], cookies=cookies) if resp is None: api.logger.warning('[-] got %s personal info failed.' % student_id) return Config.SERVER_MSG['SERVER_ERROR'] content = resp.text res = ZfParser.parse_zf_info(content) api.logger.info('[+] got %s personal info successfully.' % student_id) return json.dumps(res, ensure_ascii=False)
def _get_img(self, cookies, student_id): """ 保存个人照片 """ img_url = ZfParser.get_zf_urls(self.ZF_URLS['IMG'], student_id) resp = req(img_url, 'get', referer=self.ZF_URLS['LOGIN'], cookies=cookies, host=self.host) if resp is None: return '' i = Image.open(StringIO(resp.content)) if self.debug: i.save(student_id+'.jpg') api.logger.info('[+] got %s image successfully.' % student_id) url = save_to_qiniu(i) i.close() return url
def get_captcha(self): """ 验证码暂时需要用户输入 模拟教务处验证码获取 http://jwxt.njupt.edu.cn/CheckCode.aspx <img src="http://jwxt.njupt.edu.cn/CheckCode.aspx"> TODO: 1. 识别验证码, 参考:http://blog.rijnx.com/post/ZF-Checkcode-Verify :return: captcha图片流, 正方登录cookie """ resp = req(self.ZF_URLS['CAPTCHA'], 'get') if resp is None: return Config.SERVER_MSG['SERVER_ERROR'] if self.debug: i = Image.open(StringIO(resp.content)) i.save('test.gif') return resp.content, resp.cookies
def _login(self, login_data=None, cookies=None): """ 登录正方 :param login_data: :param cookies: :return: (student_id, 登录结果) """ viewstate = self._get_viewstate(self.ZF_URLS['LOGIN']) if viewstate == Config.SERVER_MSG['SERVER_ERROR']: return Config.SERVER_MSG['SERVER_ERROR'] login_data = { '__VIEWSTATE': viewstate, 'txtUserName': login_data['student_id'], 'TextBox2': login_data['zf_password'], 'txtSecretCode': login_data['login_captcha'], 'RadioButtonList1': '学生', 'Button1': '登录', 'lbLanguange': '', 'hidPdrs': '', 'hidsc': '' } resp = req(self.ZF_URLS['LOGIN'], 'post', referer=self.ZF_URLS['LOGIN'], data=login_data, cookies=cookies) if resp is None: return Config.SERVER_MSG['SERVER_ERROR'] if resp.url.startswith(self.ZF_URLS['LOGIN_SUCCESS']): api.logger.info('[+] ID: %s login zf successfully.' % (login_data['txtUserName'])) msg = Config.SERVER_MSG['LOGIN_SUCCESS'] elif self.ZF_URLS['WRONG_CAPTCHA_FINGER'] in resp.text: msg = Config.SERVER_MSG['WRONG_CAPTCHA'] elif self.ZF_URLS['INVALID_CAPTCHA_FINGER'] in resp.text: msg = Config.SERVER_MSG['INVALID_USERNAME'] elif self.ZF_URLS['WRONG_PASS_FINGER'] in resp.text: api.logger.warning('[-] ID: %s login zf failed.' % (login_data['txtUserName'])) msg = Config.SERVER_MSG['WRONG_PASSWORD'] else: msg = Config.SERVER_MSG['SERVER_ERROR'] return login_data['txtUserName'], msg
def _get_img(self, cookies, student_id): """ 保存个人照片 """ img_url = ZfParser.get_zf_urls(self.ZF_URLS['IMG'], student_id) resp = req(img_url, 'get', referer=self.ZF_URLS['LOGIN'], cookies=cookies, host=self.host) if resp is None: return '' i = Image.open(StringIO(resp.content)) if self.debug: i.save(student_id + '.jpg') api.logger.info('[+] got %s image successfully.' % student_id) url = save_to_qiniu(i) i.close() return url