-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_publications.py
executable file
·415 lines (389 loc) · 13.7 KB
/
get_publications.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#!/usr/bin/env python
r"""
-------------------------------------------------------------------------------
This script retrieves publication data from NASA ADS and writes out each
publication as \item to a latex file. Open access information can be attached as
well. The default output file is `publication_list.txt`.
Notes:
------
Some UTF8 characters still might not work (like greek letters in paper titles)
in which case those need to be declared in the header below, for example:
\DeclareUnicodeCharacter{3BC}{$\mu$}
The `-p` option allows to include articles in press which are read from the text
file `in_press.txt` (or some file specified by `-in`). In this case the content
is split up into `\item`s and if open access information should be attached, it
is taken from `oa_info.py`. This file should just contain something like
OA_INPRESS = ['[OA]','[OA]','']
if there are for example three paper in press and the first two should be listed
as open access.
-------------------------------------------------------------------------------
"""
import urllib2, os, sys, re, json, codecs, argparse, subprocess
#
# set default values
#
OPENACCESS = False
CITATIONS = False
OA_INPRESS = []
AUTHOR = 'Birnstiel'
AUTHOR_F = 'Tilman'
FILE = 'publication_list.txt'
INFILE = 'in_press.txt'
DEVKEY = os.environ['ADS_DEV_KEY']
IN_PRESS = False
RUN = 'txt'
LATEX = 'pdflatex -interaction=nonstopmode'.split()
DATABASE = ''
#
# handle command line arguments
#
if __name__ == '__main__':
RTHF = argparse.RawTextHelpFormatter
PARSER = argparse.ArgumentParser()
PARSER = argparse.ArgumentParser(description=__doc__,formatter_class=RTHF)
GROUP = PARSER.add_mutually_exclusive_group()
GROUP.add_argument('-oa', '--openaccess',\
help='include open access information', action='store_true')
GROUP.add_argument('-c', '--citations',\
help='include citation information', action='store_true')
PARSER.add_argument('-r', '--run',\
help='what output to produce:\n'+\
r'`txt` = just `\item`s'+'\n'+\
'`tex` = full latex document\n'+\
'`pdf` = compiled pdf file\n'+\
'default=' + RUN,\
choices=['txt', 'tex', 'pdf'],\
type=str, default=RUN)
PARSER.add_argument('-p', '--in-press',\
help='include articles in press', action='store_true')
PARSER.add_argument('-db', '--database',\
help='select database, e.g. `astronomy`',\
type=str, default=DATABASE)
PARSER.add_argument('-a', '--author',\
help='Last name of Author, default='+AUTHOR,\
type=str, default=AUTHOR)
PARSER.add_argument('-i', '--initial',\
help='Authors first name, default='+AUTHOR_F,\
type=str, default=AUTHOR_F)
PARSER.add_argument('-d', '--devkey',\
help='NASA ADS Dev Key, default=' + DEVKEY,\
type=str, default=DEVKEY)
PARSER.add_argument('-out', '--output',\
help='output file, default=' + FILE,\
type=str, default=FILE)
PARSER.add_argument('-in', '--input',\
help='in-press input file, default=' + INFILE,\
type=str, default=INFILE)
PARSER.add_argument('-l', '--latex',\
help='latex command, default=' + ' '.join(LATEX),\
type=str, default=' '.join(LATEX))
PARSER.add_argument('-ys', '--year-start',\
help='include only publications after (including) this year',\
type=float, default=-1e6)
PARSER.add_argument('-ye', '--year-end',\
help='include only publications before (including) this year',\
type=float, default=1e6)
PARSER.add_argument('-lid', '--library-id',\
help='Use personal library with this id instead of a general query (needs also --library-name)',\
type=str, default=None)
PARSER.add_argument('-ln', '--library-name',\
help='Use personal library with this name instead of a general query (needs also --library-id)',\
type=str, default=None)
ARGS = PARSER.parse_args()
OPENACCESS = ARGS.openaccess
CITATIONS = ARGS.citations
RUN = ARGS.run
IN_PRESS = ARGS.in_press
DATABASE = ARGS.database.lower()
AUTHOR = ARGS.author
AUTHOR_F = ARGS.initial
DEVKEY = ARGS.devkey
FILE = ARGS.output
INFILE = ARGS.input
LATEX = ARGS.latex.split()
#
# print promotion
#
print('-----------------------')
print('Publication List Script')
print('-----------------------\n')
print('by Til Birnstiel')
print('https://github.com/birnstiel/get_publications\n')
#
# process options
#
if DEVKEY == '':
print('\nERROR:\n' +
'You need to specify a valid NASA ADS\n' +
'developer key, either by setting the\n' +
'environment variable `ADS_DEV_KEY` or\n' +
'by using the argument `-d`.\n')
sys.exit(1)
#
# display some information
#
print('Script will create publication list (file `{}`) for author: {} {}\n'.\
format(os.path.splitext(FILE)[0] + '.' + RUN, AUTHOR_F, AUTHOR))
if OPENACCESS:
if IN_PRESS:
from oa_info import OA_INPRESS
print('- Including open access information')
if CITATIONS:
print('- Including citations')
if RUN in ['tex', 'pdf']:
FILE = os.path.splitext(FILE)[0] + '.tex'
if RUN == 'txt':
FILE = os.path.splitext(FILE)[0] + '.txt'
#
# set header and footer of the latex document
#
HEAD = r"""
\documentclass[11pt,letterpaper]{amsart}
\usepackage[margin=3cm]{geometry}
\usepackage{enumitem}
\usepackage[utf8]{inputenc}
\DeclareUnicodeCharacter{3B1}{$\alpha$}
\DeclareUnicodeCharacter{3B4}{$\delta$}
\DeclareUnicodeCharacter{3BC}{$\mu$}
\usepackage{etaremune}
\usepackage{xspace}
\input{abbrev.tex}
%%%%%%%%%%%%%%%%%%%%%%
\usepackage{fancyhdr}
\usepackage{lastpage}
\renewcommand{\headrulewidth}{0pt}
\fancyhf{}
\fancyfoot[C]{%
\vspace{0.5cm}\small\emph{Page \thepage\ of \pageref{LastPage}}
}
\pagestyle{fancy}
\thispagestyle{fancy}
%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\begin{center}
\uppercase{{\large\textbf{List of Publications}}}\\
\vspace{0.3cm}
\textsc{-- FIRSTNAME LASTNAME --}\\
\end{center}
\begin{etaremune}[topsep=0pt,itemsep=0.5ex,partopsep=1ex,parsep=1ex]
"""
HEAD = HEAD.replace('FIRSTNAME', AUTHOR_F + '.' * (len(AUTHOR_F) == 1))
HEAD = HEAD.replace('LASTNAME', AUTHOR)
FOOT = r'\end{etaremune}'+'\n'+r'\end{document}'+'\n'
def replace_journal_name(journal):
"""
Replace some of the journal names with default abbreviations
as found in many journals, see for example here:
http://doc.adsabs.harvard.edu/abs_doc/aas_macros.sty
http://www.aanda.org/doc_journal/instructions/aa_instructions.pdf
"""
if journal == u'Annual Review of Astronomy and Astrophysics':
return r'\araa'
elif journal == u'Astronomische Nachrichten':
return r'AN'
elif journal == u'Astronomy and Astrophysics':
return r'\aap'
elif journal == u'Geochimica et Cosmochimica Acta Supplement':
return r'GCA'
elif journal == u'Icarus':
return r'Icarus'
elif journal == u'Monthly Notices of the Royal Astronomical Society':
return r'\mnras'
elif journal == u'Nature':
return r'\nat'
elif journal == u'Ph.D. Thesis':
return r'PhD Thesis'
elif journal == u'Physical Review D':
return r'\prd'
elif journal == u'Physical Review Letters':
return r'\prl'
elif journal == u'Protostars and Planets V':
return r'PPV'
elif journal == u'Protostars and Planets VI':
return r'PPVI'
elif journal == u'Science':
return r'\sci'
elif journal == u'The Astronomical Journal':
return r'\aj'
elif journal == u'The Astrophysical Journal':
return r'\apj'
elif journal == u'The Astrophysical Journal Supplement Series':
return r'\apjs'
else:
print('Unknown Journal {}, no replacement done!'.format(journal))
return journal
#
# read in_press file
#
sys.stdout.write('- reading from {} ... '.format(INFILE))
sys.stdout.flush()
if IN_PRESS:
PUBS_INPRESS = re.findall('\\\\item.*', open(INFILE).read())
print('Done ')
#
# get the publication data via the ADS API
#
sys.stdout.write('- getting publication data from NASA ADS ... ')
sys.stdout.flush()
URL = r'https://api.adsabs.harvard.edu/v1/search/query?q=author:%22'+AUTHOR+',+'+AUTHOR_F[0]+'%22&rows=200&fl=author,title,pub,pubdate,year,volume,page,bibcode,citation_count,property,doi,abstract&fq=property:refereed&sort=pubdate+desc';
if ARGS.library_id==None and ARGS.library_name==None:
pass
elif ARGS.library_id==None or ARGS.library_name==None:
raise ArgumentError('both or none of --library-id and --library-name need to be specified')
else:
raise ArgumentError('private libraries are not yet implemented for the new ADS API')
URL = r'https://api.adsabs.harvard.edu/v1/search/query?q=author:%22'+AUTHOR+',+'+AUTHOR_F[0]+'%22&rows=200&fl=author,title,pub,pubdate,year,volume,page,bibcode,citation_count,property,doi,abstract&fq=property:refereed&sort=pubdate+desc';
#URL = r'http://adsabs.harvard.edu/cgi-bin/export_privlib?libid='+ARGS.library_id+'&libname='+ARGS.library_name
#BIBCODES = [b['bibcode'] for b in json.load(urllib2.urlopen(URL))['entries']]
#URL = r'http://adslabs.org/adsabs/api/search/?q='+\
# 'bibcode:' + '&bibcode:'.join(BIBCODES) +\
# '&rows=2000&dev_key=' + DEVKEY
request = urllib2.Request(URL, headers={"Authorization" : "Bearer "+DEVKEY})
contents = urllib2.urlopen(request).read()
PUBS = json.loads(contents)['response']['docs']
print('Done')
#
# apply the database and year filters
#
N_PUBS = len(PUBS)
if DATABASE != '':
PUBS = [p for p in PUBS if any([db.lower() == DATABASE.lower() \
for db in p['database']])]
if len(PUBS) < N_PUBS: print('- DATABASE FILTER: FILTERED OUT {:d} PUBLICATIONS'.format(N_PUBS - len(PUBS)))
N_PUBS = len(PUBS)
PUBS = [p for p in PUBS if float(p['year'])>=ARGS.year_start]
if len(PUBS) < N_PUBS: print('- START YEAR FILTER: FILTERED OUT {:d} PUBLICATIONS'.format(N_PUBS - len(PUBS)))
N_PUBS = len(PUBS)
PUBS = [p for p in PUBS if float(p['year'])<=ARGS.year_end]
if len(PUBS) < N_PUBS: print('- END YEAR FILTER: FILTERED OUT {:d} PUBLICATIONS'.format(N_PUBS - len(PUBS)))
#
# open file to write out results
#
sys.stdout.write('- writing files ... ')
sys.stdout.flush()
FID = codecs.open(FILE, 'w', 'utf-8')
if RUN in ['tex', 'pdf']:
FID.write(HEAD)
#
# write in_press articles
#
if IN_PRESS:
for i, pub in enumerate(PUBS_INPRESS):
if OPENACCESS:
if OA_INPRESS[i] != '':
pub = pub + ' ' + OA_INPRESS[i]
FID.write((pub + '\n').decode("utf-8"))
#
# convert each publication in a latex item
#
for pub in PUBS:
string = r'\item '
#
# get last names
#
authors = [a.split(',')[0] for a in pub['author']]
#
# boldface author if initial matches
#
if AUTHOR in authors:
idx = authors.index(AUTHOR)
if pub['author'][idx].split(',')[1][1] == AUTHOR_F[0]:
authors[idx] = r'\textbf{' + authors[idx] + '}'
#
# format with commas between, and finish with ", and lastauthor"
#
if len(authors) == 1:
authors = authors[0]
elif len(authors) == 2:
authors = ' and '.join(authors)
else:
authors = ', '.join(authors[0:-1]) + ', and ' + authors[-1]
string += authors + ': '
#
# fix special characters in the title
#
title = pub['title'][0]
#specials = ['{', '}', '_', '$', '^']
#for c in specials:
# title = title.replace(c, '\\' + c)
title=re.sub('<sub>(.*?)</sub>',r'$_{\1}$',title,flags=re.IGNORECASE)
title=re.sub('<sup>(.*?)</sup>',r'$^{\1}$',title,flags=re.IGNORECASE)
if '$' not in title:
title=re.sub('(_[^ ]*)',r'$\1$',title)
title=re.sub('\{\^(.*?)\}',r'$^{\1}$',title)
print(title)
title = r'\textit{' + title + '}'
string += title + ', '
if 'pub' in pub.keys():
journal = replace_journal_name(pub['pub'])
else:
journal = ''
year = pub['pubdate'][0:4]
string += journal + ' (' + year + ')'
if 'volume' in pub.keys():
volume = pub['volume']
page = pub['page'][0]
string += ', vol. ' + volume + ', ' + page
string += '.'
#
# append the open access property
#
if OPENACCESS:
if 'PUB_OPENACCESS' in pub['property']:
#
# paper is open access
#
string += ' [OA]'
elif 'OPENACCESS' in pub['property']:
# or any(['arXiv' in i for i in pub['identifier']]):
#
# paper is on arxiv
#
string += ' [OA]*'
#
# citation count
#
if CITATIONS:
if 'citation_count' in pub.keys():
c = pub['citation_count']
else:
c = 0
string += ' [{:d} citation{}]'.format(c, 's' * (c != 1))
#
# write it out
#
FID.write(string + '\n')
#
# append open access legend
#
if OPENACCESS:
FID.write('\\\\[1em] \n[OA] = gold open access \\\\[0em] \n'+\
'[OA]* = green open access')
if CITATIONS:
FID.write('\\\\[1em] \n Total number of citations: {:d}'.\
format(sum([pub['citation_count'] for pub in PUBS if 'citation_count' in pub.keys()])))
if RUN in ['tex', 'pdf']:
FID.write(FOOT)
FID.close()
print('Done')
if RUN == 'pdf':
sys.stdout.write('- compiling latex file `{:}` ... '.format(FILE))
sys.stdout.flush()
#
# run latex twice
#
for i in range(2):
try:
p = subprocess.Popen(LATEX + [FILE], stderr=subprocess.PIPE,\
stdout=subprocess.PIPE)
ret_out, ret_err = p.communicate()
ret_val = p.poll()
except OSError as err:
ret_out = "OSError({0}): {1}".format(err.errno, err.strerror)
print('Done')
#
# clean up
#
for ext in ['aux', 'log', 'tex']:
os.unlink(os.path.splitext(FILE)[0] + '.' + ext)
print('\nScript finished!\n')