diff --git a/app/classes/minecraft/serverjars.py b/app/classes/minecraft/serverjars.py index 447cf80b..55e98a80 100644 --- a/app/classes/minecraft/serverjars.py +++ b/app/classes/minecraft/serverjars.py @@ -11,12 +11,14 @@ from app.classes.models.server_permissions import PermissionsServers from app.classes.shared.websocket_manager import WebSocketManager logger = logging.getLogger(__name__) +MINEACADEMYJARS = ["paper", "folia"] class ServerJars: def __init__(self, helper): self.helper = helper self.base_url = "https://serverjars.com" + self.academy = "https://mineacademy.org" def _get_api_result(self, call_url: str): full_url = f"{self.base_url}{call_url}" @@ -38,6 +40,23 @@ class ServerJars: return api_response + def get_paper_versions(self, project): + try: + response = requests.get( + f"https://api.papermc.io/v2/projects/{project}/", timeout=2 + ) + response.raise_for_status() + api_data = json.loads(response.content) + except Exception as e: + logger.error( + f"Unable to load https://api.papermc.io/v2/projects/{project}/" + f"api due to error: {e}" + ) + return {} + versions = api_data.get("versions", []) + versions.reverse() + return versions + def _read_cache(self): cache_file = self.helper.serverjar_cache cache = {} @@ -95,6 +114,8 @@ class ServerJars: for j in data["types"].get(s): versions = self._get_jar_details(j, s) data["types"][s].update({j: versions}) + for item in MINEACADEMYJARS: + data["types"]["servers"][item] = self.get_paper_versions(item) # save our cache try: with open(cache_file, "w", encoding="utf-8") as f: @@ -133,6 +154,8 @@ class ServerJars: for j in data["types"].get(s): versions = self._get_jar_details(j, s) data["types"][s].update({j: versions}) + for item in MINEACADEMYJARS: + data["types"]["servers"][item] = self.get_paper_versions() # save our cache try: with open(cache_file, "w", encoding="utf-8") as f: @@ -171,7 +194,12 @@ class ServerJars: def a_download_jar(self, jar, server, version, path, server_id): # delaying download for server register to finish time.sleep(3) - fetch_url = f"{self.base_url}/api/fetchJar/{jar}/{server}/{version}" + if server not in MINEACADEMYJARS: + fetch_url = f"{self.base_url}/api/fetchJar/{jar}/{server}/{version}" + else: + fetch_url = ( + f"https://mineacademy.org/api/{server}/{str(version).replace('.', '_')}" + ) server_users = PermissionsServers.get_server_user_list(server_id) # We need to make sure the server is registered before