-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoi2bib.py
executable file
·154 lines (119 loc) · 3.41 KB
/
doi2bib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python
from __future__ import unicode_literals, print_function, absolute_import
from builtins import str
import argparse as arg
import bibtexparser
import pandas as pd
import requests
import csv
import sys
import re
bare_url = "http://api.crossref.org/"
def get_bib(doi):
"""
Parameters
----------
doi: str
Returns
-------
found: bool
bib: str
"""
url = "{}works/{}/transform/application/x-bibtex"
url = url.format(bare_url, doi)
r = requests.get(url)
found = False if r.status_code != 200 else True
bib = r.content
bib = str(bib, "utf-8")
return found, bib
def get_json(doi):
"""
Parameters
----------
doi: str
Returns
-------
found: bool
item: dict
Response from crossref
"""
url = "{}works/{}"
url = url.format(bare_url, doi)
r = requests.get(url)
found = False if r.status_code != 200 else True
item = r.json()
return found, item
def get_bib_from_doi(doi, abbrev_journal=True, add_abstract=False):
"""
Parameters
----------
doi: str
abbrev_journal: bool
If True try to abbreviate the journal name
Returns
-------
found: bool
bib: str
The bibtex string
"""
found, bib = get_bib(doi)
if found and abbrev_journal:
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 = \{[^>]*?\}",
r"journal = \{.*\}",
"journal = {" + abbreviated_journal + "}",
bib)
return found, bib
def options():
'''Defines the options of the script.'''
parser = arg.ArgumentParser(
formatter_class=arg.ArgumentDefaultsHelpFormatter)
#
# Input Options
#
inp = parser.add_argument_group("Input Data")
inp.add_argument('DOIs', nargs='?', default=None,
help='''DOIs.''')
inp.add_argument('-i', '--input', type=str, default=None, dest='InpFile',
help='''Input File.''')
#
# Output Options
#
out = parser.add_argument_group("Output Options")
out.add_argument('-o', '--output', default=None, type=str, dest='OutFile',
help='''Output File.''')
args = parser.parse_args()
Opts = vars(args)
return Opts
if __name__ == '__main__':
opts = options()
if opts['InpFile'] is None:
dois = opts['DOIs'].split()
else:
with open(opts['InpFile']) as f:
dois = list(map(str.strip, f.readlines()))
items = []
for i, doi in enumerate(dois, start=1):
found, item = get_bib_from_doi(doi)
if found:
items.append(item)
else:
items.append(doi)
if opts['OutFile'] is None:
for item in items:
print(item)
else:
with open(opts['OutFile'], "w") as f:
for item in items:
f.write(item)
f.write("\n%\n")