def get_access_token(self, refresh=False): if not refresh: # 检查变量 if self.token and self.token[1] > time.time(): return self.token # 检查文件 template_json_str = '''[]''' token_json_obj = file.get_json_data("user/wechat_token.json", template_json_str) if token_json_obj and token_json_obj[1] > time.time(): self.token = token_json_obj return self.token # 获取新token appid = cfg_get("addition.wechat.appid", "") appsecret = cfg_get("addition.wechat.appsecret", "") url_token = 'https://api.weixin.qq.com/cgi-bin/token?' res = requests.get(url=url_token, params={ "grant_type": 'client_credential', 'appid': appid, 'secret': appsecret, }).json() token = res.get('access_token') expires = int(res.get('expires_in')) - 10 + time.time() self.token = [token, expires] file.save_json_data("user/wechat_token.json", self.token) return self.token
def get_uid(oid): json_str = '''[]''' json_obj = file.get_json_data("user/wechat_bind.json", json_str) wx_list = list(filter(lambda w: w["openId"] == oid, json_obj)) if wx_list: return wx_list[0]["accountId"] else: return ""
def get_user_status(): template_json_str = '''{\n "#-说明1":"此文件是保存用户数据及登陆状态的配置文件",''' + \ '''\n "#-说明2":"程序会自动读写该文件。",''' + \ '''\n "#-说明3":"如不熟悉,请勿自行修改内容。错误修改可能导致程序崩溃",''' + \ '''\n "#____________________________________________________________":"",''' + \ '''\n "last_userId":0,\n "userId_mapping":{\n "0":"default"\n }\n}''' status = file.get_json_data("user/user_status.json", template_json_str) save_user_status(status) # print(status) return status
def save_cookies(cookies): # print(type(cookies), cookies) template_json_str = '''{}''' cookies_json_obj = file.get_json_data("user/cookies.json", template_json_str) userId = get_userId(cookies) cookies_bytes = pickle.dumps(cookies) cookies_b64 = base64.b64encode(cookies_bytes) cookies_json_obj[str(userId)] = str(cookies_b64, encoding='utf-8') # print(type(cookies_json_obj), cookies_json_obj) file.save_json_data("user/cookies.json", cookies_json_obj)
def get_opendid_by_uid(self, uid): """ 账号换绑定的openid,没有则返回主账号 """ json_str = '''[]''' json_obj = file.get_json_data("user/wechat_bind.json", json_str) wx_list = list( filter(lambda w: w["accountId"] == uid or w["openId"] == uid, json_obj)) if wx_list: return wx_list[0]["openId"] else: return self.openid
def wechat_unbind(msg: MessageInfo): """ 解绑微信号 """ args = msg.content.split(" ") if len(args) == 2: json_str = '''[]''' json_obj = file.get_json_data("user/wechat_bind.json", json_str) wx_list = list(filter(lambda w: w["openId"] == args[1], json_obj)) if wx_list: index = json_obj.index(wx_list[0]) json_obj.pop(index) file.save_json_data("user/wechat_bind.json", json_obj) return msg.returnXml("解绑成功") else: return msg.returnXml("账号编码错误或该编码未绑定账号") else: return msg.returnXml("参数格式错误")
def wechat_bind(msg: MessageInfo): """ 绑定微信号 """ args = msg.content.split(" ") if len(args) == 3: json_str = '''[]''' json_obj = file.get_json_data("user/wechat_bind.json", json_str) wx_list = list(filter(lambda w: w["openId"] == args[1], json_obj)) if wx_list: index = json_obj.index(wx_list[0]) json_obj[index]["accountId"] = args[2] else: json_obj.append({"openId": args[1], "accountId": args[2]}) file.save_json_data("user/wechat_bind.json", json_obj) return msg.returnXml("绑定成功") else: return msg.returnXml("参数格式错误")
def get_cookie(userId): userId = str(userId) template_json_str = '''{}''' cookies_json_obj = file.get_json_data("user/cookies.json", template_json_str) for i in cookies_json_obj: if (i == userId): cookies_b64 = cookies_json_obj[i] 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: expiry_timestamp = int(d['expiry']) if expiry_timestamp > (int)(time.time()): pass else: return [] return cookie_list return []
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)
def get_article_video_json(): template_json_str = '''{"#此文件记录用户的视频和文章的浏览进度":"","article_index":{},"video_index":{}}''' article_video_json = file.get_json_data("user/article_video_index.json", template_json_str) return article_video_json
def remove_cookie(uid): template_json_str = '''{}''' cookies_json_obj = file.get_json_data("user/cookies.json", template_json_str) cookies_json_obj.pop(str(uid)) file.save_json_data("user/cookies.json", cookies_json_obj)