Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add try-except blocks to handle JSON decoding errors in Zenodo scripts. #560

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions scripts/auto-add-from-zenodo-communities.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,13 @@ def main():
response = requests.get('https://zenodo.org/api/records',
params={'communities': community,
'access_token': token})
online_data = response.json()
hits = online_data["hits"]["hits"]
urls = [u["links"]["self_html"] for u in hits]

try:
online_data = response.json()
hits = online_data["hits"]["hits"]
urls = [u["links"]["self_html"] for u in hits]
except requests.exceptions.JSONDecodeError:
print(f"Error decoding JSON for community: {community}")
continue

# compare which new is not in old

Expand Down Expand Up @@ -100,6 +103,5 @@ def main():




if __name__ == "__main__":
main()
38 changes: 5 additions & 33 deletions scripts/generate_link_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,11 @@ def read_zenodo(record):

# Download the file
response = requests.get(url)
data = response.json()
try:
data = response.json()
except json.JSONDecodeError:
data = {}

return data


Expand Down Expand Up @@ -495,35 +499,3 @@ def complete_zenodo_data(zenodo_url):
"""
zenodo_data = read_zenodo(zenodo_url)
entry = {}
urls = [zenodo_url]

if 'doi_url' in zenodo_data.keys():
doi_url = zenodo_data['doi_url']

# Add DOI URL to the URLs list if it's not already there
if doi_url not in urls:
urls.append(doi_url)
entry['url'] = urls

if 'metadata' in zenodo_data.keys():
metadata = zenodo_data['metadata']
# Update entry with Zenodo metadata and statistics
entry['name'] = metadata['title']
if 'publication_date' in metadata.keys():
entry['publication_date'] = metadata['publication_date']
if 'description' in metadata.keys():
entry['description'] = remove_html_tags(metadata['description'])
if 'creators' in metadata.keys():
creators = metadata['creators']
entry['authors'] = [c['name'] for c in creators]
if 'license' in metadata.keys():
entry['license'] = metadata['license']['id']

if 'stats' in zenodo_data.keys():
entry['num_downloads'] = zenodo_data['stats']['downloads']

return entry


if __name__ == "__main__":
main()