bug fix related to servers() using the wrong endpoint

+ renaming of `token` to `api_key` for parity with serverseeker's
  discord callback

fix whereis command not being functional

i might've been stupid

add a try-except when the error is not known in servers
This commit is contained in:
magmaus3 2024-07-10 18:37:30 +02:00
parent 3b3ab9263d
commit 3f35fd34a6
Signed by: magmaus3
GPG key ID: 695507CAEE1DB324

View file

@ -18,10 +18,10 @@ class BadRequest(Exception):
class ServerSeeker:
def __init__(self, token: str, endpoint: str = "https://api.serverseeker.net"):
def __init__(self, api_key: str, endpoint: str = "https://api.serverseeker.net"):
self.endpoint = endpoint
self.session = requests.Session()
self.session.headers.update({"Authorization": "Bearer " + token})
self.session.headers.update({"Authorization": "Bearer " + api_key})
self.session.headers.update({"User-Agent": f"ServerSeeker.py/{__version__} +https://git.magmaus3.eu.org/magmaus3/serverseeker.py"})
def server_info(self, ip: str, port: int = 25565) -> dict:
@ -44,7 +44,7 @@ class ServerSeeker:
def servers(self,
country_code: Optional[str] = None,
max_players: Optional[int] = None,
online_players: Optional[tuple[Union[int, str], Union[int, str]]] = None,
online_players: Optional[Union[tuple[Union[int, str], Union[int, str]], int]] = None,
protocol: Optional[int] = None,
software: Optional[str] = None,
cracked: Optional[bool] = None,
@ -52,13 +52,12 @@ class ServerSeeker:
asn: Optional[int] = None,
ignore_modded: Optional[bool] = None,
only_bungeespoofable: Optional[bool] = None
) -> list:
"""
Gives you a list of servers (with matching criteria). Limited to 100 results.
"""
rq = self.session.post(
urljoin(self.endpoint, "/server_info"), json={
urljoin(self.endpoint, "/servers"), json={
"asn": asn,
"country_code": country_code,
"cracked": cracked,
@ -72,12 +71,15 @@ class ServerSeeker:
}
)
if not rq.ok:
if rq.status_code == 400:
raise BadRequest(rq.json()["error"])
elif rq.status_code == 429:
raise LimitExceeded(rq.json()["error"])
elif rq.status_code == 504:
raise QueryTimeout(rq.json()["error"])
try:
if rq.status_code == 400:
raise BadRequest(rq.json()["error"])
elif rq.status_code == 429:
raise LimitExceeded(rq.json()["error"])
elif rq.status_code == 504:
raise QueryTimeout(rq.json()["error"])
except:
raise Exception(rq.text)
return rq.json()
@ -108,15 +110,22 @@ class ServerSeeker:
return rq.json()
def whereis(self, name: Optional[str], uuid: Optional[str]) -> dict:
def whereis(self, name: Optional[str] = None, uuid: Optional[str] = None) -> dict:
"""
Gives you a list of servers where a player was. Limited to 1000 results.
**Attention: You must use name OR uuid as your parameter, not both!**
"""
if name and not uuid:
data = {"name": name}
elif uuid and not name:
data = {"uuid": uuid}
else:
raise ValueError("You can only provide name or uuid, not both")
rq = self.session.post(
urljoin(self.endpoint, "/whereis"), json={"name": "name", "uuid": uuid}
urljoin(self.endpoint, "/whereis"), json=data
)
if not rq.ok:
if rq.status_code == 400:
raise BadRequest(rq.json()["error"])