Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up get_bib_from_doi #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions doi2bib/crossref.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ def get_bib_from_doi(doi, abbrev_journal=True, add_abstract=False):
doi: str
abbrev_journal: bool
If True try to abbreviate the journal name
add_abstract: bool
If True try to add abstract to bibtex
Returns
-------

Expand All @@ -75,22 +77,22 @@ def get_bib_from_doi(doi, abbrev_journal=True, add_abstract=False):
The bibtex string
"""
found, bib = get_bib(doi)
if found and abbrev_journal:

if found:
found, item = get_json(doi)
if found:
abbreviated_journal = item["message"]["short-container-title"]
if add_abstract and "abstract" in item["message"].keys():
abstract = item["message"]["abstract"]
bi = bibtexparser.loads(bib)
bi.entries[0]["abstract"] = abstract
bib = bibtexparser.dumps(bi)

if len(abbreviated_journal) > 0:
abbreviated_journal = abbreviated_journal[0].strip()
bib = re.sub(
r"journal = \{[^>]*?\}",
"journal = {" + abbreviated_journal + "}",
bib)

message = item.get("message")
if message:
abstract = message.get("abstract")
abbreviated_journal = message.get("short-container-title")
if add_abstract and abstract:
bi = bibtexparser.loads(bib)
bi.entries[0]["abstract"] = abstract
bib = bibtexparser.dumps(bi)
if abbrev_journal and abbreviated_journal:
abbreviated_journal = abbreviated_journal[0].strip()
bib = re.sub(
r"journal = \{[^>]*?\}",
"journal = {" + abbreviated_journal + "}",
bib
)
return found, bib