Created
May 12, 2022 14:17
-
-
Save kandersolar/a1d1e1c01d7b82d9c54d4a81f7b0e701 to your computer and use it in GitHub Desktop.
Get bibtex for a DOI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# doi -> bibtex\n", | |
"\n", | |
"A bare-bones, untested, probably buggy fallback option for when https://doi2bib.org is offline.\n", | |
"\n", | |
"Some docs for this lookup: https://citation.crosscite.org/docs.html" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import requests\n", | |
"import re\n", | |
"\n", | |
"\n", | |
"def fetch_bibtex(doi, orcids=False, format=\"application/x-bibtex\"):\n", | |
" prefix = \"https://doi.org/\"\n", | |
" url = doi if \"doi.org\" in doi else prefix + doi\n", | |
" response = requests.get(url, headers={\"Accept\": format})\n", | |
" bibtex = response.content.decode()\n", | |
" if not orcids:\n", | |
" return bibtex\n", | |
"\n", | |
" authors = {}\n", | |
" text = response.headers['link']\n", | |
" items = text.split(\", \")\n", | |
" for item in items:\n", | |
" if 'author' not in item:\n", | |
" continue\n", | |
" sections = item.split(\"; \")\n", | |
" orcid = re.search(r'orcid.org/(.*)>', item).group(1)\n", | |
" name = re.search(r'\"(.*)\"', sections[1]).group(1)\n", | |
" authors[name] = orcid\n", | |
"\n", | |
" return bibtex, authors" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"@article{F_Holmgren_2018,\n", | |
"\tdoi = {10.21105/joss.00884},\n", | |
"\turl = {https://doi.org/10.21105%2Fjoss.00884},\n", | |
"\tyear = 2018,\n", | |
"\tmonth = {sep},\n", | |
"\tpublisher = {The Open Journal},\n", | |
"\tvolume = {3},\n", | |
"\tnumber = {29},\n", | |
"\tpages = {884},\n", | |
"\tauthor = {William F. Holmgren and Clifford W. Hansen and Mark A. Mikofski},\n", | |
"\ttitle = {pvlib python: a python package for modeling solar energy systems},\n", | |
"\tjournal = {Journal of Open Source Software}\n", | |
"}\n" | |
] | |
} | |
], | |
"source": [ | |
"print(fetch_bibtex('https://doi.org/10.21105/joss.00884'))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"@article{F_Holmgren_2018,\n", | |
"\tdoi = {10.21105/joss.00884},\n", | |
"\turl = {https://doi.org/10.21105%2Fjoss.00884},\n", | |
"\tyear = 2018,\n", | |
"\tmonth = {sep},\n", | |
"\tpublisher = {The Open Journal},\n", | |
"\tvolume = {3},\n", | |
"\tnumber = {29},\n", | |
"\tpages = {884},\n", | |
"\tauthor = {William F. Holmgren and Clifford W. Hansen and Mark A. Mikofski},\n", | |
"\ttitle = {pvlib python: a python package for modeling solar energy systems},\n", | |
"\tjournal = {Journal of Open Source Software}\n", | |
"}\n", | |
"{'William F. Holmgren': '0000-0001-6218-9767', 'Clifford W. Hansen': '0000-0002-8620-5378', 'Mark A. Mikofski': '0000-0001-8001-8582'}\n" | |
] | |
} | |
], | |
"source": [ | |
"bibtex, orcids = fetch_bibtex('10.21105/joss.00884', orcids=True)\n", | |
"print(bibtex)\n", | |
"print(orcids)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"F. Holmgren, W., W. Hansen, C., & A. Mikofski, M. (2018). pvlib python: a python package for modeling solar energy systems. Journal of Open Source Software, 3(29), 884. https://doi.org/10.21105/joss.00884\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"print(fetch_bibtex('10.21105/joss.00884', format='text/x-bibliography; style=apa'))" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.7" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment