Overlay ausgebaut
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
import csv
|
||||
import re
|
||||
import urllib.request
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
|
||||
SPREADSHEET_ID = "1ESSYHbhnAfJ4PMPKxlg43g4B2stShhIKhpE4eYZuW9E"
|
||||
GID = "862418922"
|
||||
CSV_URL = f"https://docs.google.com/spreadsheets/d/{SPREADSHEET_ID}/gviz/tq?tqx=out:csv&gid={GID}"
|
||||
|
||||
def parse_csv_data(reader):
|
||||
users = {}
|
||||
|
||||
# 1. Zeile auslesen (für die Deadline in Zelle A1)
|
||||
# next() holt das nächste (hier: das erste) Element aus dem Reader
|
||||
first_row = next(reader, None)
|
||||
|
||||
# 2. Den restlichen Reader durchlaufen (ab Zeile 2)
|
||||
for row in reader:
|
||||
# Prüfen, ob die Zeile mindestens 5 Spalten hat (wegen Spalte E)
|
||||
# und ob Spalte A (Index 0) nicht leer ist
|
||||
if len(row) >= 12 and row[8].strip():
|
||||
if row[9].strip() == "TRUE":
|
||||
users[row[8].strip()] = "https://i.ibb.co/mzVnwcP/Mario.png"
|
||||
if row[10].strip() == "TRUE":
|
||||
users[row[8].strip()] = "https://i.ibb.co/bjh4pyrN/twitch-removebg-preview.png"
|
||||
|
||||
return users
|
||||
|
||||
|
||||
|
||||
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("twitch_state.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 = "twitch_state.json"
|
||||
max_age_seconds = 60 * 5 # fünf Minuten 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