amber / tools /queue_named.py
amongglue's picture
Corpus index: 142 datasets, 5,876 h
343a59d verified
Raw History Blame Contribute Delete
1.97 kB
#!/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())