File size: 1,971 Bytes
343a59d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Queue specific repository records by id.

The crawlers find records by searching text, which misses the well-known
datasets whose pages do not read like search results -- the ones already
listed by hand in work/discovery/manual_candidates.jsonl. This puts a named
record into the same queue the ingest lanes already consume, so no separate
code path is needed to fetch it.

    python3 tools/queue_named.py osf q3zws 3jk45 thsqg ktv7m
"""
from __future__ import annotations

import json
import sys
import urllib.request
from pathlib import Path

ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT))

from tools.enrich_repos import figshare_files, osf_files, run  # noqa: E402

FETCH = {"osf": osf_files, "figshare": figshare_files}


def osf_meta(nid: str) -> tuple[str, str]:
    req = urllib.request.Request(f"https://api.osf.io/v2/nodes/{nid}/",
                                 headers={"User-Agent": "eegx-discovery/1.0"})
    with urllib.request.urlopen(req, timeout=60) as r:
        at = json.load(r).get("data", {}).get("attributes", {})
    return at.get("title", "") or nid, f"https://osf.io/{nid}/"


def main() -> int:
    if len(sys.argv) < 3:
        print(__doc__)
        return 2
    source, ids = sys.argv[1], sys.argv[2:]
    if source not in FETCH:
        print(f"unknown source {source}; expected one of {sorted(FETCH)}")
        return 2

    rows = []
    for rid in ids:
        title, url = (osf_meta(rid) if source == "osf" else (rid, ""))
        print(f"  {rid}: {title[:70]}")
        # Named by hand precisely because they are active-task datasets; the
        # tier is the reason they are on the list at all.
        rows.append(dict(source=source, id=rid, doi=None, title=title,
                         url=url, license=None, tier=1,
                         tier_label="active-task"))
    run(source, rows, FETCH[source])
    return 0


if __name__ == "__main__":
    sys.exit(main())