|
| 1 | +import pathlib |
| 2 | +import textwrap |
| 3 | +import time |
| 4 | +import warnings |
| 5 | + |
| 6 | +import requests |
| 7 | + |
| 8 | +import pedpy |
| 9 | + |
| 10 | +zenodo_path = pathlib.Path("docs/source/ZENODO.rst") |
| 11 | + |
| 12 | +search_query = "PedPy" |
| 13 | +version = f"v{pedpy.__version__}" |
| 14 | +record_id = None |
| 15 | +zenodo_record = "If you use *PedPy* in your work, please cite it with the following information from Zenodo.\n\n" |
| 16 | + |
| 17 | + |
| 18 | +def fetch_data_with_retries( |
| 19 | + url, params=None, headers=None, max_retries=10, wait_time=2 |
| 20 | +): |
| 21 | + for attempt in range(max_retries): |
| 22 | + try: |
| 23 | + response = requests.get(url, params=params, headers=headers) |
| 24 | + response.raise_for_status() |
| 25 | + return response |
| 26 | + except requests.RequestException as e: |
| 27 | + warnings.warn(f"Attempt {attempt + 1} failed: {e}") |
| 28 | + time.sleep(wait_time) |
| 29 | + raise RuntimeError("All attempts to fetch data failed.") |
| 30 | + |
| 31 | + |
| 32 | +try: |
| 33 | + response = fetch_data_with_retries( |
| 34 | + "https://zenodo.org/api/records", |
| 35 | + params={"q": search_query, "all_versions": True, "sort": "mostrecent"}, |
| 36 | + ) |
| 37 | + data = response.json() |
| 38 | + |
| 39 | + # Check for errors |
| 40 | + if "status" in data and data["status"] != 200: |
| 41 | + raise RuntimeError("Not found") |
| 42 | + else: |
| 43 | + # Print available records |
| 44 | + for record in data.get("hits", {}).get("hits", []): |
| 45 | + if ( |
| 46 | + "version" in record["metadata"] |
| 47 | + and version == record["metadata"]["version"] |
| 48 | + ): |
| 49 | + record_id = record["id"] |
| 50 | + |
| 51 | + headers = {"accept": "application/x-bibtex"} |
| 52 | + response = fetch_data_with_retries( |
| 53 | + f"https://zenodo.org/api/records/{record_id}", headers=headers |
| 54 | + ) |
| 55 | + response.encoding = "utf-8" |
| 56 | + |
| 57 | + if response.status_code == 200: |
| 58 | + zenodo_record += ( |
| 59 | + ".. code-block:: bibtex\n\n" |
| 60 | + + textwrap.indent(response.text, " " * 4) |
| 61 | + + "\n" |
| 62 | + ) |
| 63 | + else: |
| 64 | + raise RuntimeError("Not found") |
| 65 | + |
| 66 | +except Exception as e: |
| 67 | + warnings.warn(f"An error occurred: {e}") |
| 68 | + |
| 69 | +zenodo_record += textwrap.dedent( |
| 70 | + """\ |
| 71 | + |
| 72 | + Information to all versions of PedPy can be found on `Zenodo <https://zenodo.org/doi/10.5281/zenodo.7194992>`_. |
| 73 | + |
| 74 | + .. image:: https://zenodo.org/badge/DOI/10.5281/zenodo.7194992.svg |
| 75 | + :target: https://doi.org/10.5281/zenodo.7194992 |
| 76 | + |
| 77 | + To find your installed version of *PedPy*, you can run: |
| 78 | + |
| 79 | + .. code-block:: bash |
| 80 | + |
| 81 | + import pedpy |
| 82 | + print(pedpy.__version__) |
| 83 | + """ |
| 84 | +) |
| 85 | + |
| 86 | +with open(zenodo_path, "w") as f: |
| 87 | + f.write(zenodo_record) |
| 88 | + |
| 89 | +print(zenodo_record) |
0 commit comments