Update.
[scripts.git] / arxiv-rename-pdf.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 ARXIV_BIBFILE="${HOME}/tex/teaching/dlc/dlc.bib"
25
26 tmp=$(mktemp /tmp/arxiv-bib.sh.XXXXXX)
27
28 while [[ "$1" ]]
29 do
30
31     if [[ $1 =~ ^http ]]
32     then
33         wget "$1"
34         id="$(echo "$1" | sed -e 's|^.*/\([^\]*\)$|\1|')"
35         filename="${id}.pdf"
36     else
37         filename="$1"
38         id="$(basename ${filename} .pdf)"
39     fi
40
41     echo -n "Fetching info from arxiv.org ... "
42
43     curl -s > "${tmp}" "https://arxiv.org/abs/${id}"
44
45     echo "done."
46
47     ######################################################################
48     # Bibtex entry
49
50     AUTHORS=""
51
52     while read line
53     do
54         [[ "${AUTHORS}" ]] && AUTHORS="${AUTHORS} and "
55         AUTHORS="${AUTHORS}${line}"
56     done < <(grep -q '<meta name="citation_author"' "${tmp}" | sed -e 's/^.*content="\([^,]*, .\).*$/\1./')
57
58     TITLE=$(grep '<meta name="citation_title"' ${tmp} | sed -e 's/^.*content="\([^"]*\)".*$/\1/')
59     YEAR=$(echo ${id} | sed -e 's/^\(..\).*$/20\1/')
60
61     if grep "${TITLE}" "${ARXIV_BIBFILE}"
62     then
63         echo
64         echo "This article seems to be already in ${ARXIV_BIBFILE}"
65         echo
66     else
67
68         cat >> ${ARXIV_BIBFILE} <<EOF
69
70 @article{arxiv-${id},
71   author={${AUTHORS}},
72   title={${TITLE}},
73   journal={CoRR},
74   volume={abs/${id}},
75   year={${YEAR}},
76   url={https://arxiv.org/pdf/${id}}
77 }
78 EOF
79
80     fi
81
82     ######################################################################
83     # Rename the file
84
85     AUTHORS=""
86     nb_authors=0
87
88     while read line
89     do
90         if [[ "${AUTHORS}" ]]
91         then
92             AUTHORS="${AUTHORS} "
93         else
94             FIRST_AUTHOR="${line}"
95         fi
96         nb_authors=$((nb_authors+1))
97         AUTHORS="${AUTHORS}${line}"
98     done < <(grep '<meta name="citation_author"' "${tmp}" | sed -e 's/^.*content="\([^,.]*\), .*$/\1/')
99
100     TITLE=$(grep '<meta name="citation_title"' ${tmp} | sed -e 's/^.*content="\([^"]*\)".*$/\1/')
101     YEAR=$(echo ${id} | sed -e 's/^\(..\).*$/20\1/')
102
103     if [[ ${nb_authors} -gt 3 ]]
104     then
105         AUTHORS="${FIRST_AUTHOR} et al"
106     fi
107
108     TITLE="$(echo "${AUTHORS} - ${TITLE} ${YEAR} ${id}.pdf" | sed -e 's/ /_/g')"
109
110     mv -v "${filename}" "${TITLE}"
111
112     ######################################################################
113
114     shift
115
116 done
117
118 rm -rf ${tmp}