Claude Opus 5
Deploy: BEAR dyad mode, the design pool, screen timing, and the ID-code removal
bf22a94 Download download_data.py from princessamygdala/TreasureHuntForTwo: direct link, hf CLI and curl.
- Browser
- Download file 5.63 kB
-
https://huggingface.co/spaces/princessamygdala/TreasureHuntForTwo/resolve/main/download_data.py
- Command line
-
hf download hf://spaces/princessamygdala/TreasureHuntForTwo/download_data.py
-
curl -L -o download_data.py https://huggingface.co/spaces/princessamygdala/TreasureHuntForTwo/resolve/main/download_data.py
5.63 kB
| """Download all participant data from the GitLab repo to ./downloaded_data/. | |
| Pavlovia's GitLab blocks the in-browser download button, so use this instead. | |
| Reads GITLAB_* from .env. Run any time to pull the latest data: | |
| uv run python download_data.py | |
| A pull is INCREMENTAL and it is LOUD when it fails. Both of those were learned | |
| the same morning. The repo holds hundreds of participant files and this script | |
| used to ask for every one of them on every run with no pause, which GitLab | |
| answers with 429 once a few hundred requests land in a few seconds. The old code tested | |
| `status_code < 400` and silently skipped anything else, so a throttled pull | |
| wrote a third of the files, printed "Downloaded 121 files" and exited 0. The | |
| mirror accumulates across runs, so the shortfall never showed up as a missing | |
| directory: it showed up as a session that had plainly been played being absent | |
| from the analysis, five days later. Two sessions from 2026-10-28 and 2026-10-30 | |
| were missing when this was found. | |
| """ | |
| import json | |
| import os | |
| import sys | |
| import time | |
| from urllib.parse import quote | |
| import requests | |
| from dotenv import load_dotenv | |
| from gitlab_save import GITLAB_URL_DEFAULT | |
| load_dotenv() | |
| BASE = os.getenv("GITLAB_URL", GITLAB_URL_DEFAULT).strip().rstrip("/") | |
| TOKEN = os.getenv("GITLAB_TOKEN", "").strip() | |
| PROJECT = os.getenv("GITLAB_PROJECT", "").strip() | |
| BRANCH = (os.getenv("GITLAB_BRANCH", "") or "main").strip() | |
| H = {"PRIVATE-TOKEN": TOKEN} | |
| PID = quote(PROJECT, safe="") | |
| OUT = "downloaded_data" | |
| #: Blob id of every file already on disk, so an unchanged file is not asked | |
| #: for twice. Participant files are written once and never touched again, so | |
| #: after the first pull a run costs the tree listing and nothing else. This is | |
| #: what keeps the request count under GitLab's rate limit. | |
| MANIFEST = os.path.join(OUT, ".manifest.json") | |
| #: Repo subtrees that hold participant data. The hunt writes everything under | |
| #: data/<pid>/; "pilot" is listed for the harvest layout and 404s here. | |
| #: "bear" is the dyad study, which shares this project with the SONA/lab sample and is kept | |
| #: apart by folder (see web_app's _export_and_push_gitlab). | |
| DATA_PATHS = ("data", "pilot", "bear") | |
| #: A 429 carries Retry-After. Without it, back off and try again anyway. | |
| RETRIES = 6 | |
| BACKOFF = 4 | |
| def _get(url, **params): | |
| """GET with backoff on throttling. Returns the response, or None on 404.""" | |
| for attempt in range(RETRIES): | |
| r = requests.get(url, params=params, headers=H, timeout=30) | |
| if r.status_code == 404: | |
| return None | |
| if r.status_code == 429 or r.status_code >= 500: | |
| wait = int(r.headers.get("Retry-After") or BACKOFF * (attempt + 1)) | |
| print(f" {r.status_code} from GitLab, waiting {wait}s", flush=True) | |
| time.sleep(wait) | |
| continue | |
| r.raise_for_status() | |
| return r | |
| raise RuntimeError(f"gave up after {RETRIES} attempts: {url}") | |
| def list_data_files(paths=DATA_PATHS): | |
| """[(path, blob_id)] for every participant file in the repo.""" | |
| files = [] | |
| for p in paths: | |
| files += _list_tree(p) | |
| return files | |
| def _list_tree(path): | |
| files, page = [], 1 | |
| while True: | |
| r = _get(f"{BASE}/api/v4/projects/{PID}/repository/tree", | |
| path=path, recursive="true", per_page=100, page=page) | |
| # A subtree that does not exist in this repo is a 404, which is the | |
| # "data" path in the harvest and the "pilot" path in the hunt. | |
| if r is None: | |
| break | |
| body = r.json() | |
| if not body: | |
| break | |
| files += [(x["path"], x["id"]) for x in body if x["type"] == "blob"] | |
| nxt = r.headers.get("x-next-page") | |
| if not nxt: | |
| break | |
| page = int(nxt) | |
| return files | |
| def _load_manifest(): | |
| try: | |
| with open(MANIFEST, encoding="utf-8") as f: | |
| return json.load(f) | |
| except (OSError, ValueError): | |
| return {} | |
| def main(): | |
| files = list_data_files() | |
| os.makedirs(OUT, exist_ok=True) | |
| seen = _load_manifest() | |
| fresh, got, failed = 0, 0, [] | |
| for path, blob in files: | |
| dest = os.path.join(OUT, path) | |
| if seen.get(path) == blob and os.path.exists(dest): | |
| fresh += 1 | |
| continue | |
| try: | |
| raw = _get(f"{BASE}/api/v4/projects/{PID}/repository/files/" | |
| f"{quote(path, safe='')}/raw", ref=BRANCH) | |
| except (RuntimeError, requests.RequestException) as e: | |
| failed.append((path, str(e))) | |
| continue | |
| if raw is None: | |
| failed.append((path, "404 on a path the tree had just listed")) | |
| continue | |
| os.makedirs(os.path.dirname(dest), exist_ok=True) | |
| with open(dest, "wb") as fh: | |
| fh.write(raw.content) | |
| seen[path] = blob | |
| got += 1 | |
| with open(MANIFEST, "w", encoding="utf-8") as f: | |
| json.dump(seen, f, indent=0, sort_keys=True) | |
| print(f"Downloaded {got} new or changed files to ./{OUT}/ " | |
| f"({fresh} already current, {len(files)} in the repo)") | |
| if failed: | |
| # Loudly, and non-zero, because the caller builds an analysis out of | |
| # whatever is on disk and cannot tell a short mirror from a short study. | |
| print(f"\nFAILED to fetch {len(failed)} of {len(files)} files. " | |
| f"The mirror is INCOMPLETE and any analysis built on it is too.") | |
| for path, why in failed[:20]: | |
| print(f" {path}: {why}") | |
| if len(failed) > 20: | |
| print(f" ... and {len(failed) - 20} more") | |
| return 1 | |
| return 0 | |
| if __name__ == "__main__": | |
| sys.exit(main()) | |