From b70469e15c38c0edef27130b622b122e7f4d68a3 Mon Sep 17 00:00:00 2001 From: GDcheeriosYT Date: Sun, 21 Jul 2024 01:50:49 -0500 Subject: [PATCH] Remove ServerData --- crap/ServerData.py | 53 --------- main.py | 283 +++++++++++++-------------------------------- 2 files changed, 82 insertions(+), 254 deletions(-) delete mode 100644 crap/ServerData.py diff --git a/crap/ServerData.py b/crap/ServerData.py deleted file mode 100644 index 9c999e44..00000000 --- a/crap/ServerData.py +++ /dev/null @@ -1,53 +0,0 @@ -import json - -from crap.Event import Event -from crap.ApiCall import ApiCall -from crap.ApiType import ApiType -from crap.StatusHandler import StatusHandler - - -class ServerData: - # osu - osu_player_json = json.load(open("player_data.json")) - - # api - API_history = [] - API_rate_hour = 0 - API_rate_minute = 0 - API_rate_second = 0 - API_total_second = 0 - API_most_common = None - API_occurrences = {} - for type in ApiType: - API_occurrences[str(type.value)] = 0 - - # status - token_status = StatusHandler("token") - account_status = StatusHandler("account") - osu_status = StatusHandler("osu") - gqc_status = StatusHandler("Gentry's Quest Classic") - gq_status = StatusHandler("Gentry's Quest") - - # events - on_api = Event("OnApi") - - @staticmethod - def api_call(type: ApiType) -> None: - api_call = ApiCall(type) - print(f"handling Api call {api_call.id} [{api_call.type}]") - ServerData.API_history.append(api_call) - type = str(api_call.type.value) - ServerData.API_occurrences[type] += 1 - - current = api_call.timestamp.now() - # ServerData.API_rate_hour = int((len(ServerData.API_history) / (current.second / 3600))) - # ServerData.API_rate_minute = int((len(ServerData.API_history) / (current.second / 60))) - # ServerData.API_rate_second = int((len(ServerData.API_history) / current.second)) - ServerData.on_api() - - @staticmethod - def get_occurrences(): - return { - 'names': [ApiType(int(type_str)).name for type_str in ServerData.API_occurrences.keys()], - 'values': [ServerData.API_occurrences.get(type_str) for type_str in ServerData.API_occurrences.keys()] - } diff --git a/main.py b/main.py index 51a5c3a0..2bf77509 100644 --- a/main.py +++ b/main.py @@ -2,12 +2,10 @@ import asyncio import atexit import json -import math +import logging import random import string -import traceback import urllib -import logging # flask packages import requests @@ -29,9 +27,6 @@ from crap.osu_crap.PlayerList import PlayerList from crap.osu_crap.MatchHandler import MatchHandler -from crap.ServerData import ServerData -from crap.ApiType import ApiType - from crap.gentrys_quest_crap.GQManager import GQManager # global vars @@ -74,38 +69,18 @@ def exit_func(): atexit.register(exit_func) -ServerData.on_api += lambda: socketio.emit('status receive', - { - 'total apis': len(ServerData.API_history), - 'aph': ServerData.API_rate_hour, - 'apm': ServerData.API_rate_minute, - 'aps': ServerData.API_rate_second, - 'api values': ServerData.get_occurrences()["values"], - 'token status': ServerData.token_status.health, - 'account status': ServerData.account_status.health, - 'osu status': ServerData.osu_status.health, - 'gqc status': ServerData.gqc_status.health, - 'gq status': ServerData.gq_status.health - }) - # # @app.route("/api/generate-token") async def generate_token(): - ServerData.api_call(ApiType.TokenGenerate) - try: - token = "" - for i in range(32): - token += random.choice(string.ascii_letters) + token = "" + for i in range(32): + token += random.choice(string.ascii_letters) - DB.do("INSERT INTO tokens values (%s);", params=(token,)) - ServerData.token_status.successful() - return token - except: - ServerData.token_status.unsuccessful() - return "False" + DB.do("INSERT INTO tokens values (%s);", params=(token,)) + return token @app.route("/api/clear-tokens") @@ -130,33 +105,20 @@ def verify_token(token): # @app.route("/api/account/create/++") async def account_create(username, password, email, osu_id=0, about_me=""): - ServerData.api_call(ApiType.AccountCreate) - - # try: password = str(password) password = str(bcrypt.generate_password_hash(password))[2:-1] Account.create(username, password, email, osu_id, about_me) - ServerData.account_status.successful() - # except: - ServerData.account_status.unsuccessful() - @app.route("/api/account/login/+") async def login(username, password): - ServerData.api_call(ApiType.AccountLogIn) - try: - account = Account(username) - if account: - if bcrypt.check_password_hash(account.password, password): - ServerData.account_status.successful() - return account - - ServerData.account_status.successful() - return "incorrect info" - except: - ServerData.account_status.unsuccessful() + account = Account(username) + if account: + if bcrypt.check_password_hash(account.password, password): + return account + + return "incorrect info" @app.route('/login', methods=['POST']) @@ -176,15 +138,9 @@ def login_cookie(): @app.route("/account/signout") async def signout(): - ServerData.api_call(ApiType.AccountLogOut) - - try: - resp = make_response(render_template('account/login.html')) - resp.delete_cookie('userID') - ServerData.account_status.successful() - return resp - except: - ServerData.account_status.unsuccessful() + resp = make_response(render_template('account/login.html')) + resp.delete_cookie('userID') + return resp @app.route("/create-account", methods=['POST']) @@ -217,36 +173,23 @@ def create_account(): @app.route("/api/account/change-pfp", methods=["POST"]) def change_profile_picture(): - ServerData.api_call(ApiType.AccountChangePfp) - - try: - id = request.cookies.get('userID') - account_data = Account(id) - account_data.pfp = request.form.get("url") - ServerData.account_status.successful() - return render_template('account/user-profile.html', - account=account_data - ) - except: - ServerData.account_status.unsuccessful() + id = request.cookies.get('userID') + account_data = Account(id) + account_data.pfp = request.form.get("url") + return render_template('account/user-profile.html', + account=account_data + ) @app.route("/api/account/change-username", methods=["POST"]) async def change_username(): - ServerData.api_call(ApiType.AccountChangeUsername) - - try: - id = request.cookies.get("userID") - account = Account(id) - username = request.form.get("username") - if not Account.name_exists(username): - Account.change_username(int(id), username) - ServerData.account_status.successful() + id = request.cookies.get("userID") + account = Account(id) + username = request.form.get("username") + if not Account.name_exists(username): + Account.change_username(int(id), username) - return redirect(f'/user/{account.id}') - - except: - ServerData.account_status.unsuccessful() + return redirect(f'/user/{account.id}') @app.route("/api/account/grab/") @@ -260,70 +203,56 @@ async def grab_account(idname): # osu auth stuff @app.route('/code_grab') def code_grab(): - ServerData.api_call(ApiType.OsuAuthenticate) + code = urllib.parse.parse_qs(request.query_string.decode('utf-8'))["code"][0] - try: - code = urllib.parse.parse_qs(request.query_string.decode('utf-8'))["code"][0] - - response = requests.post("https://osu.ppy.sh/oauth/token", - json={'client_id': client.osu_client_id, - 'code': code, - 'client_secret': client.osu_secret, - 'grant_type': 'authorization_code', - 'redirect_uri': f"{client.domain}/code_grab", - 'scope': 'public'}, - headers={'Accept': 'application/json', - 'Content-Type': 'application/json'}) - - response = response.json() - - user_info = requests.get("https://osu.ppy.sh/api/v2/me/osu", headers={ - 'Accept': 'application/json', - 'Content-Type': 'application/json', - 'Authorization': f"Bearer {response['access_token']}" - }).json() - - info = { - "username": user_info["username"], - "id": user_info["id"], - "avatar": f"https://a.ppy.sh/{user_info['id']}", - "background url": user_info["cover_url"] - } + response = requests.post("https://osu.ppy.sh/oauth/token", + json={'client_id': client.osu_client_id, + 'code': code, + 'client_secret': client.osu_secret, + 'grant_type': 'authorization_code', + 'redirect_uri': f"{client.domain}/code_grab", + 'scope': 'public'}, + headers={'Accept': 'application/json', + 'Content-Type': 'application/json'}) - ServerData.osu_status.successful() - return redirect(f"/account/create?osu_info={json.dumps(info)}") - except: - ServerData.osu_status.unsuccessful() + response = response.json() + + user_info = requests.get("https://osu.ppy.sh/api/v2/me/osu", headers={ + 'Accept': 'application/json', + 'Content-Type': 'application/json', + 'Authorization': f"Bearer {response['access_token']}" + }).json() + + info = { + "username": user_info["username"], + "id": user_info["id"], + "avatar": f"https://a.ppy.sh/{user_info['id']}", + "background url": user_info["cover_url"] + } + + return redirect(f"/account/create?osu_info={json.dumps(info)}") # @app.route("/api/get_match/") async def grabber(match_name, from_socket: bool = False): - if not from_socket: - ServerData.api_call(ApiType.OsuMatchGrab) - - try: - match = match_handler.get_match(match_name) + match = match_handler.get_match(match_name) - new_dict = {} - for player in match.players: - new_dict[player.id] = { - "background url": player.background, - "score": match.get_score(player), - "rank": player.rank, - "playcount": match.get_playcount(player), - "liveStatus": None if player.id not in live_player_status else live_player_status[player.id], - } + new_dict = {} + for player in match.players: + new_dict[player.id] = { + "background url": player.background, + "score": match.get_score(player), + "rank": player.rank, + "playcount": match.get_playcount(player), + "liveStatus": None if player.id not in live_player_status else live_player_status[player.id], + } - for team in match.team_data: - for player_ref in team.players: - new_dict[player_ref.id]["team"] = f"{team.jsonify()}" + for team in match.team_data: + for player_ref in team.players: + new_dict[player_ref.id]["team"] = f"{team.jsonify()}" - ServerData.osu_status.successful() - return new_dict - except Exception as e: - traceback.print_exc() - ServerData.osu_status.unsuccessful() + return new_dict # @@ -332,35 +261,21 @@ async def grabber(match_name, from_socket: bool = False): @app.route("/refresh/", methods=['GET', 'POST']) async def web_player_refresh(player_id): - ServerData.api_call(ApiType.OsuRefresh) - - try: - player = PlayerList.get_users([player_id])[0] - player.update_data() - ServerData.osu_status.successful() - return player.jsonify() - except: - ServerData.osu_status.unsuccessful() - - return "{}" + player = PlayerList.get_users([player_id])[0] + player.update_data() + return player.jsonify() @app.route("/api/grab/", methods=['GET']) async def all_grabber(ids): - ServerData.api_call(ApiType.OsuIdGrab) - try: - id_list = ids.split("+") + id_list = ids.split("+") - data = PlayerList.get_users(id_list) - player_list = [] - for player in data: - player_list.append(player.jsonify()) + data = PlayerList.get_users(id_list) + player_list = [] + for player in data: + player_list.append(player.jsonify()) - ServerData.osu_status.successful() - return player_list - except: - return "{}" - ServerData.osu_status.unsuccessful() + return player_list # @@ -369,49 +284,25 @@ async def all_grabber(ids): @app.route("/api/live/del/", methods=["POST"]) async def del_live_status(id): global live_player_status - ServerData.api_call(ApiType.OsuLiveDelete) - - try: - live_player_status.pop(int(id)) - ServerData.osu_status.successful() - except: - ServerData.osu_status.unsuccessful() + live_player_status.pop(int(id)) @app.route("/api/live/get/", methods=["get"]) async def get_live_status(id): - ServerData.api_call(ApiType.OsuLiveGet) - - try: - player_status = live_player_status[id] - ServerData.osu_status.successful() - return player_status - except: - ServerData.osu_status.unsuccessful() + player_status = live_player_status[id] + return player_status @app.route("/api/live/get", methods=["get"]) async def get_all_live_status(): - ServerData.api_call(ApiType.OsuLiveGet) - - try: - ServerData.osu_status.successful() - return live_player_status - except: - ServerData.osu_status.unsuccessful() + return live_player_status @app.route("/api/live/update/", methods=["POST"]) async def update_live_status(id): global live_player_status - ServerData.api_call(ApiType.OsuLiveUpdate) - - try: - info = request.json - live_player_status[id] = info - ServerData.osu_status.successful() - except: - ServerData.osu_status.unsuccessful() + info = request.json + live_player_status[id] = info # @@ -447,7 +338,6 @@ async def gq_submit_leaderboard(leaderboard, user, score): @app.route("/api/gqc/get-leaderboard/+", methods=["GET"]) async def get_gq_leaderboard(start, display_number): - ServerData.api_call(ApiType.GQLeaderboard) return GQManager.get_leaderboard(True, int(start), int(display_number)) @@ -473,15 +363,6 @@ async def about(): return render_template("about.html") -@app.route("/status") -async def status(): - return render_template( - "status.html", - server_data=ServerData, - api_enum=ApiType - ) - - @app.route("/down") async def down(): return render_template("down.html")