Skip to content

Commit

Permalink
Removes old caching, vertical center image, logic
Browse files Browse the repository at this point in the history
  • Loading branch information
p0ns committed Oct 3, 2023
1 parent 561f9ec commit b1bc3a4
Showing 1 changed file with 39 additions and 56 deletions.
95 changes: 39 additions & 56 deletions apps/minecraftserver/minecraft_server.star
Original file line number Diff line number Diff line change
Expand Up @@ -5,75 +5,58 @@ Description: View Minecraft Server Activity and icon.
Author: Michael Blades
"""

load("cache.star", "cache")
load("encoding/base64.star", "base64")
load("encoding/json.star", "json")
load("http.star", "http")
load("render.star", "render")
load("schema.star", "schema")

def main(config):
minecraftURL = config.get("server", "mc.azitoth.com")
result_cached = cache.get("".join(["api_result_", minecraftURL]))
if result_cached != None:
resultData = base64.decode(result_cached)
result = json.decode(resultData)
onlinePlayers = result["players"]["online"]
maxPlayers = result["players"]["max"]
motd = result["motd"]["clean"][0]
motd2 = result["motd"]["clean"][1]
iconURL = result["icon"].split(",")[1]
else:
apiURL = "".join(["https://api.mcsrvstat.us/2/", minecraftURL])
result = http.get(apiURL)
if result.status_code != 200:
fail("Minecraft API request failed with status %d", result.status_code)

# TODO: Determine if this cache call can be converted to the new HTTP cache.
cache.set(
"".join(["api_result_", minecraftURL]),
base64.encode(result.body()),
ttl_seconds = 300,
)
onlinePlayers = result.json()["players"]["online"]
maxPlayers = result.json()["players"]["max"]
motd = result.json()["motd"]["clean"][0]
motd2 = result.json()["motd"]["clean"][1]
iconURL = result.json()["icon"].split(",")[1]
apiURL = "".join(["https://api.mcsrvstat.us/2/", minecraftURL])
result = http.get(apiURL, ttl_seconds = 300)
if result.status_code != 200:
fail("Minecraft API request failed with status %d", result.status_code)
result_json = result.json()
onlinePlayers = result_json["players"]["online"] if "players" in result_json else 0
maxPlayers = result_json["players"]["max"] if "players" in result_json else 0
motd = result_json["motd"]["clean"][0] if len(result_json["motd"]["clean"]) > 0 else ""
motd2 = result_json["motd"]["clean"][1] if len(result_json["motd"]["clean"]) > 1 else ""

iconURL = result_json["icon"].split(",")[1] if "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qcAGSNKrSPH4+Tu3Xosx+7m9/fOtJjXori62YcyfH3HnrDg9f/AirebscOf3K1qnRJfHyWhTZdF01xyVIarRGBG6sKubrBmM0wzxBomQ63N66nOj+8biIf7jaK/8cD+PVhx64QD5IyIsI9e/b8vj3PkPYrZm6lnvPnmR5o8Zlarj3wfz+PI6+kkb2wc6FE/uLkg8OFzLI491euOxQv8xuPf9y986VymbgbECqwPH7dvjJ95+/eYn4P/rpdy7y9Scu8eNHH2J2uObMhWNpOWvuu7I3++p03TzQde7SsXHxVB2rV5b7jcvP3sLr73yUu+94gZ899dL/8P4FMC0IrZd+CyYAAAAvdEVYdENvbW1lbnQAR0lGIHJlc2l6ZWQgb24gaHR0cHM6Ly9lemdpZi5jb20vcmVzaXploju4sgAAACV0RVh0ZGF0ZTpjcmVhdGUAMjAyMy0xMC0wMVQwNzoxMzowMSswMDowMA9WCEoAAAAldEVYdGRhdGU6bW9kaWZ5ADIwMjMtMTAtMDFUMDc6MTM6MDErMDA6MDB+C7D2AAAAKHRFWHRkYXRlOnRpbWVzdGFtcAAyMDIzLTEwLTAxVDA3OjEzOjA0KzAwOjAweya+jgAAABJ0RVh0U29mdHdhcmUAZXpnaWYuY29toMOzWAAAAABJRU5ErkJggg=="

serverIcon = base64.decode(iconURL)

return render.Root(
child = render.Column(
children = [
render.Row(
children = [
render.Image(src = serverIcon, width = 25, height = 25),
render.Column(
children = [
render.Marquee(
width = 40,
child = render.Text(
"%d Online" % onlinePlayers,
),
),
render.Marquee(
width = 40,
child = render.Text("%d Max" % maxPlayers),
),
render.Marquee(
width = 64,
child = render.Text("%s" % motd),
child = render.Box(
render.Row(
cross_align = "center",
children = [
render.Image(src = serverIcon, width = 25, height = 25),
render.Column(
children = [
render.Marquee(
width = 40,
child = render.Text(
"%d Online" % onlinePlayers,
),
render.Marquee(
width = 64,
child = render.Text("%s" % motd2),
),
],
),
],
),
],
),
render.Marquee(
width = 40,
child = render.Text("%d Max" % maxPlayers),
),
render.Marquee(
width = 64,
child = render.Text("%s" % motd),
),
render.Marquee(
width = 64,
child = render.Text("%s" % motd2),
),
],
),
],
),
),
)

Expand Down

0 comments on commit b1bc3a4

Please sign in to comment.