-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_pubs.py
executable file
·221 lines (174 loc) · 4.94 KB
/
update_pubs.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env python
from __future__ import unicode_literals, print_function, absolute_import
import re
import csv
import requests
import bibtexparser
import argparse as arg
from builtins import str
from impact_factor.core import Factor
from pyscopus import Scopus
bare_url = "http://api.crossref.org/"
import warnings
warnings.filterwarnings("ignore")
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(
'-a',
'--author',
type=str,
dest='Author',
help='''Scopus author ID.''',
default="24463809800"
)
inp.add_argument(
'-k',
'--key',
type=str,
required=True
dest='APIkey',
help='''Scopus API key.'''
)
#
# Output Options
#
out = parser.add_argument_group("Output Options")
out.add_argument(
'-p',
'--pre',
type=str,
dest='Prefix',
help='''Prefix for files to save and in .bib items.''',
default=None
)
out.add_argument(
'-o',
'--out',
type=str,
dest='Output',
choices=[ "csv", "bib" ],
help='''Format for files to save.''',
default=[ "csv", "bib" ]
)
args = parser.parse_args()
Opts = vars(args)
if not Opts["Prefix"]:
Opts["Prefix"] = Opts["Author"]
return Opts
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 get_IF(journal):
"""
Parameters
----------
journal: str
Returns
-------
IF: float
"""
engine = Factor()
data = engine.search(journal)
# Fix known bugs in names
if len(data) == 0:
journal = journal.replace(" and ", " & ")
journal = journal.replace(" - ", "-")
data = engine.search(journal)
try:
IF = data[0]['factor']
except:
IF = None
return IF
if __name__ == '__main__':
Opts = options()
# Interact with Scopus
scopus = Scopus(Opts["APIkey"])
pub_df = scopus.search_author_publication(Opts["Author"])
dois = pub_df["doi"].values.tolist()
# Save a csv
if "csv" in Opts["Output"]:
df = pub_df[["scopus_id", "doi", "cover_date", "publication_name", "title"]]
df["IF"] = df["publication_name"].apply(get_IF)
df = df[["scopus_id", "doi", "cover_date", "IF", "publication_name", "title"]]
df.to_csv("%s_pubs.csv" % Opts["Prefix"], index=False, quoting=csv.QUOTE_NONNUMERIC)
# Save a bibt
if "bib" in Opts["Output"]:
bibitems = [ get_bib_from_doi(x)[1] for x in dois ]
n = len(bibitems)
bib = ""
for i, bibitem in enumerate(bibitems):
replacement = "@article{%s%d,\n" % (Opts["Prefix"], n - i)
b = bibitem.split("\n")[1:]
newbibitem = replacement + "\n".join(b)
newbibitem += "\n%\n"
bib += newbibitem
with open("%s_publications.bib" % Opts["Prefix"], "w") as f:
f.write(bib)