72 lines
2.2 KiB
Python
72 lines
2.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Download the stat-check ELO workbook and write public/elo-data.json.
|
|
|
|
Run from the statcheck-elo-scraper/ directory:
|
|
python refresh_elo.py
|
|
"""
|
|
import csv
|
|
import json
|
|
import sys
|
|
from datetime import datetime, timezone
|
|
from pathlib import Path
|
|
|
|
from download_and_parse import download, xlsx_to_csv
|
|
from pure_python_elo_scrape import EMBED_URL, download_url_for_embed
|
|
|
|
HERE = Path(__file__).parent
|
|
ROOT = HERE.parent
|
|
|
|
|
|
def csv_to_players(csv_path: Path) -> list[dict]:
|
|
players = []
|
|
with csv_path.open(newline="", encoding="utf-8-sig") as f:
|
|
reader = csv.DictReader(f)
|
|
for row in reader:
|
|
name = row.get("Name", "").strip()
|
|
rank_s = row.get("Rank", "").strip()
|
|
elo_s = row.get("Elo", "").strip()
|
|
last_event = row.get("Last Event", "").strip()
|
|
if not (name and rank_s and elo_s):
|
|
continue
|
|
try:
|
|
players.append({
|
|
"name": name,
|
|
"rank": int(rank_s),
|
|
"elo": round(float(elo_s), 1),
|
|
"lastEvent": last_event,
|
|
})
|
|
except ValueError:
|
|
pass
|
|
return players
|
|
|
|
|
|
def main() -> None:
|
|
xlsx_path = Path(sys.argv[1]) if len(sys.argv) > 1 else HERE / "elo-pure.xlsx"
|
|
csv_path = Path(sys.argv[2]) if len(sys.argv) > 2 else HERE / "elo-pure.csv"
|
|
json_path = Path(sys.argv[3]) if len(sys.argv) > 3 else ROOT / "public" / "elo-data.json"
|
|
|
|
print("Resolving OneDrive download URL…")
|
|
url = download_url_for_embed(EMBED_URL)
|
|
|
|
print("Downloading XLSX…")
|
|
download(url, xlsx_path)
|
|
print(f" {xlsx_path.stat().st_size:,} bytes → {xlsx_path}")
|
|
|
|
print("Converting to CSV…")
|
|
xlsx_to_csv(xlsx_path, csv_path)
|
|
print(f" {csv_path.stat().st_size:,} bytes → {csv_path}")
|
|
|
|
print("Building elo-data.json…")
|
|
players = csv_to_players(csv_path)
|
|
payload = {
|
|
"updated": datetime.now(timezone.utc).isoformat(),
|
|
"players": players,
|
|
}
|
|
json_path.parent.mkdir(parents=True, exist_ok=True)
|
|
json_path.write_text(json.dumps(payload, separators=(",", ":")), encoding="utf-8")
|
|
print(f" {len(players):,} players → {json_path}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|