Skip to content

Commit 06a872a

Browse files
Merge pull request #907 from NFDI4BIOIMAGE/git-bob-mod-bSXx0xjUNQ
Add script to fetch Zenodo authors and ORCIDs and update YAML.
2 parents 33f4507 + d61649d commit 06a872a

2 files changed

Lines changed: 125 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Run Weekly Auto-Add ORCIDs
2+
3+
on:
4+
schedule:
5+
- cron: '0 11 * * 0' # This runs the job every Sunday at 11 AM
6+
7+
jobs:
8+
run-weekly-auto-add-orcids:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- name: Checkout code
13+
uses: actions/checkout@v2
14+
15+
- name: Set up Python
16+
uses: actions/setup-python@v4
17+
with:
18+
python-version: '3.x'
19+
20+
- name: Install dependencies
21+
run: |
22+
python -m pip install --upgrade pip
23+
pip install -r requirements.txt
24+
25+
- name: Run Python
26+
env:
27+
GITHUB_API_KEY: "${{ secrets.GITHUB_TOKEN }}"
28+
GITHUB_RUN_ID: "${{ github.run_id }}"
29+
run: |
30+
python scripts/add_author_orcids.py nfdi4bioimage/training

scripts/add_author_orcids.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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

Comments
 (0)