def get_all_mark_details(): sno = request.values.get("username") password = request.values.get("password") """此处预留token验证方法""" s, err = LoginFuc(sno, password).login() r = s.get(app.config["INDEX_URL"], headers=app.config["HEADERS"]) soup = BeautifulSoup(r.content, "html.parser", from_encoding="gb18030") stu = StudentInfo(soup) sno_id = str(stu.studentid()) res = s.get(app.config["STUDENT_INFO_URL"] + sno_id + "=", headers=app.config["HEADERS"]) soup1 = BeautifulSoup(res.content, "html.parser", from_encoding="gb18030") compulsory_title_list = list() compulsory_td_list = list() stu_mark = soup1.find('table', attrs={'class': 'table'}) try: tag = stu_mark.select("th") # 获取顶部信息 except Exception: return Response(json_results(success=False, errmsg="该期间暂不提供查询", errcode=40413), content_type="application/json;charset=utf-8") for i in range(len(tag)): compulsory_title_list.append(tag[i].getText()) more = stu_mark.select("td") for j in range(len(more)): compulsory_td_list.append(more[j].getText()) compulsory_none_item = [None if x == '' else x for x in compulsory_td_list] n = len(tag) mark_result = slice_list(compulsory_none_item, n) compulsory_mark_data = CompulsoryInfo(mark_result).get_compulsory_info() try: optional = soup1.find_all('table', attrs={'class': 'table'})[1] optional_title = optional.select('th') optional_td = optional.select('td') optional_td_list = list() for j in range(len(optional_td)): optional_td_list.append(optional_td[j].getText().strip()) optional_none_item = [None if x == '' else x for x in optional_td_list] n = len(optional_title) optional_data_result = slice_list(optional_none_item, n) optional_mark_data = OptionalInfo( optional_data_result).get_optional_info() except: optional_mark_data = None student_credits = AcademicCredits(soup1).get_grade_point() return Response(json_results(msg="成绩信息返回正常", compulsory=compulsory_mark_data, optional=optional_mark_data, credits=student_credits), content_type="application/json;charset=utf-8")
def get_grade_point(self): grade_point_list = [] point = self.soup.find_all('table')[-1] point_select_tr = point.select('td') for i in range(len(point_select_tr)): grade_point_list.append(point_select_tr[i].getText().strip()) for del_item in range(len(grade_point_list)): if '' in grade_point_list: grade_point_list.remove('') slicing_grade_point_list = grade_point_list[4:] n = 2 grade_point_list_result = slice_list(slicing_grade_point_list, n) return grade_point_list_result
def curr_timetable(): if request.method == "GET": abort(403) else: sno = request.values.get("username") password = request.values.get("password") """此处预留token验证方法""" s, err = LoginFuc(sno, password).login() res = s.get(app.config["TIMETABLE_URL"], headers=app.config["HEADERS"]) soup = BeautifulSoup(res.content, "html.parser", from_encoding="gb18030") timetable_td = soup.find_all('td', attrs={'align': 'left'}) try: week = soup.find_all('span', attrs={'class': 'style16'})[-1] week_text = week.getText().strip().split(': ')[-1] re_week = re.findall(r"[0-9]*", week_text) find_term = soup.find_all('span', attrs={'class': 'style17'})[0] split_term = find_term.getText().strip().split('\n') term = split_term[0] for item in range(len(re_week)): if '' in re_week: re_week.remove('') result_week = int(re_week[0]) except IndexError: result_week = "假期" term = "假期" timetable = list() for item in range(len(timetable_td)): if len(timetable_td[item].getText()) <= 1: timetable.append(None) else: timetable.append( TimetableInfo( timetable_td[item].getText()).timetable_more_info()) n = 7 # 设置分割点长度 data = slice_list(timetable, n) for i in range(len(data)): data[i].insert(0, None) data[i].pop(-1) return Response(json_results(msg="课表数据返回正常", week=result_week, term=term, timetable=data), content_type="application/json;charset=utf-8")
def new_timetable(): if request.method == "GET": abort(403) else: sno = request.values.get("username") password = request.values.get("password") """此处预留token验证方法""" s, err = LoginFuc(sno, password).login() current_result = s.get(app.config["TIMETABLE_URL"], headers=app.config["HEADERS"]) current_soup = BeautifulSoup(current_result.content, 'html.parser', from_encoding="gb18030") current_soup_td = current_soup.find_all('td', attrs={'align': 'left'}) s_mon = datetime.datetime.now().strftime('%m') s_years = datetime.datetime.now().strftime('%Y') # 此处预留一个月份判断学年bug,待修复 if int(s_mon) > 8: school_year = int(s_years) semester = 2 elif 1 <= int(s_mon) <= 2: school_year = int(s_years) - 1 semester = 2 else: school_year = int(s_years) semester = 1 new_timetable_url = app.config["TIMETABLE_URL"] + "?schoolyear=" + str( school_year) + "&semester=" + str(semester) # 新学期课表url new_timetable_result = s.get(new_timetable_url, headers=app.config["HEADERS"]) new_timetable_soup = BeautifulSoup(new_timetable_result.content, 'html.parser', from_encoding="gb18030") new_timetable_td = new_timetable_soup.find_all('td', attrs={'align': 'left'}) timetable = list() current_timetable = list() for item in range(len(current_soup_td)): if len(current_soup_td[item].getText()) <= 1: current_timetable.append(None) else: current_timetable.append( TimetableInfo( current_soup_td[item].getText()).timetable_more_info()) for item in range(len(new_timetable_td)): if len(new_timetable_td[item].getText()) <= 1: timetable.append(None) else: timetable.append( TimetableInfo(new_timetable_td[item].getText()). timetable_more_info()) # 对比两个课表是否一致 周数必须大于15才可以启用 if timetable == current_timetable: return Response(json_results(success=False, errmsg="没有新学期课表信息", errcode=40010), content_type="application/json;charset=utf-8") else: # 设置分割点长度 data = slice_list(timetable, 7) for i in range(len(data)): data[i].insert(0, None) data[i].pop(-1) return Response(json_results(msg="新学期课表信息返回正常", timetable=data), content_type="application/json;charset=utf-8")
def get_exam(): sno = request.values.get("username") password = request.values.get("password") """此处预留token验证方法""" s, err = LoginFuc(sno, password).login() r = s.get(app.config["INDEX_URL"], headers=app.config["HEADERS"]) soup = BeautifulSoup(r.content, "html.parser", from_encoding="gb18030") stu = StudentInfo(soup) sno_no_id = str(stu.no_studentid()) res = s.get(app.config["STUDENT_EXAM_URL"] + sno_no_id, headers=app.config["HEADERS"]) soup1 = BeautifulSoup(res.content, "html.parser", from_encoding="gb18030") exam_info_list = list() '''考试API''' try: exam_details = soup1.find_all('table', attrs={'class': 'table'})[0] exam_title = exam_details.select('th') exam_info = exam_details.select('td') for i in range(len(exam_info)): exam_info_list.append( exam_details.select('td')[i].getText().strip()) n = len(exam_title) return Response(json_results(msg="考试时间信息返回正常", exam=slice_list(exam_info_list, n)), content_type="application/json;charset=utf-8") except: try: unfinished_details = soup1.find_all( 'table', attrs={'align': 'center'})[-1].select('td')[0].getText() if "未完成" in unfinished_details: return Response(json_results(success=False, errmsg=unfinished_details, errcode=-4, exam=None), content_type="application/json;charset=utf-8") except: res2 = s.get(app.config["STUDENT_RE_EXAM_URL"] + sno_no_id, headers=app.config["HEADERS"]) soup2 = BeautifulSoup(res2.content, 'html.parser', from_encoding="gb18030") exam_info_list2 = list() try: exam_details1 = soup2.find_all('table', attrs={'class': 'table'})[0] exam_title1 = exam_details1.select('th') exam_info1 = exam_details1.select('td') for i in range(len(exam_info1)): exam_info_list2.append( exam_details1.select('td')[i].getText().strip()) n = len(exam_title1) return Response(json_results(msg="重考考试时间信息返回正常", exam=slice_list( exam_info_list2, n)), content_type="application/json;charset=utf-8") except: reExam_details = soup2.find_all( 'table', attrs={'align': 'center'})[-1].select('td')[0].getText() if "未公布" in reExam_details: return Response( json_results(success=False, errmsg="暂无考试信息", errcode=-4, exam=None), content_type="application/json;charset=utf-8") return Response(json_results(success=False, errmsg="暂无考试信息", errcode=-4, exam=None), content_type="application/json;charset=utf-8")