Skip to content

Instantly share code, notes, and snippets.

@lucasamparo
Last active September 12, 2023 17:47
Show Gist options
  • Save lucasamparo/089dab865afdb03513fe0e8eec9926f9 to your computer and use it in GitHub Desktop.
Save lucasamparo/089dab865afdb03513fe0e8eec9926f9 to your computer and use it in GitHub Desktop.
DOI 2 BibTex
import requests as rq
import bibtexparser as parser
import argparse
import progressbar
def readFile(path):
doi_list = []
f = open(path, "r")
for doi in f:
if len(doi) > 0:
doi_list.append(doi.replace("\n", ""))
return doi_list
line_parser = argparse.ArgumentParser(description="Doi To BibTex Online Converter")
line_parser.add_argument(
"--list", type=str,
help="Path to the DOIs List",
required=True)
line_parser.add_argument(
"--export", type=bool, nargs="?",
const=True, help="Flag to export the bibtex loaded",
default=False)
line_parser.add_argument(
"--output", type=str,
help="Path to save the loaded DOIs",
default="references.bib", required=False)
args = line_parser.parse_args()
url = 'https://citation.crosscite.org/format'
dois = readFile(args.list)
print("Loaded {} DOIs".format(len(dois)))
widgets = [progressbar.Percentage(), progressbar.Bar()]
bar = progressbar.ProgressBar(widgets=widgets, max_value=len(dois)).start()
bib_list = []
for i, doi in enumerate(dois):
params = {
'doi': doi,
'style': 'bibtex',
'lang': 'en-US'
}
r = rq.get(url, params=params)
bib_list.append(r.text.strip())
bar.update(i)
bib_list_str = "\n".join(bib_list)
library = parser.parse_string(bib_list_str)
print("")
print("All DOIs downloaded!")
print("Valid DOIs: {}".format(len(library.entries)))
print("Unvalid DOIs: {}".format(len(library.failed_blocks)))
for entry in library.entries:
for field in entry.fields:
if "pages" == field.key:
tmp_str = field.value.replace("â","-")
field.value = tmp_str
if args.export:
print("Saving bibtex file at {}".format(args.output))
f = open(args.output, "w")
f.write(parser.write_string(library))
f.close()
@brschettini
Copy link

checar se o len(sys.argv) = 1 n seria melhor aqui? Se mandar nada ele passa.

@lucasamparo
Copy link
Author

checar se o len(sys.argv) = 1 n seria melhor aqui? Se mandar nada ele passa.

$ python doi2bib.py
Usage: python doi2bib.py <path_to_doi_list>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment