"""Helper used while compiling the hand-curated tables: fetch abstracts from Europe PMC. Not part of run_all.py (the curated tables carry the verified values); kept for transparency. Usage: python3 lit_lookup.py 'EPMC QUERY' ['EPMC QUERY' ...] e.g. 'TITLE:"The oldest gnathostome teeth"' """ import sys, json, re, time, urllib.request, urllib.parse def epmc(query, n=1, maxlen=1300): url = ("https://www.ebi.ac.uk/europepmc/webservices/rest/search?" + urllib.parse.urlencode(dict(query=query, format="json", resultType="core", pageSize=n))) d = None for attempt in range(3): try: with urllib.request.urlopen(url, timeout=25) as r: d = json.loads(r.read().decode()) break except Exception: # noqa: BLE001 time.sleep(3) if d is None: return "## FAILED: " + query + "\n" out = [] for r in d.get("resultList", {}).get("result", []): j = r.get("journalInfo", {}) out.append("## %s | %s | %s %s %s | %s | doi:%s" % ( (r.get("authorString") or "")[:80], r.get("pubYear"), j.get("journal", {}).get("title"), j.get("volume"), r.get("pageInfo"), r.get("title"), r.get("doi"))) out.append(re.sub("<[^>]+>", "", r.get("abstractText") or "")[:maxlen]) out.append("") return "\n".join(out) if out else "## NO HIT: " + query + "\n" if __name__ == "__main__": for q in sys.argv[1:]: print(epmc(q), flush=True); time.sleep(1.0)