def __init__(self, file_name, beatmap_id=None, beatmapset_id=None): self.ali = lib.alioss.AliOss() self.file_name = re.sub(r"\<|\>|\/|\\|\||\:|\"|\*|\?", "", file_name) self.search_url = config.MAP_SEARCH_BASE_URL.format(beatmap_id) self.map_download_url = config.MAP_DOWNLOAD_BASE_URL.format(beatmapset_id) self.mysql_cookie = OsuMySQL()
def update_mode(user_id, mode): """ 对模式进行修改 :param user_id: 用户的qq号 :param mode: 模式 0 1 2 3 :return: 保存成功信息 """ o = OsuMySQL() try: sql_str = "update {} SET mode={} where qq_id={}".format(config.MYSQL_TBNAME, mode, user_id) result = o.update_data(sql_str) except Exception as e: o.close() return None if not result: return None return "{}模式修改完成".format(mode_dict[mode])
def get_osu_id(user_id): """ 根据qq号查询osu的id :param user_id: qq号 :return: osu_id, mode """ sql_str = "select osu_id, mode from {} where qq_id='{}'".format(config.MYSQL_TBNAME, user_id) o = OsuMySQL() try: result = o.query_data(sql_str) o.close() except Exception as e: o.close() return None if result: return {"osu_id": result[0][0], "mode": result[0][1]} return None
def logout_user(user_id): """ 接触qq用户和osu账户的绑定 :param user_id: qq号 :return: 删除成功信息 """ o = OsuMySQL() try: sql_str = "delete from {} where qq_id='{}'".format(config.MYSQL_TBNAME, user_id) o.delete_data(sql_str) except Exception as e: o.close() return return "{}不要丢下我,呜呜呜!!!!".format(user_id)
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 "发生异常"
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()
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()