Update.
authorFrancois Fleuret <francois@fleuret.org>
Fri, 5 Jan 2018 20:17:14 +0000 (21:17 +0100)
committerFrancois Fleuret <francois@fleuret.org>
Fri, 5 Jan 2018 20:17:14 +0000 (21:17 +0100)
arxiv-bib.sh
arxiv-rename-pdf.sh [new file with mode: 0755]

index 45a6b34..f8407e2 100755 (executable)
@@ -25,16 +25,16 @@ tmp=$(mktemp /tmp/arxiv-bib.sh.XXXXXX)
 
 while [[ "$1" ]]
 do
-
-    id="$1"
-
-    if [[ ${id} =~ ^http ]]
+    if [[ $1 =~ ^http ]]
     then
-        id="$(echo "${id}" | sed -e 's|^.*/\([^\]*\)$|\1|')"
+        wget "$1"
+        id="$(echo "$1" | sed -e 's|^.*/\([^\]*\)$|\1|')"
+        filename="${id}.pdf"
+    else
+        filename="$1"
+        id="$(basename ${filename} .pdf)"
     fi
 
-    id="$(echo "${id}" | sed -e 's/.pdf$//')"
-
     curl -s > "${tmp}" "https://arxiv.org/abs/${id}"
 
     AUTHORS=""
diff --git a/arxiv-rename-pdf.sh b/arxiv-rename-pdf.sh
new file mode 100755 (executable)
index 0000000..5ec49c0
--- /dev/null
@@ -0,0 +1,118 @@
+#!/bin/bash
+
+#########################################################################
+# This program is free software: you can redistribute it and/or modify  #
+# it under the terms of the version 3 of the GNU General Public License #
+# as published by the Free Software Foundation.                         #
+#                                                                       #
+# This program is distributed in the hope that it will be useful, but   #
+# WITHOUT ANY WARRANTY; without even the implied warranty of            #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      #
+# General Public License for more details.                              #
+#                                                                       #
+# You should have received a copy of the GNU General Public License     #
+# along with this program. If not, see <http://www.gnu.org/licenses/>.  #
+#                                                                       #
+# Written by and Copyright (C) Francois Fleuret                         #
+# Contact <francois.fleuret@idiap.ch> for comments & bug reports        #
+#########################################################################
+
+
+set -e
+set -o pipefail
+
+ARXIV_BIBFILE="${HOME}/tex/teaching/dlc/dlc.bib"
+
+tmp=$(mktemp /tmp/arxiv-bib.sh.XXXXXX)
+
+while [[ "$1" ]]
+do
+
+    if [[ $1 =~ ^http ]]
+    then
+        wget "$1"
+        id="$(echo "$1" | sed -e 's|^.*/\([^\]*\)$|\1|')"
+        filename="${id}.pdf"
+    else
+        filename="$1"
+        id="$(basename ${filename} .pdf)"
+    fi
+
+    echo -n "Fetching info from arxiv.org ... "
+
+    curl -s > "${tmp}" "https://arxiv.org/abs/${id}"
+
+    echo "done."
+
+    ######################################################################
+    # Bibtex entry
+
+    AUTHORS=""
+
+    while read line
+    do
+        [[ "${AUTHORS}" ]] && AUTHORS="${AUTHORS} and "
+        AUTHORS="${AUTHORS}${line}"
+    done < <(grep -q '<meta name="citation_author"' "${tmp}" | sed -e 's/^.*content="\([^,]*, .\).*$/\1./')
+
+    TITLE=$(grep '<meta name="citation_title"' ${tmp} | sed -e 's/^.*content="\([^"]*\)".*$/\1/')
+    YEAR=$(echo ${id} | sed -e 's/^\(..\).*$/20\1/')
+
+    if grep "${TITLE}" "${ARXIV_BIBFILE}"
+    then
+        echo
+        echo "This article seems to be already in ${ARXIV_BIBFILE}"
+        echo
+    else
+
+        cat >> ${ARXIV_BIBFILE} <<EOF
+
+@article{arxiv-${id},
+  author={${AUTHORS}},
+  title={${TITLE}},
+  journal={CoRR},
+  volume={abs/${id}},
+  year={${YEAR}},
+  url={https://arxiv.org/pdf/${id}}
+}
+EOF
+
+    fi
+
+    ######################################################################
+    # Rename the file
+
+    AUTHORS=""
+    nb_authors=0
+
+    while read line
+    do
+        if [[ "${AUTHORS}" ]]
+        then
+            AUTHORS="${AUTHORS} "
+        else
+            FIRST_AUTHOR="${line}"
+        fi
+        nb_authors=$((nb_authors+1))
+        AUTHORS="${AUTHORS}${line}"
+    done < <(grep '<meta name="citation_author"' "${tmp}" | sed -e 's/^.*content="\([^,.]*\), .*$/\1/')
+
+    TITLE=$(grep '<meta name="citation_title"' ${tmp} | sed -e 's/^.*content="\([^"]*\)".*$/\1/')
+    YEAR=$(echo ${id} | sed -e 's/^\(..\).*$/20\1/')
+
+    if [[ ${nb_authors} -gt 3 ]]
+    then
+        AUTHORS="${FIRST_AUTHOR} et al"
+    fi
+
+    TITLE="$(echo "${AUTHORS} - ${TITLE} ${YEAR} ${id}.pdf" | sed -e 's/ /_/g')"
+
+    mv -v "${filename}" "${TITLE}"
+
+    ######################################################################
+
+    shift
+
+done
+
+rm -rf ${tmp}