Skip to content

Commit 07ff777

Browse files
authored
Use automatic generated bibtex key as additional citation information (#353)
1 parent f309230 commit 07ff777

5 files changed

Lines changed: 94 additions & 24 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,4 +316,5 @@ cython_debug/
316316

317317
docs/build
318318
pedpy/_version.py
319+
docs/source/ZENODO.rst
319320
# End of https://www.toptal.com/developers/gitignore/api/python,visualstudiocode,pycharm,jupyternotebooks

.readthedocs.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ build:
1414
jobs:
1515
pre_build:
1616
- pip install -e .
17-
17+
- sleep 60 # sleep for Zenodo upload to finsih
18+
- python scripts/generate_bibtex.py
1819

1920
# Build documentation in the docs/ directory with Sphinx
2021
sphinx:

docs/source/citation.rst

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,6 @@
22
Cite PedPy
33
**********
44

5-
If you use *PedPy* in your work, please cite it using the following information from zenodo.
6-
Here you can find all versions of PedPy with this link:
5+
.. include:: ZENODO.rst
76

8-
.. image:: https://zenodo.org/badge/DOI/10.5281/zenodo.7194992.svg
9-
:target: https://doi.org/10.5281/zenodo.7194992
10-
11-
You can find out the installed version of *PedPy* with:
12-
13-
.. code-block:: bash
14-
15-
import pedpy
16-
print(pedpy.__version__)
177

docs/source/index.rst

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,7 @@ Install PedPy
3939
Cite PedPy
4040
==========
4141

42-
If you use *PedPy* in your work, please cite it using the following information from zenodo.
43-
Here you can find all versions of PedPy with this link:
44-
45-
.. image:: https://zenodo.org/badge/DOI/10.5281/zenodo.7194992.svg
46-
:target: https://doi.org/10.5281/zenodo.7194992
47-
48-
You can find out the installed version of *PedPy* with:
49-
50-
.. code-block:: bash
51-
52-
import pedpy
53-
print(pedpy.__version__)
42+
.. include:: ZENODO.rst
5443

5544
|
5645

scripts/generate_bibtex.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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

Comments
 (0)