update_citation.py 1.1 KB

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