Figure out a bibtex entry from a arxiv url or number.
[scripts.git] / arxiv-bib.sh
1 #!/bin/bash
2
3 #########################################################################
4 # This program is free software: you can redistribute it and/or modify  #
5 # it under the terms of the version 3 of the GNU General Public License #
6 # as published by the Free Software Foundation.                         #
7 #                                                                       #
8 # This program is distributed in the hope that it will be useful, but   #
9 # WITHOUT ANY WARRANTY; without even the implied warranty of            #
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      #
11 # General Public License for more details.                              #
12 #                                                                       #
13 # You should have received a copy of the GNU General Public License     #
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.  #
15 #                                                                       #
16 # Written by and Copyright (C) Francois Fleuret                         #
17 # Contact <francois.fleuret@idiap.ch> for comments & bug reports        #
18 #########################################################################
19
20
21 set -e
22 set -o pipefail
23
24 tmp=$(mktemp /tmp/arxiv-bib.sh.XXXXXX)
25
26 while [[ "$1" ]]
27 do
28
29     id="$1"
30
31     if [[ ${id} =~ ^http ]]
32     then
33         id="$(echo "${id}" | sed -e 's|^.*/\([^\]*\)$|\1|')"
34     fi
35
36     curl -s > "${tmp}" "https://arxiv.org/abs/${id}"
37
38     AUTHORS=""
39
40     while read line
41     do
42         [[ "${AUTHORS}" ]] && AUTHORS="${AUTHORS} and "
43         AUTHORS="${AUTHORS}${line}"
44     done < <(grep '<meta name="citation_author"' "${tmp}" | sed -e 's/^.*content="\([^,]*, .\).*$/\1./')
45
46     TITLE=$(grep '<meta name="citation_title"' ${tmp} | sed -e 's/^.*content="\([^"]*\)".*$/\1/')
47     YEAR=$(echo ${id} | sed -e 's/^\(..\).*$/20\1/')
48
49     cat <<EOF
50 @article{arxiv-${id},
51   author={${AUTHORS}},
52   title={${TITLE}},
53   journal={CoRR},
54   volume={abs/${id}},
55   year={${YEAR}},
56   url={https://arxiv.org/pdf/${id}}
57 }
58 EOF
59
60     shift
61
62 done
63
64
65
66       rm -rf ${tmp}