Last active
September 12, 2023 17:47
-
-
Save lucasamparo/089dab865afdb03513fe0e8eec9926f9 to your computer and use it in GitHub Desktop.
DOI 2 BibTex
This file contains 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
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() |
Author
lucasamparo
commented
Sep 12, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment