Initialer Commit
This commit is contained in:
@@ -0,0 +1,155 @@
|
||||
import csv
|
||||
import re
|
||||
import urllib.request
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
|
||||
SPREADSHEET_ID = "1bsO07WG6ymV4UzHZyB0ayrKCn2zv67SpFaeE4Zmghcc"
|
||||
GID = "1669168528"
|
||||
CSV_URL = f"https://docs.google.com/spreadsheets/d/{SPREADSHEET_ID}/gviz/tq?tqx=out:csv&gid={GID}"
|
||||
|
||||
|
||||
def parse_title(title_str):
|
||||
match = re.search(r"(.+?)\s+S(\d+)L(\d+)", title_str, re.IGNORECASE)
|
||||
if match:
|
||||
return {
|
||||
"game": match.group(1).strip(),
|
||||
"season": int(match.group(2)),
|
||||
"level": int(match.group(3)),
|
||||
}
|
||||
return {"raw": title_str.strip()}
|
||||
|
||||
|
||||
def parse_meta(meta_str):
|
||||
median_m = re.search(r"Median:\s*([\d,\.]+)", meta_str)
|
||||
runners_m = re.search(r"Runners:\s*(\d+)", meta_str)
|
||||
subs_m = re.search(r"Submissions:\s*(\d+)", meta_str)
|
||||
best_m = re.search(
|
||||
r"Community Best:\s*([\d,\.]+)\s*\((?:P1:\s*([\d,\.]+)\s*\|\s*P2:\s*([\d,\.]+))\)",
|
||||
meta_str,
|
||||
)
|
||||
|
||||
return {
|
||||
"median": median_m.group(1).replace(",", ".") if median_m else None,
|
||||
"runners": int(runners_m.group(1)) if runners_m else 0,
|
||||
"submissions": int(subs_m.group(1)) if subs_m else None,
|
||||
"communityBest": (
|
||||
{
|
||||
"total": best_m.group(1).replace(",", "."),
|
||||
"p1": best_m.group(2).replace(",", ".") if best_m.group(2) else None,
|
||||
"p2": best_m.group(3).replace(",", ".") if best_m.group(3) else None,
|
||||
}
|
||||
if best_m
|
||||
else None
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
def parse_csv_data(reader):
|
||||
rows = list(reader)
|
||||
if not rows or len(rows) < 4:
|
||||
return {"error": "Keine ausreichenden Daten gefunden"}
|
||||
|
||||
# Zeile 1 (Index 0): Titel aus Spalte B (Index 1)
|
||||
raw_title = rows[0][1] if len(rows[0]) > 1 else ""
|
||||
title_parsed = parse_title(raw_title)
|
||||
|
||||
# Zeile 2 (Index 1): Meta Info aus Spalte B (Index 1)
|
||||
meta_b2 = rows[1][1] if len(rows[1]) > 1 else ""
|
||||
deadline = rows[1][8] if len(rows[1]) > 8 else "" # Falls Deadline in Spalte I steht
|
||||
|
||||
# Ab Zeile 4 (Index 3): Spieler-Leaderboard
|
||||
leaderboard = []
|
||||
for row in rows[3:]:
|
||||
# Nur wenn ein Spielername in Spalte F (Index 5) existiert
|
||||
if len(row) > 5 and row[5].strip():
|
||||
player_name = row[5].strip()
|
||||
division = row[0].strip() if len(row) > 0 else ""
|
||||
match division:
|
||||
case "C":
|
||||
division = "Champion"
|
||||
case "M":
|
||||
division = "Master"
|
||||
case "D":
|
||||
division = "Diamond"
|
||||
case "P":
|
||||
division = "Platin"
|
||||
case "G":
|
||||
division = "Gold"
|
||||
case "S":
|
||||
division = "Silver"
|
||||
case "B":
|
||||
division = "Bronze"
|
||||
rank_div = row[2].strip() if len(row) > 2 else ""
|
||||
|
||||
# Rang säubern, z. B. "(1)" -> 1
|
||||
rank_overall_raw = row[3].strip() if len(row) > 3 else ""
|
||||
rank_overall_clean = re.sub(r"[^\d]", "", rank_overall_raw)
|
||||
|
||||
part1 = row[6].strip() if len(row) > 6 else ""
|
||||
part2 = row[7].strip() if len(row) > 7 else ""
|
||||
total_time = row[8].strip() if len(row) > 8 else ""
|
||||
online_str = row[9].strip().lower() #if len(row) > 9 else ""
|
||||
leaderboard.append({
|
||||
"division": division,
|
||||
"divisionRank": int(rank_div) if rank_div.isdigit() else 0,
|
||||
"overallRank": int(rank_overall_clean) if rank_overall_clean.isdigit() else None,
|
||||
"player": player_name,
|
||||
"part1": part1,
|
||||
"part2": part2,
|
||||
"totalTime": total_time,
|
||||
"isOnline": online_str in ["on", "online", "true", "1", "ja"],
|
||||
})
|
||||
|
||||
return {
|
||||
"title": raw_title.strip(),
|
||||
"details": title_parsed,
|
||||
"meta": {
|
||||
**parse_meta(meta_b2),
|
||||
"deadline": deadline,
|
||||
},
|
||||
"leaderboardCount": len(leaderboard),
|
||||
"leaderboard": leaderboard,
|
||||
}
|
||||
|
||||
|
||||
def update_data():
|
||||
try:
|
||||
req = urllib.request.Request(CSV_URL, headers={"User-Agent": "Mozilla/5.0"})
|
||||
response = urllib.request.urlopen(req)
|
||||
lines = [line.decode("utf-8") for line in response.readlines()]
|
||||
|
||||
reader = csv.reader(lines)
|
||||
parsed_data = parse_csv_data(reader)
|
||||
with open("data.json", "w") as f:
|
||||
f.write(json.dumps(parsed_data))
|
||||
return parsed_data
|
||||
|
||||
except Exception as e:
|
||||
return {"success": False, "error": str(e)}
|
||||
|
||||
|
||||
def read_data():
|
||||
file_path = "data.json"
|
||||
max_age_seconds = 60 # 1 Minute Cache-Dauer
|
||||
|
||||
# Prüfen, ob die Datei existiert und wie alt sie ist
|
||||
if os.path.exists(file_path):
|
||||
# Mtime = Letzte Änderungszeit der Datei in Sekunden seit Epoch
|
||||
file_age = time.time() - os.path.getmtime(file_path)
|
||||
|
||||
if file_age > max_age_seconds:
|
||||
update_data()
|
||||
else:
|
||||
# Falls die Datei noch gar nicht existiert, direkt initial Daten holen
|
||||
update_data()
|
||||
|
||||
# Nach dem potenziellen Update die Datei einlesen
|
||||
with open(file_path, "r", encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# bindet an 0.0.0.0, damit es im gesamten Netzwerk erreichbar ist
|
||||
read_data()
|
||||
Reference in New Issue
Block a user