update_citation.py 1.1 KB

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