helloworld / topic1 /code /harvest_ns.py
eduzrh's picture
Upload topic1/code/harvest_ns.py with huggingface_hub
24aa8e0 verified
Raw History Blame Contribute Delete
2.97 kB
"""Nature 系列子刊检索:按期刊 ISSN + 主题词查 OpenAlex,汇总候选清单。
用法:python3 harvest_ns.py
输出:topic1/ns_harvest_v2.json
"""
import json
import os
import time
import urllib.parse
import urllib.request
BASE = os.path.dirname(os.path.abspath(__file__))
UA = {"User-Agent": "cdhe-inventory/1.0 (mailto:eduzrh@example.org)"}
JOURNALS = {
"Scientific Data": "2052-4463",
"Nature Sustainability": "2398-9629",
"Nature Human Behaviour": "2397-3374",
"Nature Water": "2731-6084",
"Nature Reviews Earth & Environment": "2662-138X",
"Nature Communications": "2041-1723",
"Nature": "0028-0836",
}
TERMS = [
"drought", "heatwave", "heat wave", "compound event", "climate extreme",
"extreme precipitation", "flood", "wildfire", "fire", "vegetation",
"soil moisture", "streamflow", "disaster", "food production", "carbon",
"climate risk", "exposure vulnerability", "reanalysis",
]
def get(url):
req = urllib.request.Request(url, headers=UA)
with urllib.request.urlopen(req, timeout=60) as r:
return json.loads(r.read().decode())
def main():
out = {}
for jname, issn in JOURNALS.items():
for term in TERMS:
q = urllib.parse.urlencode({
"filter": f"primary_location.source.issn:{issn},"
f"from_publication_date:2014-01-01,title_and_abstract.search:{term}",
"per-page": "25",
"sort": "cited_by_count:desc",
"select": "id,doi,title,publication_year,cited_by_count,type,"
"primary_location,best_oa_location",
})
url = f"https://api.openalex.org/works?{q}"
try:
d = get(url)
except Exception as e: # noqa: BLE001
print(f"[warn] {jname} / {term}: {e}")
time.sleep(2)
continue
for w in d.get("results", []):
doi = (w.get("doi") or "").replace("https://doi.org/", "")
if not doi or not w.get("title"):
continue
key = doi.lower()
rec = out.get(key, {})
rec.update({
"doi": doi, "title": w["title"], "journal": jname,
"year": w.get("publication_year"),
"cited_by": w.get("cited_by_count"),
"type": w.get("type"),
})
rec.setdefault("terms", [])
if term not in rec["terms"]:
rec["terms"].append(term)
out[key] = rec
time.sleep(0.15)
print(f"{jname}: 累计 {len(out)}")
recs = sorted(out.values(), key=lambda r: (-(r.get("cited_by") or 0)))
json.dump(recs, open(os.path.join(BASE, "ns_harvest_v2.json"), "w"),
ensure_ascii=False, indent=1)
print("total", len(recs))
if __name__ == "__main__":
main()