homepage/homepage/main.py

108 lines
3.3 KiB
Python
Raw Normal View History

2021-03-22 16:39:31 +01:00
#!/usr/bin/env python3
2021-03-23 14:37:38 +01:00
from bottle import route, run, static_file, template
from os import listdir
from os.path import join as pathjoin
from random import random, choice
2021-03-24 16:40:17 +01:00
from mcrcon import MCRcon
2021-03-24 16:02:57 +01:00
import configparser
import requests
import hashlib
import base64
import json
2021-03-24 16:40:17 +01:00
import re
2021-03-23 14:37:38 +01:00
# echo -n "["; ls | xargs -I{} echo -n \"{}\",; echo "]"
BG_IMAGES = listdir("./static/img/background")
2021-03-24 01:50:28 +01:00
news = """
=== 2021-03-24 ===
* TestABC
* FooBar
=== 2020-xx-xx ===
* Map now available
"""
2021-03-23 14:37:38 +01:00
@route("/")
def index():
2021-03-24 16:40:17 +01:00
players = currentPlayerData()
2021-04-30 02:12:50 +02:00
return template("html/index.html", **{"player_count": players["count"]["current"] , "max_players": players["count"]["max"], "news": news, "donations": donations()})
2021-03-23 14:37:38 +01:00
@route("/getPlayerSkins")
def getPlayerSkins():
players = currentPlayerData()
skins = []
for p in players["players"]:
hash = fetchSkin(p["uuid"])
skins.append(hash)
return {"skins": skins}
2021-03-23 14:37:38 +01:00
@route("/img/bg.png")
def random_bg_image():
bg_file = choice(BG_IMAGES)
print(bg_file)
response = static_file(bg_file, root="static/img/background")
response.set_header("Cache-Control", "no-cache")
response.set_header("Cache-Control", "no-store")
response.set_header("Pragma-Directive", "no-cache")
response.set_header("Cache-Directive", "no-cache")
response.set_header("Pragma", "no-cache")
response.set_header("Expires", "0")
return response
@route("/<path:path>")
def callback(path):
return static_file(path, root="static")
2021-03-24 16:40:17 +01:00
def currentPlayerData():
player = {}
with MCRcon(CONFIG["mcrcon"]["host"], CONFIG["mcrcon"]["password"], port=int(CONFIG["mcrcon"]["port"])) as mcr:
resp = mcr.command("list uuids")
resp = resp.split(":")
2021-03-31 12:09:13 +02:00
player_string = resp[1].strip()
if player_string != "":
player_list = player_string.split(",")
else:
player_list = []
2021-03-24 16:40:17 +01:00
player_list = list(map(lambda x: x.strip(), player_list))
count_regex = re.match("There are ([0-9]+) of a max of ([0-9]+) players online", resp[0])
num_players = count_regex.group(1)
max_players = count_regex.group(2)
player["count"] = {}
player["count"]["current"] = num_players
player["count"]["max"] = max_players
player["players"] = []
for p in player_list:
data = p.split(" ")
name = data[0].strip()
uuid = data[1].strip().replace("(", "").replace(")", "")
player["players"].append({"name": name, "uuid": uuid})
return player
def fetchSkin(uuid):
profile = requests.get("https://sessionserver.mojang.com/session/minecraft/profile/{}".format(uuid)).json()
properties = profile.get("properties")
skin = list(filter(lambda x: x.get("name", "") == "textures", properties))[0]
skin_json = json.loads(base64.b64decode(skin.get("value")))
skin_url = skin_json.get("textures").get("SKIN").get("url")
skin_req = requests.get(skin_url)
player_hash = hashlib.sha256(uuid.encode()).hexdigest()
with open('./static/img/skins/{}'.format(player_hash), 'wb') as f:
f.write(skin_req.content)
return player_hash
2021-03-24 16:02:57 +01:00
def parseConfig():
config = configparser.ConfigParser()
config.read("config.ini")
return config
2021-04-30 02:12:50 +02:00
def donations():
with open("donations.txt", "r") as donation_file:
donations = donation_file.read()
return donations
2021-03-24 16:02:57 +01:00
CONFIG = parseConfig()
2021-03-23 14:37:38 +01:00
run(host="localhost", port=8080)