|
| 1 | +import yaml |
| 2 | +from _github_utilities import get_github_repository, create_branch, write_file, send_pull_request |
| 3 | +import requests |
| 4 | + |
| 5 | +def fetch_authors_from_zenodo(record_url): |
| 6 | + """Fetch author names and ORCIDs from Zenodo REST API. |
| 7 | +
|
| 8 | + Parameters |
| 9 | + ---------- |
| 10 | + record_url : str |
| 11 | + A Zenodo record URL. |
| 12 | +
|
| 13 | + Returns |
| 14 | + ------- |
| 15 | + list of str |
| 16 | + Authors with or without ORCIDs. |
| 17 | + """ |
| 18 | + zenodo_api_url = "https://zenodo.org/api/records/" + record_url.split("/")[-1].split(".")[-1] |
| 19 | + response = requests.get(zenodo_api_url) |
| 20 | + response.raise_for_status() |
| 21 | + data = response.json() |
| 22 | + |
| 23 | + authors = data.get("metadata", {}).get("creators", []) |
| 24 | + |
| 25 | + author_with_orcid = [] |
| 26 | + for author in authors: |
| 27 | + name = author.get("name", "") |
| 28 | + if "," in name: |
| 29 | + name = name.split(",")[1].strip() + " " + name.split(",")[0].strip() |
| 30 | + |
| 31 | + if "orcid" in author: |
| 32 | + author_with_orcid.append(f"{name} https://orcid.org/{author['orcid']}") |
| 33 | + else: |
| 34 | + author_with_orcid.append(name) |
| 35 | + return author_with_orcid |
| 36 | + |
| 37 | +def update_authors_with_orcid(content, repository, file_path): |
| 38 | + """Update YAML content by adding author_with_orcid field. |
| 39 | +
|
| 40 | + Parameters |
| 41 | + ---------- |
| 42 | + content : dict |
| 43 | + Loaded YAML content. |
| 44 | + repository : str |
| 45 | + GitHub repository string (e.g., "org/repo"). |
| 46 | + file_path : str |
| 47 | + Path to the YAML file in the repository. |
| 48 | + """ |
| 49 | + modified = False |
| 50 | + do_exit = False |
| 51 | + for resource in content.get("resources", []): |
| 52 | + if "author_with_orcid" not in resource: |
| 53 | + urls = resource.get("url", []) |
| 54 | + if isinstance(urls, str): |
| 55 | + urls = [urls] |
| 56 | + for url in urls: |
| 57 | + if url.startswith("https://zenodo.org/record") or url.startswith("https://zenodo.org/doi"): |
| 58 | + try: |
| 59 | + resource["author_with_orcid"] = fetch_authors_from_zenodo(url) |
| 60 | + modified = True |
| 61 | + break |
| 62 | + except Exception as e: |
| 63 | + print(f"Error fetching authors for URL {url}: {e}") |
| 64 | + if "TOO MANY REQUESTS" in str(e): |
| 65 | + print("Too many requests, skipping this URL") |
| 66 | + do_exit = True |
| 67 | + break |
| 68 | + if do_exit: |
| 69 | + break |
| 70 | + if modified: |
| 71 | + main_branch = "main" |
| 72 | + new_branch = create_branch(repository, parent_branch=main_branch) |
| 73 | + new_content = yaml.dump(content, sort_keys=False, allow_unicode=True) |
| 74 | + write_file(repository, new_branch, file_path, new_content, "Add authors with ORCIDs to resources") |
| 75 | + pr_url = send_pull_request( |
| 76 | + repository, |
| 77 | + new_branch, |
| 78 | + "Add authors with ORCIDs", |
| 79 | + f"This PR adds authors with ORCIDs to resources YAML file." |
| 80 | + ) |
| 81 | + print(pr_url) |
| 82 | + |
| 83 | +if __name__ == "__main__": |
| 84 | + import sys |
| 85 | + repository = sys.argv[1] |
| 86 | + file_path = "resources/nfdi4bioimage.yml" |
| 87 | + |
| 88 | + # Authenticate and fetch file content |
| 89 | + repo = get_github_repository(repository) |
| 90 | + main_branch = "main" |
| 91 | + file_content = repo.get_contents(file_path, ref=main_branch) |
| 92 | + content = yaml.safe_load(file_content.decoded_content.decode()) |
| 93 | + |
| 94 | + # Update YAML content |
| 95 | + update_authors_with_orcid(content, repository, file_path) |
0 commit comments