def check_default_user_cookie(): default_userId = get_default_userId() default_fullname = get_default_fullname() default_nickname = get_default_nickname() print_list = [color.blue(str(default_userId)), color.blue(default_nickname)] print( "=" * 60, "\n默认用户ID:{0[0]},默认用户昵称:{0[1]}".format(print_list), end=" ") cookies = get_cookie(default_userId) if not cookies: print(color.red("【无有效cookie信息,需要登录】")) return [] else: print(color.green("(cookie信息有效)")) return cookies
def refresh_all_cookies(live_time=8.0, display_score=False): # cookie有效时间保持在live_time以上 template_json_str = '''{}''' cookies_json_obj = file.get_json_data("user/cookies.json", template_json_str) need_check = False valid_cookies = [] for uid in cookies_json_obj: cookies_b64 = cookies_json_obj[uid] cookies_bytes = base64.b64decode(cookies_b64) cookie_list = pickle.loads(cookies_bytes) for d in cookie_list: # 检查是否过期 if 'name' in d and 'value' in d and 'expiry' in d and d[ "name"] == "token": remain_time = (int(d['expiry']) - (int)(time.time())) / 3600 print( color.green(uid + "_" + get_nickname(uid) + ",登录剩余有效时间:" + str(int(remain_time * 1000) / 1000) + " 小时."), end="") if remain_time < 0: print(color.red(" 已过期 需要重新登陆,将自动移除此cookie.")) remove_cookie(uid) else: # print(color.blue(" 有效"), end="") valid_cookies.append(cookie_list) if remain_time <= live_time: # 全新cookies的有效时间是12h print(color.red(" 需要刷新")) need_check = True # 暂没有证据表明可以用requests来请求,requests请求的响应不带cookies,不确定会不会更新cookies时间 # (但是万一服务端自动更新了cookie,可以试试12h之后再访问呢?则剩余时间直接设为12即可。有空的伙计可以做个实验) # jar = RequestsCookieJar() # for cookie in cookie_list: # jar.set(cookie['name'], cookie['value']) # new_cookies = requests.get("https://pc.xuexi.cn/points/my-points.html", cookies=jar, # headers={'Cache-Control': 'no-cache'}).cookies.get_dict() # 浏览器登陆方式更新cookie,速度较慢但可靠 driver_login = Mydriver(nohead=False) driver_login.get_url( "https://www.xuexi.cn/notFound.html") driver_login.set_cookies(cookie_list) driver_login.get_url( 'https://pc.xuexi.cn/points/my-points.html') new_cookies = driver_login.get_cookies() driver_login.quit() found_token = False for j in new_cookies: # 检查token if 'name' in j and j["name"] == "token": found_token = True if not found_token: remove_cookie(uid) # cookie不含token则无效,删除cookie else: save_cookies(new_cookies) else: print(color.green(" 无需刷新")) if need_check: # 再执行一遍来检查有效情况 print("再次检查cookies有效时间...") refresh_all_cookies() elif display_score: for cookie in valid_cookies: user_id = get_userId(cookie) print(color.blue(get_fullname(user_id)) + " 的今日得分:") score.show_score(cookie)
'answer': r[1], 'datetime': r[2] }) ###################################将tikuNet表中的题库,插入到tiku表中############### # question = r[1] # answer = r[2] # db = DbTool() # q = db.query('select * from ' + 'tiku' + ' where question = "' + question + '" and answer = "' + answer + '"') # if not len(q): # result = db.execute('insert into ' + 'tiku' + '(question,answer) values (?,?)', (question, answer)) # print(result) ###################################将tikuNet表中的题库,插入到tiku表中############### # return json.dumps(data, ensure_ascii=False) return data if __name__ == '__main__': count = 0 while True: count = count + 1 question = input("开始第{}次挑战答题,请输入题目(输入`exit`退出答题):".format(count)) if question == 'exit': break data = search(keyword=question) print(color.yellow("共查询到{}条数据如下:".format(data['total']))) for i in range(len(data['rows'])): print("Question{}:{}\nAnswer:{}\n".format( i + 1, data['rows'][i]['question'], color.green(data['rows'][i]['answer'])))