CoolFace
Apppublic

LZ-SG/embedded-dev-tutorials

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
push_v151_spa.py39 linesDownload Raw Back to root
1#!/usr/bin/env python32"""Push v1.5.1: index.html + app.py"""3import os, base64, json, urllib.request, urllib.error4 5TOKEN = "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"6REPO = "SG06231219-boop/embedded-dev-tutorials"7BRANCH = "main"8API_BASE = f"https://api.github.com/repos/{REPO}/contents"9BASE = os.path.dirname(os.path.abspath(__file__))10 11FILES = ["app.py", "static/index.html"]12 13for fpath in FILES:14    local = os.path.join(BASE, fpath.replace("/", os.sep))15    with open(local, "rb") as f:16        content_b64 = base64.b64encode(f.read()).decode()17    18    try:19        req = urllib.request.Request(f"{API_BASE}/{fpath}?ref={BRANCH}",20            headers={"Authorization": f"token {TOKEN}", "Accept": "application/vnd.github.v3+json", "User-Agent": "push"})21        with urllib.request.urlopen(req, timeout=20) as resp:22            sha = json.loads(resp.read()).get("sha")23    except: sha = None24    25    payload = json.dumps({26        "message": f"feat: v1.5.1 - SPA bookmark+progress+sort ({os.path.basename(fpath)})",27        "content": content_b64, "branch": BRANCH28    } | ({"sha": sha} if sha else {}))29    30    req2 = urllib.request.Request(f"{API_BASE}/{fpath}",31        data=payload.encode(),32        headers={"Authorization": f"token {TOKEN}", "Content-Type": "application/json", "Accept": "application/vnd.github.v3+json", "User-Agent": "push"})33    try:34        with urllib.request.urlopen(req2, timeout=30) as resp:35            result = json.loads(resp.read())36            print(f"OK {fpath} ({result.get('commit',{}).get('sha','?')[:7]})")37    except urllib.error.HTTPError as e:38        print(f"FAIL {fpath}: HTTP {e.code} {e.read().decode()[:200]}")39