Skip to content

Instantly share code, notes, and snippets.

@kathrin77
Forked from lnielsen/stats.py
Last active May 3, 2024 08:55
Show Gist options
  • Save kathrin77/5c2f0dfacc6698bf84ac148c36d06ea8 to your computer and use it in GitHub Desktop.
Save kathrin77/5c2f0dfacc6698bf84ac148c36d06ea8 to your computer and use it in GitHub Desktop.
###Get zenodo community statistics
from urllib.request import Request, urlopen
from urllib.parse import quote_plus
import json
import csv
query = "communities:lory"
filename = "output.csv"
query = quote_plus(query)
link = f'https://zenodo.org/api/records?all_versions=true&size=200&q={query}'
def request(link, writer):
with urlopen(Request(link)) as r:
data = json.loads(r.read())
next_link = data['links'].get('next', None)
for h in data['hits']['hits']:
self_link = h['links']['self']
doi = h['metadata']['doi']
title = h['metadata']['title']
date = h['metadata']['publication_date']
views = h['stats']['views']
downloads = h['stats']['downloads']
writer.writerow([self_link, doi, title, date, views, downloads])
return next_link
with open(filename, mode='w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
writer.writerow(["URL","DOI","Title","PublicationDate","Views","Downloads"])
print(f"Requesting {link}")
link = request(link, writer)
while link:
print(f"Requesting {link}")
link = request(link, writer)
@kathrin77
Copy link
Author

Zenodo community stats

Get zenodo community statistics with this script:

  1. Edit query (line 6) into your community name, e.g. "communities:mycommunity"
  2. run script:

python stats.py

If you're opening the CSV file "output.csv" in Excel and encountering issues with character encoding, there are a few steps you can take within Excel to try to correct the problem:

Import Data Wizard:
Instead of directly opening the CSV file, try using Excel's "Import Data" wizard. This wizard allows you to specify the file origin and encoding before importing the data into Excel.

  1. In Excel, go to the "Data" tab.
  2. Click on "Get Data" or "From Text/CSV" depending on your Excel version.
  3. Choose your CSV file and follow the steps in the wizard.
  4. In one of the steps, you'll have the option to specify the file origin and encoding. Choose the appropriate encoding for your file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment