def save_user(user_id, osu_id, mode): """ 对数据进行保存操作 :param user_id: 用户的qq号 :param osu_id: osuid :param mode: 游玩模式 :return: ok """ o = OsuMySQL() try: # 保存时先进行是否存在的校验 sql_str = "select osu_id, mode from {} where qq_id='{}'".format(config.MYSQL_TBNAME, user_id) select_result = o.query_data(sql_str) if select_result: return "{}: 该账号已经被绑定过了".format(user_id) # 没有查询出值则进行保存处理 sql = "insert into {} values ('{}', '{}', '{}');".format(config.MYSQL_TBNAME, user_id, osu_id, mode) result = o.insert_data(sql) except Exception as e: o.close() return "查询发生异常" if result: return "{}我已经记住你的名字啦!".format(user_id) return "发生异常"
class SaveCookie(object): def __init__(self): self.home_url = "https://osu.ppy.sh/home" self.login_url = "https://osu.ppy.sh/session" self.login_referer = "https://osu.ppy.sh/home" self.mysql_cookie = OsuMySQL() @property def _set_headers(self): """生成headers""" return {"User-Agent": UserAgent(verify_ssl=False).random} @property def _get_login_token(self): """登录主页获取token数据""" headers = self._set_headers # 获取官网主页 response_html = requests.get(self.home_url, headers=headers) element = etree.HTML(response_html.content.decode()) token = element.xpath("//form/input/@value") if len(token) > 0: return token[0] return None @property def get_cookie(self): """登录osu官网并获取cookies""" session = requests.Session() headers = self._set_headers # 设置headers headers["referer"] = self.login_referer # 设置登录账号和密码 data = { "_token": self._get_login_token, "username": LOGIN_OSU_USERNAME, "password": LOGIN_OSU_PASSPORD } response = session.post(self.login_url, headers=headers, data=data) return requests.utils.dict_from_cookiejar(response.cookies) def save_cookie(self): """保存cookie""" # 查询数据库是否存在cookie,因为只有一个账号所以只保存一条数据 try: select_all = "select * from {};".format(MYSQL_TBCOOKIE) res_select = self.mysql_cookie.query_data(select_all) if len(res_select) > 0: # 如果有记录则删除 delete_all = "delete from {};".format(MYSQL_TBCOOKIE) self.mysql_cookie.delete_data(delete_all) cookie_list = [ "{}={}".format(key, value) for key, value in self.get_cookie.items() ] cookie_value = "|".join(cookie_list) # 添加cookie insert_sql = "insert into {} values ('{}');".format( MYSQL_TBCOOKIE, cookie_value) result = self.mysql_cookie.insert_data(insert_sql) print("*" * 50) print(result) return result except: return False finally: self.mysql_cookie.close()