update_citation.py 1013 B

12345678910111213141516171819202122232425262728293031323334
  1. import os
  2. import sys
  3. import requests
  4. import yaml
  5. def get_infos() -> str:
  6. headers = {
  7. 'Accept': 'application/json',
  8. }
  9. params = {
  10. 'access_token': os.environ['ZENODO_TOKEN'],
  11. 'q': 'nicegui',
  12. 'sort': 'mostrecent',
  13. 'status': 'published',
  14. }
  15. try:
  16. response = requests.get('https://zenodo.org/api/records', params=params, headers=headers)
  17. response.raise_for_status()
  18. # Hide all error details to avoid leaking the token
  19. except Exception:
  20. print('Error while getting the Zenodo infos')
  21. sys.exit(1)
  22. data = response.json()[0]['metadata']
  23. return data['doi'], data['version'], data['publication_date']
  24. if __name__ == '__main__':
  25. with open('CITATION.cff', 'r') as file:
  26. citation = yaml.safe_load(file)
  27. citation['doi'], citation['version'], citation['date-released'] = get_infos()
  28. with open('CITATION.cff', 'w') as file:
  29. yaml.dump(citation, file, sort_keys=False, default_flow_style=False)