Update.
[scripts.git] / arxiv.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}" ]] || (echo >&2 "Define \$ARXIV_BIBFILE"; exit 2)
25 [[ "${ARXIV_BIBDIR}" ]] || (echo >&2 "Define \$ARXIV_BIBDIR"; exit 2)
26
27 tmp=$(mktemp /tmp/arxiv-bib.sh.XXXXXX)
28
29 while [[ "$1" ]]
30 do
31
32     if [[ $1 =~ ^http ]] || [[ $1 =~ ^[0-9v\.]*$ ]]
33     then
34         id="$(echo "$1" | sed -e 's|^.*/\([0-9v.]*\)$|\1|' | sed -e 's/v[0-9]*$//')"
35         wget -U "Mozilla/5.0 (iPhone; CPU iPhone OS 11_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.0 Mobile/15E148 Safari/604.1" "https://arxiv.org/pdf/${id}"
36         mv "${id}" "${id}.pdf"
37         filename="${id}.pdf"
38     else
39         filename="$1"
40         id="$(basename ${filename} .pdf)"
41     fi
42
43     while [[ -f "${id}.pdf.part" ]] # dealing with firefox's download
44     do
45         echo "Waiting for ${id}.pdf.part to vanish"
46         sleep 1
47     done
48
49     echo -n "Fetching info from arxiv.org ... "
50
51     curl -s > "${tmp}" "https://arxiv.org/abs/${id}"
52
53     echo "done."
54
55     ######################################################################
56     # Bibtex entry
57
58     AUTHORS=""
59
60     while read line
61     do
62         [[ "${AUTHORS}" ]] && AUTHORS="${AUTHORS} and "
63         AUTHORS="${AUTHORS}${line}"
64     done < <(grep '<meta name="citation_author"' "${tmp}" | sed -e 's/^.*content="\([^,]*, .\).*$/\1./')
65
66     TITLE=$(grep '<meta name="citation_title"' ${tmp} | sed -e 's/^.*content="\([^"]*\)".*$/\1/')
67     YEAR=$(echo ${id} | sed -e 's/^\(..\).*$/20\1/')
68
69     if grep "${TITLE}" "${ARXIV_BIBFILE}"
70     then
71         echo
72         echo "This article seems to be already in ${ARXIV_BIBFILE}"
73         echo
74     else
75
76         cat >> ${ARXIV_BIBFILE} <<EOF
77
78 @article{arxiv-${id},
79   author={${AUTHORS}},
80   title={${TITLE}},
81   journal={CoRR},
82   volume={abs/${id}},
83   year={${YEAR}},
84   url={https://arxiv.org/pdf/${id}}
85 }
86 EOF
87
88     fi
89
90     ######################################################################
91     # Rename the file
92
93     AUTHORS=""
94     nb_authors=0
95
96     while read line
97     do
98         if [[ "${AUTHORS}" ]]
99         then
100             AUTHORS="${AUTHORS} "
101         else
102             FIRST_AUTHOR="${line}"
103         fi
104         nb_authors=$((nb_authors+1))
105         AUTHORS="${AUTHORS}${line}"
106     done < <(grep '<meta name="citation_author"' "${tmp}" | sed -e 's/^.*content="\([^,.]*\), .*$/\1/')
107
108     TITLE=$(grep '<meta name="citation_title"' ${tmp} | sed -e 's/^.*content="\([^"]*\)".*$/\1/')
109     YEAR=$(echo ${id} | sed -e 's/^\(..\).*$/20\1/')
110
111     if [[ ${nb_authors} -gt 3 ]]
112     then
113         AUTHORS="${FIRST_AUTHOR} et al"
114     fi
115
116     #TITLE="$(echo "${AUTHORS} - ${TITLE} ${YEAR} ${id}.pdf" | sed -e 's/ /_/g')"
117     TITLE="$(echo "${AUTHORS} - ${TITLE} ${YEAR} ${id}.pdf" | sed -e 's/[^-a-zA-Z0-9_\.\/]/_/g' | sed -e 's/__*/_/g')"
118
119     mv -v "${filename}" "${ARXIV_BIBDIR}/${TITLE}"
120
121     ######################################################################
122
123     shift
124
125 done
126
127 #rm -rf ${tmp}