File size: 2,517 Bytes
04782c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""Page the public Hugging Face listing of datasets tagged LeRobot. Resumable.

usage: list.py OUT_DIR [MAX_PAGES]
Creates OUT_DIR if missing. Appends one JSON line per dataset (id, author, created, tags) to OUT_DIR/datasets.jsonl and
replaces OUT_DIR/cursor.txt with the next page URL after each page ("END" when done). A rerun with the same OUT_DIR
resumes from cursor.txt; use a new OUT_DIR for a fresh listing. Each page is at most 64 MiB; the next-page link must
stay on https://huggingface.co/api/datasets."""
import json, os, re, sys, time, urllib.parse
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import hub

START = "https://huggingface.co/api/datasets?filter=LeRobot&limit=1000&full=false"
PAGE_LIMIT = 64 << 20


def listing_url(url):
    u = urllib.parse.urlsplit(url)
    return hub.allowed(url) and u.hostname == "huggingface.co" and u.path == "/api/datasets"


def main():
    if len(sys.argv) < 2:
        sys.exit(__doc__)
    d = sys.argv[1]
    max_pages = int(sys.argv[2]) if len(sys.argv) > 2 else None
    os.makedirs(d, exist_ok=True)
    out, cur = os.path.join(d, "datasets.jsonl"), os.path.join(d, "cursor.txt")
    url = open(cur).read().strip() if os.path.exists(cur) else START
    n = pages = 0
    while url != "END" and (max_pages is None or pages < max_pages):
        if not listing_url(url):
            sys.exit("refused listing URL " + url)
        for attempt in range(5):
            try:
                body, headers = hub.get(url, PAGE_LIMIT, timeout=60)
                break
            except (hub.OffOrigin, hub.TooLarge) as e:
                sys.exit("refused: " + repr(e)[:200])
            except Exception as e:
                print("retry", attempt, repr(e)[:120], flush=True)
                time.sleep(10 * (attempt + 1))
        else:
            sys.exit("GIVEUP " + url)
        rows = json.loads(body)
        with open(out, "a") as f:
            for r in rows:
                f.write(json.dumps({"id": r.get("id"), "author": r.get("author"), "created": r.get("createdAt"), "tags": r.get("tags")}) + "\n")
        n += len(rows); pages += 1
        m = re.search(r'<([^>]+)>;\s*rel="next"', headers.get("Link") or "")
        url = m.group(1) if m else "END"
        if url != "END" and not listing_url(url):
            sys.exit("refused next-page link " + url)
        with open(cur, "w") as f:
            f.write(url)
    print("DONE" if url == "END" else "PAUSED", n, "rows", pages, "pages")


if __name__ == "__main__":
    main()