def register_by_password(cls, student_id: str, password: str, jw_password: str, captcha_ticket: str, captcha_rand: str, remote_addr: str): """使用密码注册 4001 学号为空 4002 密码为空 4010 密码太弱 4003 验证码无效 4501 everyclass-auth 服务错误 """ if not student_id: raise ValueError("Empty student ID") if not password: raise ValueError("Empty password") if not jw_password: raise ValueError("Empty JW password") resp = HttpRpc.call(method='POST', url=f'{BASE_URL}/register/byPassword', data={ 'student_id': student_id, 'password': password, 'jw_password': jw_password, 'captcha_ticket': captcha_ticket, 'captcha_rand': captcha_rand, 'remote_addr': remote_addr }, retry=True) return RegisterByPasswordResponse.make(resp)
def login(cls, student_id: str, password: str, captcha_ticket: str, captcha_rand: str, remote_addr: str): """登录 4001 用户名为空 4002 密码空 4003 验证码验证未通过 4004 学号不存在 4005 此学生未注册 4006 密码错误 """ if not student_id: ValueError("Empty Student ID") if not password: raise ValueError("Empty password") resp = HttpRpc.call(method='GET', url=f'{BASE_URL}/login', data={ 'student_id': student_id, 'password': password, 'captcha_ticket': captcha_ticket, 'captcha_rand': captcha_rand, 'remote_addr': remote_addr }, retry=True) return GeneralResponse.make(resp)
def register_by_email(cls, request_id: str, student_id: str): return HttpRpc.call(method='POST', url=f'{cls.BASE_URL}/register_by_email', data={ 'request_id': request_id, 'student_id': student_id }, retry=True)
def get_rooms(cls) -> Dict[str, Dict[str, List[str]]]: """获得所有校区和楼栋的教室ID""" resp = HttpRpc.call(method="GET", url=f'{cls.BASE_URL}/room/', retry=True) if resp["status"] != "OK": raise RpcException('Entity returns non-success status') return resp["room_group"]
def get_available_rooms(cls, week: int, session: str, campus: str, building: str): """获得指定地点指定时间的可用教室""" resp = HttpRpc.call(method="GET", url=f'{cls.BASE_URL}/room/available?week={week}&session={session}&campus={campus}&building={building}', retry=True) if resp["status"] != "OK": raise RpcException('Entity returns non-success status') return resp["available_room"]
def register_by_password(cls, request_id: str, student_id: str, password: str): return HttpRpc.call(method='POST', url=f'{cls.BASE_URL}/register_by_password', data={ 'request_id': request_id, 'student_id': student_id, 'password': password })
def check_password_strength(cls, password: str): """检查密码强度""" if not password: raise ValueError("Empty password") resp = HttpRpc.call(method='GET', url=f'{BASE_URL}/register/passwordStrengthCheck', data={'password': password}, retry=True) return PasswordStrengthResponse.make(resp)
def get_visitors(cls, student_id: str): """获得访客列表 """ if not student_id: raise ValueError("Empty student ID") resp = HttpRpc.call(method='GET', url=f'{BASE_URL}/visitors', headers={'STUDENT_ID': student_id}, retry=True) return VisitorsResponse.make(resp)
def reset_calendar_token(cls, student_id: str): """清空日历 token """ if not student_id: raise ValueError("Empty student ID") resp = HttpRpc.call(method='POST', url=f'{BASE_URL}/resetCalendarToken', headers={'STUDENT_ID': student_id}, retry=True) return GeneralResponse.make(resp)
def cron_update_remote_manifest(): """更新数据最后更新时间""" from everyclass.rpc.http import HttpRpc # 获取安卓客户端下载链接 android_manifest = HttpRpc.call( method="GET", url="https://everyclass.cdn.admirable.pro/android/manifest.json", retry=True) android_ver = android_manifest['latestVersions']['mainstream'][ 'versionCode'] __app.config['ANDROID_CLIENT_URL'] = android_manifest['releases'][ android_ver]['url'] # 更新数据最后更新时间 _api_server_status = HttpRpc.call( method="GET", url=__app.config['API_SERVER_BASE_URL'] + '/info/service', retry=True, headers={'X-Auth-Token': __app.config['API_SERVER_TOKEN']}) __app.config['DATA_LAST_UPDATE_TIME'] = _api_server_status["data_time"]
def _verify(cls, ticket: str, rand_str: str, user_ip: str) -> bool: params = { "aid": current_app.config['TENCENT_CAPTCHA_AID'], "AppSecretKey": current_app.config['TENCENT_CAPTCHA_SECRET'], "Ticket": ticket, "Randstr": rand_str, "UserIP": user_ip } resp = HttpRpc.call(method="GET", url='https://ssl.captcha.qq.com/ticket/verify', params=params, retry=True) return bool(resp["response"])
def verify_email_token(cls, token: str): """验证邮箱 token 4008 token 为空 4009 token 无效 """ if not token: raise ValueError("Empty token") resp = HttpRpc.call(method='GET', url=f'{BASE_URL}/register/emailVerification', data={'token': token}, retry=True) return GeneralResponse.make(resp)
def register(cls, student_id: str): """检查学号是否已注册 4001 用户名为空 4007 已经注册过了 """ if not student_id: raise ValueError("Empty student ID") resp = HttpRpc.call(method='GET', url=f'{BASE_URL}/register', data={'student_id': student_id}, retry=True) return GeneralResponse.make(resp)
def get_card(cls, semester: str, card_id: str) -> CardResult: """ 根据学期和card ID获得card :param semester: 学期,如 2018-2019-1 :param card_id: card ID :return: """ resp = HttpRpc.call(method="GET", url=f'{cls.BASE_URL}/lesson/{card_id}/timetable/{semester}', retry=True) if resp["status"] != "success": raise RpcException('API Server returns non-success status') search_result = CardResult.make(resp) return search_result
def get_classroom_timetable(cls, semester: str, room_id: str): """ 根据学期和教室ID获得教室课表 :param semester: 学期,如 2018-2019-1 :param room_id: 教室ID :return: """ resp = HttpRpc.call(method="GET", url=f'{cls.BASE_URL}/room/{room_id}/timetable/{semester}', retry=True, headers={'X-Auth-Token': cls.REQUEST_TOKEN}) if resp["status"] != "success": raise RpcException('API Server returns non-success status') search_result = ClassroomTimetableResult.make(resp) return search_result
def get_teacher(cls, teacher_id: str): """ 根据教工号获得教师信息 :param teacher_id: 学号 :return: """ resp = HttpRpc.call(method="GET", url=f'{cls.BASE_URL}/teacher/{teacher_id}', retry=True, headers={'X-Auth-Token': cls.REQUEST_TOKEN}) if resp["status"] != "success": raise RpcException('API Server returns non-success status') search_result = TeacherResult.make(resp) return search_result
def get_student(cls, student_id: str): """ 根据学号获得学生信息 :param student_id: 学号 :return: """ resp = HttpRpc.call(method="GET", url=f'{cls.BASE_URL}/student/{student_id}', retry=True, headers={'X-Auth-Token': cls.REQUEST_TOKEN}) if resp["status"] != "success": raise RpcException('API Server returns non-success status') search_result = StudentResult.make(resp) return search_result
def register_by_email(cls, student_id: str): """使用邮箱验证注册 4001 用户名为空 4007 已经注册过了 4501 未定义的错误 """ if not student_id: raise ValueError("Empty student ID") resp = HttpRpc.call(method='POST', url=f'{BASE_URL}/register/byEmail', data={'student_id': student_id}, retry=True) return GeneralResponse.make(resp)
def get_teacher_timetable(cls, teacher_id: str, semester: str): """ 根据学期和教工号获得老师课表 :param teacher_id: 教工号 :param semester: 学期,如 2018-2019-1 :return: """ resp = HttpRpc.call(method="GET", url=f'{cls.BASE_URL}/teacher/{teacher_id}/timetable/{semester}', retry=True, headers={'X-Auth-Token': cls.REQUEST_TOKEN}) if resp["status"] != "success": raise RpcException('API Server returns non-success status') search_result = TeacherTimetableResult.make(resp) return search_result
def search(cls, keyword: str) -> SearchResult: """搜索 :param keyword: 需要搜索的关键词 :return: 搜索结果列表 """ keyword = keyword.replace("/", "") resp = HttpRpc.call(method="GET", url=f'{cls.BASE_URL}/search/query?key={keyword}', retry=True, headers={'X-Auth-Token': cls.REQUEST_TOKEN}) if resp["status"] != "OK": raise RpcException('API Server returns non-success status') search_result = SearchResult.make(resp) return search_result
def set_privacy_level(cls, student_id: str, privacy_level: int): """设置隐私级别 4101 无效的隐私级别 4100 无效的请求 """ if not student_id: raise ValueError("Empty student ID") if privacy_level is None: # avoid 0 raise ValueError("Empty privacy level") resp = HttpRpc.call(method='POST', url=f'{BASE_URL}/setPreference', data={'privacy_level': privacy_level}, headers={'STUDENT_ID': student_id}, retry=True) return GeneralResponse.make(resp)
def password_verification_status(cls, request_id: str): """检查密码验证的状态 4011 请求 ID 为空 4100 无效的请求 ID 4007 已经注册过了 4200 验证成功 4201 下次查询 4202 密码错误 """ if not request_id: raise ValueError("Empty request ID") resp = HttpRpc.call( method='GET', url=f'{BASE_URL}/register/byPassword/statusRefresh', data={'request_id': request_id}, retry=True) return GeneralResponse.make(resp)
def email_set_password(cls, token: str, password: str): """邮件验证 设置密码 4008 token 为空 4002 密码为空 4009 token 无效 4010 密码强度太低 """ if not token: raise ValueError("Empty token") if not password: raise ValueError("Empty password") resp = HttpRpc.call(method='POST', url=f'{BASE_URL}/register/emailVerification', data={ 'token': token, 'password': password }, retry=True) return EmailSetPasswordResponse.make(resp)
def verify_email_token(cls, token: str): resp = HttpRpc.call(method='POST', url=f'{cls.BASE_URL}/verify_email_token', data={"email_token": token}, retry=True) return VerifyEmailTokenResult.make(resp)
def get_result(cls, request_id: str): resp = HttpRpc.call(method='GET', url=f'{cls.BASE_URL}/get_result', data={'request_id': request_id}, retry=True) return GetResultResult.make(resp)