-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_papermc.py
70 lines (52 loc) · 2.3 KB
/
get_papermc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import json
import time
from typing import Union
import requests
from lxml import html
VERSIONS_URL = "https://api.papermc.io/v2/projects/paper/"
BUILDS_URL = "https://api.papermc.io/v2/projects/paper/versions/{version}/"
DOWNLOAD_URL = "https://api.papermc.io/v2/projects/paper/versions/{version}/builds/{build}/downloads/paper-{version}-{build}.jar"
"""CLASS FOR EXECPTIONS"""
"""CHECKS"""
def check_version(version: Union[float, str]) -> bool:
versions = get_versions()
return str(version) in versions
def check_build(version: Union[float, str], build: Union[int, str]) -> bool:
builds = get_builds(version)
try:
return int(build) in builds
except ValueError:
raise ValueError("Build must be an integer or an integer in a string format")
"""FUNCTIONS"""
def get_versions() -> list[str]:
response = requests.get(VERSIONS_URL)
if response.status_code != 200:
raise Exception("Error getting versions")
return json.loads(response.text)["versions"]
def get_builds_raw(version: Union[float, str]) -> list[str]:
response = requests.get(BUILDS_URL.format(version=str(version)))
if response.status_code != 200:
raise Exception("Error getting builds")
return json.loads(response.text)["builds"]
def get_builds(version: Union[float, str]) -> list[str]:
if not check_version(version):
raise Exception("Version not found")
return get_builds_raw(version)
def get_download_url_raw(version: Union[float, str], build: Union[int, str]) -> str:
return DOWNLOAD_URL.format(version=str(version), build=str(build))
def get_download_url(version: Union[float, str], build: Union[int, str]) -> str:
if not check_version(version):
raise Exception("Version not found")
if not check_build(version, build):
raise Exception("Build not found")
return get_download_url_raw(version, build)
def get_latest_version() -> str:
response = requests.get(VERSIONS_URL)
if response.status_code != 200:
raise Exception(f"Error getting latest version | {response.status_code}")
return json.loads(response.text)["versions"][-1]
def get_latest_build(version: Union[float, str]) -> int:
return get_builds(version)[-1]
def get_latest_version_and_build():
version = get_latest_version()
return version, get_latest_build(version)