87 lines
2.5 KiB
Python
Executable file
87 lines
2.5 KiB
Python
Executable file
#!/bin/python3
|
|
|
|
# AGPLv3
|
|
# Created by smolsheep
|
|
# Please don't flood the API.
|
|
|
|
import json, http.client
|
|
from urllib import request
|
|
from rich.console import Console
|
|
from rich.table import Table
|
|
|
|
# Link to get json from
|
|
xon_api = "https://xonotic.lifeisabug.com/endpoint/json"
|
|
|
|
# Create table
|
|
console = Console()
|
|
servlist = Table(show_header=True, expand=True, header_style="bold blue")
|
|
servlist.add_column("", justify="right", max_width=6)
|
|
servlist.add_column("Server", justify="left", ratio=0.1, no_wrap=True)
|
|
servlist.add_column("Mode", justify="center", max_width=6)
|
|
servlist.add_column("", justify="center", ratio=0.05)
|
|
servlist.add_column("", justify="right", max_width=6)
|
|
servlist.add_column("IP", justify="left", min_width=22)
|
|
|
|
# Request json from xon_api
|
|
try:
|
|
response = request.urlopen(xon_api)
|
|
rawdata = response.read()
|
|
except http.client.IncompleteRead as r:
|
|
# This seems to always trigger.
|
|
rawdata = r.partial
|
|
|
|
# Restructure locally before sorting
|
|
data = json.loads(rawdata)
|
|
unsorted = {}
|
|
for rserv in data["server"]:
|
|
srv = data["server"][rserv]
|
|
if 0 < srv["numplayers"]:
|
|
unsorted[srv["name"]] = {
|
|
"num": srv["numplayers"],
|
|
"max": srv["maxplayers"],
|
|
"name": srv["realname"],
|
|
"location": srv["geo"],
|
|
"modes": {0:srv["mode"], 1:srv["mode2"]},
|
|
"map": srv["map"],
|
|
"ip": srv["address"]
|
|
}
|
|
|
|
# Sort by number of players
|
|
sorted = dict(sorted(unsorted.items(), key=lambda pcount: pcount[1]["num"], reverse=True))
|
|
|
|
# Fill out table and do operations to colorize the data
|
|
for server in sorted:
|
|
s = sorted[server]
|
|
num= s["num"]
|
|
max = s["max"]
|
|
if max == num:
|
|
s["countstr"] = f"[bold bright_red]{num}[/bold bright_red]"
|
|
if (max/3) < num :
|
|
s["countstr"] = f"[bold green]{num}[/bold green]"
|
|
else:
|
|
s["countstr"] = f"{num}"
|
|
|
|
main = s["modes"][0]
|
|
min = s["modes"][1]
|
|
if "DUEL" == s["modes"][0]:
|
|
modecolor = "bright_red"
|
|
s["modestr"] = f"[bold {modecolor}]{main}[/bold {modecolor}]"
|
|
elif "CA" == s["modes"][0]:
|
|
modecolor = "bright_cyan"
|
|
s["modestr"] = f"[bold {modecolor}]{main}[/bold {modecolor}]"
|
|
else:
|
|
modecolor = "dark_white"
|
|
s["modestr"] = f"[bold {modecolor}]{main}[/bold {modecolor}]"
|
|
|
|
servlist.add_row(
|
|
s["location"],
|
|
s["name"],
|
|
s["modestr"],
|
|
s["map"],
|
|
s["countstr"],
|
|
s["ip"]
|
|
)
|
|
|
|
console.clear()
|
|
console.print(servlist)
|