|
| 1 | +""" |
| 2 | +Script to build the authors.rst file |
| 3 | +""" |
| 4 | + |
| 5 | +import json |
| 6 | +import requests |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +def full_name(au): |
| 10 | + return f"{au['givenName']} {au['familyName']}" |
| 11 | + |
| 12 | +with open("authors.json") as fp: |
| 13 | + author_data = json.load(fp) |
| 14 | +author_list = ", ".join(f"{full_name(au)}" for au in author_data) |
| 15 | + |
| 16 | +with open("authors.rst.tpl") as fp: |
| 17 | + template = fp.read() |
| 18 | + |
| 19 | +affiliation_ids = [] |
| 20 | +for person in author_data: |
| 21 | + for affil in person["affiliation"]: |
| 22 | + if affil["@id"] not in affiliation_ids: |
| 23 | + affiliation_ids.append(affil["@id"]) |
| 24 | + |
| 25 | +cache_path = ".rorid_cache" |
| 26 | + |
| 27 | +if not Path(cache_path).exists(): |
| 28 | + affiliation_data = {} |
| 29 | + for affil_id in affiliation_ids: |
| 30 | + response = requests.get(f"https://api.ror.org/v2/organizations/{affil_id.split('/')[-1]}") |
| 31 | + if response.status_code == 200: |
| 32 | + affiliation_data[affil_id] = response.json() |
| 33 | + else: |
| 34 | + print(response) |
| 35 | + with open(cache_path, "w") as fp: |
| 36 | + json.dump(affiliation_data, fp, indent=2) |
| 37 | +else: |
| 38 | + with open(cache_path) as fp: |
| 39 | + affiliation_data = json.load(fp) |
| 40 | + |
| 41 | + |
| 42 | +def get_affiliation_text(org): |
| 43 | + name = [entry["value"] for entry in org["names"] if entry["lang"] == "en" and "label" in entry["types"]][0] |
| 44 | + location = org["locations"][0]["geonames_details"] |
| 45 | + city = location["name"] |
| 46 | + country = location["country_name"] |
| 47 | + parents = [rel["label"] for rel in org["relationships"] if rel["type"] == "parent"] |
| 48 | + if parents: |
| 49 | + return f"{name}, {', '.join(parents)}, {city}, {country}" |
| 50 | + else: |
| 51 | + return f"{name}, {city}, {country}" |
| 52 | + |
| 53 | +def affiliation_number(au, affiliations): |
| 54 | + index = list(affiliations.keys()).index(au["affiliation"][0]["@id"]) # to do: handle people with multiple affiliations |
| 55 | + return index + 1 |
| 56 | + |
| 57 | + |
| 58 | +affiliations_text = "\n".join( |
| 59 | + f"{i}. {get_affiliation_text(affiliation_data[org_id])}" for i, org_id in enumerate(affiliation_data, start=1) |
| 60 | +) |
| 61 | + |
| 62 | +authors_text = "\n".join( |
| 63 | + [f"- {full_name(au)} [{affiliation_number(au, affiliation_data)}]" for au in author_data] |
| 64 | +) |
| 65 | + |
| 66 | +with open("authors.rst", "w") as fp: |
| 67 | + fp.write(template.format(authors=authors_text, affiliations=affiliations_text)) |
0 commit comments