feat: add Stat-Check ELO column with daily auto-refresh
- Scraper (statcheck-elo-scraper/refresh_elo.py) downloads the stat-check ELO workbook from OneDrive and builds public/elo-data.json - GitHub Actions workflow (.github/workflows/refresh-elo.yml) runs daily at 06:00 UTC and commits updated ELO data - Frontend fetches elo-data.json and matches players by name, using BCP career event history to disambiguate same-name candidates - ELO score shown in new sortable Stat-Check ELO column, visible on all screen sizes; unmatched players show — Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
*.xlsx
|
||||
*.csv
|
||||
__pycache__/
|
||||
@@ -0,0 +1 @@
|
||||
,RX-78-2/samle,RX-78-2,28.06.2026 23:38,file:///C:/Users/samle/AppData/Roaming/LibreOffice/4;
|
||||
@@ -0,0 +1,56 @@
|
||||
# Stat Check Elo Scraper
|
||||
|
||||
This is a pure-Python scraper for the public Excel workbook embedded at:
|
||||
|
||||
`https://www.stat-check.com/elo`
|
||||
|
||||
It does not need Selenium, Playwright, Chromium, pandas, or openpyxl. It uses only the Python standard library.
|
||||
|
||||
## How It Works
|
||||
|
||||
The Stat Check page embeds a public OneDrive Excel workbook through a `1drv.ms` URL. Microsoft's web viewer does not expose a stable direct `.xlsx` URL in the page HTML, but it does use a public OneDrive metadata API behind the scenes.
|
||||
|
||||
The scraper reproduces the useful part of that flow:
|
||||
|
||||
1. Request an anonymous Microsoft "Badger" token from:
|
||||
`https://api-badgerp.svc.ms/v1.0/token`
|
||||
2. Convert the public OneDrive embed URL into Microsoft Graph's `shares/u!...` id format.
|
||||
3. Call:
|
||||
`https://my.microsoftpersonalcontent.com/_api/v2.0/shares/{share_id}/driveItem?action=EmbedView`
|
||||
with `Authorization: Badger <token>`.
|
||||
4. Read `@content.downloadUrl` from the metadata response.
|
||||
5. Download the real `.xlsx`.
|
||||
6. Convert the first worksheet to CSV by reading the XLSX ZIP/XML structure directly.
|
||||
|
||||
The Microsoft endpoints used here are undocumented and could change, but this currently works without browser automation.
|
||||
|
||||
## Usage
|
||||
|
||||
```bash
|
||||
python3 pure_python_elo_scrape.py
|
||||
```
|
||||
|
||||
Default outputs:
|
||||
|
||||
- `elo-pure.xlsx`
|
||||
- `elo-pure.csv`
|
||||
|
||||
You can also pass output paths:
|
||||
|
||||
```bash
|
||||
python3 pure_python_elo_scrape.py statcheck-elo.xlsx statcheck-elo.csv
|
||||
```
|
||||
|
||||
## Files
|
||||
|
||||
- `pure_python_elo_scrape.py` - gets the Badger token, resolves the workbook download URL, downloads the XLSX, writes CSV.
|
||||
- `download_and_parse.py` - helper functions for downloading and converting XLSX to CSV with stdlib only.
|
||||
- `sample-elo.csv` - sample output captured during testing.
|
||||
|
||||
## Verification From Testing
|
||||
|
||||
On June 28, 2026, this downloaded a `3,465,320` byte workbook and produced a `4,145,399` byte CSV with `40,945` rows. The CSV header was:
|
||||
|
||||
```csv
|
||||
Rank,Delta,Name,W,L,D,G,WR%,Elo,Delta,Last Event,Country
|
||||
```
|
||||
@@ -0,0 +1,130 @@
|
||||
#!/usr/bin/env python3
|
||||
import csv
|
||||
import html
|
||||
import json
|
||||
import re
|
||||
import sys
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
from urllib.request import Request, urlopen
|
||||
from xml.etree.ElementTree import iterparse
|
||||
|
||||
|
||||
NS = {
|
||||
"main": "http://schemas.openxmlformats.org/spreadsheetml/2006/main",
|
||||
"rel": "http://schemas.openxmlformats.org/officeDocument/2006/relationships",
|
||||
"pkgrel": "http://schemas.openxmlformats.org/package/2006/relationships",
|
||||
}
|
||||
|
||||
|
||||
def metadata_download_url(capture_path: Path) -> str:
|
||||
capture = json.loads(capture_path.read_text())
|
||||
for body in capture["documentBodies"]:
|
||||
payload = body.get("body", {})
|
||||
text = payload.get("body", "") if isinstance(payload, dict) else ""
|
||||
if '"@content.downloadUrl"' in text:
|
||||
return json.loads(text)["@content.downloadUrl"]
|
||||
raise RuntimeError("No @content.downloadUrl found in capture")
|
||||
|
||||
|
||||
def download(url: str, output: Path) -> None:
|
||||
req = Request(url, headers={"User-Agent": "Mozilla/5.0"})
|
||||
with urlopen(req, timeout=60) as resp:
|
||||
output.write_bytes(resp.read())
|
||||
|
||||
|
||||
def shared_strings(zf: zipfile.ZipFile) -> list[str]:
|
||||
if "xl/sharedStrings.xml" not in zf.namelist():
|
||||
return []
|
||||
values = []
|
||||
with zf.open("xl/sharedStrings.xml") as f:
|
||||
for event, elem in iterparse(f, events=("end",)):
|
||||
if elem.tag == f"{{{NS['main']}}}si":
|
||||
parts = []
|
||||
for t in elem.iter(f"{{{NS['main']}}}t"):
|
||||
parts.append(t.text or "")
|
||||
values.append("".join(parts))
|
||||
elem.clear()
|
||||
return values
|
||||
|
||||
|
||||
def first_sheet_path(zf: zipfile.ZipFile) -> str:
|
||||
workbook = zf.read("xl/workbook.xml")
|
||||
first_sheet = re.search(rb'<sheet[^>]+r:id="([^"]+)"', workbook)
|
||||
if not first_sheet:
|
||||
return "xl/worksheets/sheet1.xml"
|
||||
rel_id = first_sheet.group(1).decode()
|
||||
rels = zf.read("xl/_rels/workbook.xml.rels")
|
||||
pattern = rb'<Relationship[^>]+Id="' + re.escape(rel_id.encode()) + rb'"[^>]+Target="([^"]+)"'
|
||||
target = re.search(pattern, rels)
|
||||
if not target:
|
||||
return "xl/worksheets/sheet1.xml"
|
||||
path = html.unescape(target.group(1).decode())
|
||||
if path.startswith("/"):
|
||||
return path.lstrip("/")
|
||||
return "xl/" + path
|
||||
|
||||
|
||||
def column_number(cell_ref: str) -> int:
|
||||
letters = re.match(r"[A-Z]+", cell_ref).group(0)
|
||||
n = 0
|
||||
for ch in letters:
|
||||
n = n * 26 + ord(ch) - ord("A") + 1
|
||||
return n
|
||||
|
||||
|
||||
def xlsx_to_csv(xlsx_path: Path, csv_path: Path) -> None:
|
||||
with zipfile.ZipFile(xlsx_path) as zf, csv_path.open("w", newline="", encoding="utf-8") as out:
|
||||
strings = shared_strings(zf)
|
||||
sheet = first_sheet_path(zf)
|
||||
writer = csv.writer(out)
|
||||
row_values = []
|
||||
current_row = None
|
||||
current_cell = None
|
||||
current_type = None
|
||||
value = None
|
||||
inline_parts = []
|
||||
|
||||
with zf.open(sheet) as f:
|
||||
for event, elem in iterparse(f, events=("start", "end")):
|
||||
tag = elem.tag
|
||||
if event == "start" and tag == f"{{{NS['main']}}}row":
|
||||
current_row = int(elem.attrib.get("r", "0"))
|
||||
row_values = []
|
||||
elif event == "start" and tag == f"{{{NS['main']}}}c":
|
||||
current_cell = elem.attrib.get("r", "")
|
||||
current_type = elem.attrib.get("t")
|
||||
value = None
|
||||
inline_parts = []
|
||||
elif event == "end" and tag == f"{{{NS['main']}}}v":
|
||||
value = elem.text or ""
|
||||
elif event == "end" and tag == f"{{{NS['main']}}}t" and current_type == "inlineStr":
|
||||
inline_parts.append(elem.text or "")
|
||||
elif event == "end" and tag == f"{{{NS['main']}}}c":
|
||||
col = column_number(current_cell)
|
||||
while len(row_values) < col - 1:
|
||||
row_values.append("")
|
||||
if current_type == "s" and value not in (None, ""):
|
||||
row_values.append(strings[int(value)])
|
||||
elif current_type == "inlineStr":
|
||||
row_values.append("".join(inline_parts))
|
||||
else:
|
||||
row_values.append(value or "")
|
||||
elem.clear()
|
||||
elif event == "end" and tag == f"{{{NS['main']}}}row":
|
||||
writer.writerow(row_values)
|
||||
elem.clear()
|
||||
|
||||
|
||||
def main() -> None:
|
||||
capture_path = Path(sys.argv[1] if len(sys.argv) > 1 else "iframe_capture2.json")
|
||||
xlsx_path = Path(sys.argv[2] if len(sys.argv) > 2 else "elo.xlsx")
|
||||
csv_path = Path(sys.argv[3] if len(sys.argv) > 3 else "elo.csv")
|
||||
url = metadata_download_url(capture_path)
|
||||
download(url, xlsx_path)
|
||||
xlsx_to_csv(xlsx_path, csv_path)
|
||||
print(f"downloaded={xlsx_path.stat().st_size} bytes csv={csv_path.stat().st_size} bytes")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,87 @@
|
||||
#!/usr/bin/env python3
|
||||
import base64
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from urllib.parse import quote
|
||||
from urllib.request import Request, urlopen
|
||||
|
||||
from download_and_parse import download, xlsx_to_csv
|
||||
|
||||
|
||||
EMBED_URL = (
|
||||
"https://1drv.ms/x/c/9f33f29504402fa1/"
|
||||
"UQShL0AElfIzIICfIQ4AAAAAAGSZ8S-PJuKWQmM"
|
||||
"?em=2&ActiveCell='Sheet1'!A1&Item='Sheet1'!A1:L40318"
|
||||
"&wdHideGridlines=True&wdHideHeaders=True&wdDownloadButton=True"
|
||||
"&wdInConfigurator=True&wdInConfigurator=True"
|
||||
)
|
||||
|
||||
BADGER_APP_ID = "00000000-0000-0000-0000-0000481710a4"
|
||||
|
||||
|
||||
def request_json(url: str, *, method: str = "GET", data: bytes | None = None, headers: dict[str, str] | None = None) -> dict:
|
||||
req = Request(url, data=data, method=method, headers=headers or {})
|
||||
with urlopen(req, timeout=60) as resp:
|
||||
return json.loads(resp.read().decode("utf-8"))
|
||||
|
||||
|
||||
def badger_token() -> str:
|
||||
payload = json.dumps({"appid": BADGER_APP_ID}, separators=(",", ":")).encode()
|
||||
data = request_json(
|
||||
"https://api-badgerp.svc.ms/v1.0/token",
|
||||
method="POST",
|
||||
data=payload,
|
||||
headers={
|
||||
"Content-Type": "application/json",
|
||||
"Origin": "https://onedrive.live.com",
|
||||
"Referer": "https://onedrive.live.com/",
|
||||
"User-Agent": "Mozilla/5.0",
|
||||
},
|
||||
)
|
||||
if data.get("authScheme") != "badger" or not data.get("token"):
|
||||
raise RuntimeError(f"Unexpected Badger token response: {data!r}")
|
||||
return data["token"]
|
||||
|
||||
|
||||
def share_id(url: str) -> str:
|
||||
encoded = base64.urlsafe_b64encode(url.encode()).decode().rstrip("=")
|
||||
return "u!" + encoded
|
||||
|
||||
|
||||
def download_url_for_embed(embed_url: str) -> str:
|
||||
token = badger_token()
|
||||
select = "id,openWith,officebundle,currentUserRole,eTag,name,size,content.downloadUrl,file,sharepointIds,sensitivityLabel,webUrl,webDavUrl,parentReference,vault"
|
||||
url = (
|
||||
"https://my.microsoftpersonalcontent.com/_api/v2.0/shares/"
|
||||
f"{share_id(embed_url)}/driveItem?action=EmbedView&$select={quote(select, safe=',')}"
|
||||
)
|
||||
data = request_json(
|
||||
url,
|
||||
method="POST",
|
||||
data=b"",
|
||||
headers={
|
||||
"Accept": "application/json",
|
||||
"Authorization": f"Badger {token}",
|
||||
"Origin": "https://onedrive.live.com",
|
||||
"Prefer": "autoredeem",
|
||||
"Referer": "https://onedrive.live.com/",
|
||||
"User-Agent": "Mozilla/5.0",
|
||||
},
|
||||
)
|
||||
if "@content.downloadUrl" not in data:
|
||||
raise RuntimeError(f"No download URL in metadata response: {data!r}")
|
||||
return data["@content.downloadUrl"]
|
||||
|
||||
|
||||
def main() -> None:
|
||||
xlsx_path = Path(sys.argv[1] if len(sys.argv) > 1 else "elo-pure.xlsx")
|
||||
csv_path = Path(sys.argv[2] if len(sys.argv) > 2 else "elo-pure.csv")
|
||||
url = download_url_for_embed(EMBED_URL)
|
||||
download(url, xlsx_path)
|
||||
xlsx_to_csv(xlsx_path, csv_path)
|
||||
print(f"downloaded={xlsx_path.stat().st_size} bytes csv={csv_path.stat().st_size} bytes")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,71 @@
|
||||
#!/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()
|
||||
Reference in New Issue
Block a user