"""Search Europe PMC for open-access stability studies. Saves raw JSON to _raw/europepmc/search/.""" import json, sys, hashlib, urllib.parse, urllib.request, pathlib RAW = pathlib.Path(__file__).resolve().parents[2] / "_raw" / "europepmc" / "search" RAW.mkdir(parents=True, exist_ok=True) BASE = "https://www.ebi.ac.uk/europepmc/webservices/rest/search" def search(q, size=100): url = BASE + "?" + urllib.parse.urlencode({"query": q, "format": "json", "pageSize": size, "resultType": "core"}) fn = RAW / (hashlib.sha1(q.encode()).hexdigest()[:12] + ".json") if not fn.exists(): with urllib.request.urlopen(url, timeout=60) as r: fn.write_bytes(r.read()) return json.loads(fn.read_text()) if __name__ == "__main__": for q in sys.argv[1:]: d = search(q) print(f"## {q} hits={d.get('hitCount')}") for r in d.get("resultList", {}).get("result", []): print(f"{r.get('pmcid','-')}\t{r.get('pubYear')}\t{r.get('journalInfo',{}).get('journal',{}).get('medlineAbbreviation','')}\t{r.get('license','')}\t{r.get('doi','')}\t{r.get('title','')[:150]}")