Update.
[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     id="$(echo "${id}" | sed -e 's/.pdf$//')"
37
38     curl -s > "${tmp}" "https://arxiv.org/abs/${id}"
39
40     AUTHORS=""
41
42     while read line
43     do
44         [[ "${AUTHORS}" ]] && AUTHORS="${AUTHORS} and "
45         AUTHORS="${AUTHORS}${line}"
46     done < <(grep '<meta name="citation_author"' "${tmp}" | sed -e 's/^.*content="\([^,]*, .\).*$/\1./')
47
48     TITLE=$(grep '<meta name="citation_title"' ${tmp} | sed -e 's/^.*content="\([^"]*\)".*$/\1/')
49     YEAR=$(echo ${id} | sed -e 's/^\(..\).*$/20\1/')
50
51     cat <<EOF
52 @article{arxiv-${id},
53   author={${AUTHORS}},
54   title={${TITLE}},
55   journal={CoRR},
56   volume={abs/${id}},
57   year={${YEAR}},
58   url={https://arxiv.org/pdf/${id}}
59 }
60 EOF
61
62     shift
63
64 done
65
66
67
68       rm -rf ${tmp}