Skip to content

Update Server Data #11039

Update Server Data

Update Server Data #11039

name: Update Server Data
on:
schedule:
- cron: "*/15 * * * *"
workflow_dispatch:
jobs:
update:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.x"
- name: Install dependencies
run: pip install requests
- name: Update server snapshots
shell: python
run: |
import json, requests, time, os
servers = json.load(open("servers.json"))
now = int(time.time() * 1000) # raw timestamp, no snapping
latest = {}
total_players = 0
for s in servers:
name, ip = s["name"], s["ip"]
try:
r = requests.get(f"https://api.mcsrvstat.us/2/{ip}", timeout=10)
data = r.json()
if data.get("online"):
players = data.get("players", {})
players_online = players.get("online", 0)
playernames = players.get("list", [])
total_players += players_online
latest[name] = {
"time": now,
"online": True,
"players": players_online,
"playernames": playernames,
"max": players.get("max"),
"version": data.get("version"),
"motd": " ".join(data.get("motd", {}).get("clean", [])) if "motd" in data else None
}
else:
latest[name] = {
"time": now,
"online": False,
"players": 0,
"playernames": []
}
except Exception:
latest[name] = {
"time": now,
"online": False,
"players": 0,
"playernames": []
}
latest["_total"] = {"time": now, "players": total_players}
os.makedirs("data", exist_ok=True)
with open("data/server_latest.json", "w") as f:
json.dump(latest, f, indent=2)
history_path = "data/server_history.json"
history = {}
if os.path.exists(history_path):
with open(history_path) as f:
history = json.load(f)
cutoff = now - 24 * 60 * 60 * 1000 # keep 24h only
for name, snap in latest.items():
entry = {"time": now, "players": snap["players"]}
series = history.get(name, [])
series.append(entry)
series = [d for d in series if int(d["time"]) >= cutoff]
history[name] = series
with open(history_path, "w") as f:
json.dump(history, f, indent=2)
- name: Commit and push
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add data/server_latest.json data/server_history.json
git commit -m "Update server data [skip ci]" || echo "No changes"
git push