def handle_filter_empty_classroom(building_id: str, week_of_term: int, day_of_week: int, start_class: int, end_class): cookies = login(NAME, PASSWD) section = 0 for i in range(start_class - 1, end_class): section |= (1 << i) post_data = { "fwzt": "cx", # 查询 "xnm": "2020", # 学年 "xqm": "3", "lh": building_id, # 楼号 "jyfs": "0", # 教育方式??? "zcd": 1 << (week_of_term - 1), # zcd = 2**周次-1 "xqj": day_of_week, # 星期几 "jcd": section, "queryModel.showCount": "1001" } classrooms = session.post(config.empty_classroom_url, post_data, cookies=cookies).json() classrooms = classrooms["items"] res = [] for item in classrooms: res.append({ "location": item["cdlbmc"], "roomId": item["cdmc"], "seats": item["zws"], "seatType": item.get("bz", "正常座椅") }) return {'code': 0, 'data': res}
def handle_list_grade(user_id: int): post_data = {'userId': user_id, 'timestamp': int(time.time() * 1000)} post_data['sign'] = sign(post_data) res_json = session.post(config.grade_list_url, data=post_data).json() if res_json['returnCode'] != '200': custom_abort(-1, res_json['returnMsg']) return {'code': 0, 'data': res_json['data']}
def handle_empty_classroom(building_id: str, week_of_term: int, day_of_week: int, class_of_day: int): cookies = login(NAME, PASSWD) post_data = { "fwzt": "cx", # 查询 "xnm": "2020", # 学年 "xqm": "3", "lh": building_id, # 楼号 "jyfs": "0", # 教育方式??? "zcd": 2**(week_of_term - 1), # zcd = 2**周次-1 "xqj": day_of_week, # 星期几 "jcd": 2**(class_of_day - 1), # jcd = 2**节次-1 "queryModel.showCount": "1001" } items = session.post(config.empty_classroom_url, post_data, cookies=cookies).json() items = items["items"] data = [] for item in items: room = { "location": item["cdlbmc"], "roomId": item["cdmc"], "seats": item["zws"], "seatType": item.get("bz", "正常座椅") } data.append(room) return {'code': 0, 'data': data}
def handle_export_grade(): name = request.args.get('name', type=str) passwd = request.args.get('passwd', type=str) file_type = request.args.get('type', 'pdf') cookies = login(name, passwd) if file_type == 'pdf': post_data1 = {'gsdygx': '10353-zw-mrgs1'} pdf_message = session.post(config.pdf_url1, data=post_data1, cookies=cookies).content.decode() if pdf_message.find('可打印') == -1: custom_abort(-3, pdf_message) pdf_url = session.post(config.pdf_url2, data=post_data1, cookies=cookies).content.decode().replace( '\\', '').replace('\"', '') if pdf_url.find('成功') != -1: pdf_content = session.get('http://222.31.49.139' + pdf_url.split('#')[0], cookies=cookies).content file_name = 'files/%s-grade-%s.pdf' % \ (name, hashlib.md5((name + app_secret).encode('utf-8')).hexdigest()[:5]) with open(file_name, 'wb') as pdf_file: pdf_file.write(pdf_content) upload_to_qiniu(file_name) return { 'code': 0, 'data': { 'url': 'https://blog-cdn.dreace.top/' + file_name } } custom_abort(-3, pdf_url) else: xls_content = session.post(config.excel_url, data=config.post_data, cookies=cookies).content file_name = 'files/%s-grade-%s.xls' % \ (name, hashlib.md5((name + app_secret).encode('utf-8')).hexdigest()[:5]) with open(file_name, 'wb') as xls_file: xls_file.write(xls_content) upload_to_qiniu(file_name) return { 'code': 0, 'message': { 'url': 'https://blog-cdn.dreace.top/' + file_name } }
def handle_timetable(): name = request.args.get('name', '') passwd = request.args.get('passwd', '') cookies = login(name, passwd) post_data = {'xnm': 2020, 'xqm': 3} timetable = session.post(config.timetable_url, data=post_data, cookies=cookies).json() return {'code': 0, 'data': build_timetable_items(timetable)}
def handle_login(): name = request.args.get('name', type=str) passwd = request.args.get('passwd', type=str) if not name or not passwd: custom_abort(-2, '账号密码不能为空') post_data = { 'uname': base64.b64encode(key.encrypt(pad(name))).decode(), 'pwd': base64.b64encode(key.encrypt(pad(passwd))).decode(), 'code': request.args.get('captcha', type=str), 'timestamp': int(time.time() * 1000) } post_data['sign'] = sign(post_data) login_resp = session.post( config.login_url, data=post_data, allow_redirects=False, cookies={'JSESSIONID': request.args.get('JSESSIONID', type=str)}) login_res = login_resp.json() if login_res['returnMsg']: custom_abort(1, login_res['returnMsg']) info_res = session.post(config.info_url, cookies=login_resp.cookies.get_dict()).json() return {'code': 0, 'data': {'id': info_res['data'][0]['sysUser']['id']}}
def handle_timetable_all(): name = request.args.get('name', '') passwd = request.args.get('passwd', '') cookies = login(name, passwd) all_timetable = [] for data in config.term_list: post_data = data['postData'] name = data['name'] timetable = session.post(config.timetable_url, data=post_data, cookies=cookies).json() if len(timetable["kbList"]) == 0: continue info = {'name': name, 'timetable': build_timetable_items(timetable)} all_timetable.append(info) return {'code': 0, 'data': all_timetable}
def login(name: str, passwd: str, disable_cache=False) -> dict: """ 使用用户名和密码登录教务系统 :param name: 用户名(学号) :param passwd: 密码 :param disable_cache: 是否禁用缓存的 cookie,首次登录时应当禁用,防止错误密码也能“登录成功” :return 成功登录后的 cookies """ if not name or not passwd: custom_abort(-3, '账号密码不能为空') if name.strip() != name: custom_abort(-3, '用户名包含空字符') if not disable_cache: cookie_json = redis_session.get("cookie" + name) if cookie_json: cookies = json.loads(cookie_json) r = session.get(config.test_url, allow_redirects=False, cookies=cookies) if r.status_code == 200: redis_session.expire("cookie" + name, 43200) return cookies else: redis_session.delete("cookie" + name) index_response = session.get(config.index_url) csrf_token = re.search('name="csrftoken" value="(.*?)"', index_response.content.decode()).group(1) cookies = index_response.cookies.get_dict() public_key_dict = session.get(config.public_key_url, cookies=cookies).json() public_key = rsa.PublicKey(base_64_to_int(public_key_dict["modulus"]), base_64_to_int(public_key_dict["exponent"])) post_data = { 'csrftoken': csrf_token, 'yhm': name, 'mm': base64.b64encode(rsa.encrypt(passwd.encode(), public_key)) } login_response = session.post(config.index_url, data=post_data, allow_redirects=False, cookies=cookies) if login_response.content.decode().find("不正确") != -1: custom_abort(-3, '账号或密码错误') cookies['JSESSIONID'] = login_response.cookies.get('JSESSIONID') redis_session.set("cookie" + name, json.dumps(cookies), ex=43200) return cookies
def handle_exam(): name = request.args.get('name', '') passwd = request.args.get('passwd', '') cookies = login(name, passwd) post_data = { 'xqm': 3, 'xnm': 2020, 'queryModel.showCount': 500 } items = session.post(config.exam_url, data=post_data, cookies=cookies).json() exam_items = [] for item in items["items"]: exam_items.append({ 'type': item['ksmc'], 'college': item['kkxy'], 'location': item['cdmc'], 'time': item['kssj'], 'name': item['jxbmc'] }) return { 'code': 0, 'data': exam_items }
def handle_class_timetable(class_name: str): if not class_name: custom_abort(-6, '空关键词') cookies = {} try: cookies = login(NAME, PASSWD) except CustomHTTPException: logging.warning('全局账号登录失败') custom_abort(-6, '查询失败') post_data = { 'xnm': '2020', 'xqm': '3', 'xqh_id': '01', 'njdm_id': '', 'jg_id': '', 'zyh_id': '', 'zyfx_id': '', 'bh_id': class_name, '_search': 'false', 'queryModel.showCount': '1', } pre_data_json = session.post(config.pre_class_timetable_url, data=post_data, cookies=cookies).json() if not pre_data_json['items']: custom_abort(-6, '无效的班级号') post_data = { 'xnm': '2020', 'xqm': '3', 'xnmc': '2020-2021', 'xqmmc': '1', 'xqh_id': '01', 'njdm_id': pre_data_json['items'][0]['njdm_id'], 'zyh_id': pre_data_json['items'][0]['zyh_id'], 'bh_id': class_name, 'tjkbzdm': '1', 'tjkbzxsdm': '0', # 'zxszjjs': True } timetable = session.post(config.class_timetable_url, data=post_data, cookies=cookies).json() timetable_items = [] cnt = 0 name_dict = {} for index, table in enumerate(timetable['kbList']): spited = table['jcor'].split('-') if table['kcmc'] not in name_dict: name_dict[table['kcmc']] = cnt cnt += 1 timetable_items.append({ 'name': table.get('kcmc', ''), 'teacher': table.get('xm'), # 哪几周上课,形如”9-14周“ 'weeks': table.get('zcd', ''), 'color': name_dict[table['kcmc']], # 星期几 'dayOfWeek': table.get('xqj', ''), # 第几小节开始 'start': int(spited[0]), # 上几小节 'length': int(spited[1]) - int(spited[0]) + 1, 'building': table.get('xqmc'), 'classroom': table.get('cdmc') }) for d in timetable['sjkList']: timetable_items.append({'name': d['sjkcgs']}) return {'code': 0, 'data': timetable_items}