def Get_Task_Bar(self) -> None: """ Get_Task_Bar() -> None 获取任务进度 :return: None """ while True: try: html = REQUESTS().Get(url=API().Task_Bar.geturl(), cookies=self.__cookie) data = html.json() for rule in data['data']['dayScoreDtos']: task = self.__info.Task_Bar.get(rule['ruleId']) if not task: ruleId = rule['ruleId'] name = rule['name'] desc = rule['desc'] currentScore = rule['currentScore'] dayMaxScore = rule['dayMaxScore'] task = TASK_INFO(ruleId=ruleId, name=name, desc=desc, currentScore=currentScore, dayMaxScore=dayMaxScore) else: task.Current_Score = rule['currentScore'] self.__info.Update_Task_Bar_Info(task_info=task) break except TypeError: continue
def _Login(self) -> bool: """ _Login() -> Bool 进行登录; 具体流程: 1、开启循环检测二维码状态 1、获取二维码图片 2、显示二维码 3、二维码状态检测 4、根据3的返回值决定: 1、刷新二维码,中断本次循环,再来一次 2、提取Token值,根据选项(持久化)决定是否保持token,关闭二维码容器 :return: Bool,返回值只有True,如未登录则会一直循环 """ self.__network.Clear() self.__driver.get(url=API().Login.geturl()) while True: qr = self.__Get_QR() QR_VESSEL().Show_QR(qr=qr) status = self.__Check_Status() if not status: self.__QR_Refresh() continue else: while self.__driver.current_url != API().Master.geturl(): continue cookies = self.__driver.get_cookies() token = [{cookie['name']: cookie['value']} for cookie in cookies if cookie['name'] == 'token'] if token: INFO_MANAGE().Init(token=token[0]['token']) if OPTIONS().Token: cookie = token[0] html = REQUESTS().Get( url=API().Aggregate_Score.geturl(), cookies=cookie ) data = html.json() user_id = data['data']['userId'] user = USER(user_id=user_id, token=token[0]['token']) if DB_MANAGE().User.Exist_User(user=user): DB_MANAGE().User.Update(user=user) else: DB_MANAGE().User.Insert(user=user) DB_MANAGE().Article.Update_All() DB_MANAGE().Video.Update_All() QR_VESSEL().QR_QUIT() return status
def Get_Daily_Score(self) -> None: """ Get_Daily_Score() -> None 获取每日积分 :return: None """ while True: try: html = REQUESTS().Get(url=API().Daily_Score.geturl(), cookies=self.__cookie) data = html.json() self.__info.Daily_Score = data['data']['score'] break except TypeError: continue
def _Init_Info(self) -> None: """ _Init_Info() -> None 初始化用户id :return: None """ while True: try: html = REQUESTS().Get(url=API().Aggregate_Score.geturl(), cookies=self.__cookie) data = html.json() self.__info.User_Id = data['data']['userId'] break except TypeError: continue
def Init_Article_Video(cls) -> None: """ Init_Article_Video() -> None 从任务表中取出任务,进行解析并按类别填入文章表或视频表 :return: None """ if DB_MANAGE().Task.Exist_Enough(): print("解析任务集中") task = DB_MANAGE().Task.Query() html = REQUESTS.Get(url=task.Link) DB_MANAGE().Task.Update(task=task) try: for temp in html.json(): if temp['type'] == 'tuwen': print("解析为文章任务集") article = ARTICLE(item=temp['itemId'], link=temp['url'], isread=False) DB_MANAGE().Article.Insert(article=article) elif temp['type'] == 'shipin': print("解析为视频任务集") video = VIDEO(item=temp['itemId'], link=temp['url'], isread=False) DB_MANAGE().Video.Insert(video=video) else: print("解析为其他任务集") except JSONDecodeError: print("解析为干扰项") cls.Init_Article_Video()
def Get_Level(self) -> None: """ Get_Level() -> None 获取等级 :return: None """ while True: try: html = REQUESTS().Get(url=API().Level.geturl(), cookies=self.__cookie) data = html.json() self.__info.Level = data['data']['level'] self.__info.Level_Name = data['data']['levelName'] self.__info.Rank_Accumulate_In_Country = \ data['data']['rankAccumulateInCountry'] break except TypeError: continue
def __Init_Task(cls) -> None: """ 任务表初始化(不为空的时候) :return: None """ if DB_MANAGE().Task.Empty(): html = REQUESTS.Get(url=API().Task_Parent.geturl()) parent = html.json() for key in parent.keys(): son = API().Task_Son.geturl().format(channel_id=key) task = TASK(link=son, isread=False) DB_MANAGE().Task.Insert(task=task)
def Assigning_Project_Answer(self, token: str) -> int: """ Assigning_Project_Answer(token: str) -> int 获取最早且未过期的专项答题任务ID :param token: 令牌 :return: int """ cookie = {'token': token} for num in range(5, 0, -1): html = REQUESTS.Get( url=API().Project_Answer_Topics.geturl().format(num=num), cookies=cookie) data = html.json() data = json.loads( base64.b64decode(data['data_str']).decode('utf-8')) for topic in data['list'][::-1]: if (not topic['overdue']) and (not topic['seeSolution']): return topic['id']
def Assigning_Weekly_Answer(self, token: str) -> int: """ Assigning_Weekly_Answer(token: str) -> int 获取本周最早且未做的每周答题任务ID :param token: 令牌 :return: int """ cookie = {'token': token} for num in range(3, 0, -1): html = REQUESTS.Get( url=API().Weekly_Answer_Topics.geturl().format(num=num), cookies=cookie) data = html.json() data = json.loads( base64.b64decode(data['data_str']).decode('utf-8')) for group in data['list'][::-1]: for practice in group['practices'][::-1]: if not practice['seeSolution']: return practice['id']
def __Change_Driver_Cookie(self, key: str, state: str) -> None: """ __Change_Driver_Cookie(key: str, state: str) -> None 此处是应对network中还未查询二维码状态时,所做的措施,用处是加快登录过程,而不需要浏览器 自主添加cookie :param key: 最后一次查询二维码状态的api会返回code(只会允许查询一次,随后就是过期) :param state: iframe链接中提取 :return: None """ url = parse.urlparse(url=key) code = parse.parse_qs(qs=url.query)['loginTmpCode'][0] html = REQUESTS.Get( url=API().Login_Token.geturl().format(code=code, state=state) ) token = html.cookies.get(name='token') cookie = {'domain': '.xuexi.cn', 'name': 'token', 'value': token} self.__driver.add_cookie(cookie_dict=cookie) self.__driver.refresh()
def __Get_QR_Status_Requests(self, qr_data: Dict, qr_state: str): """ __Get_QR_Status_Requests(qr_data: Dict) -> int 获取二维码状态码(requests请求) :param qr_data: 带有二维码编号的data :param qr_state: 二维码的state :return: int 1: 成功 11021: 未登录 11019: 二维码失效 """ html = REQUESTS.Post( url=API().Login_QR_Status.geturl(), data=qr_data ) status = html.json() if status['success']: self.__Change_Driver_Cookie(key=status['data'], state=qr_state) return 1 else: return int(status['code'])