Skip to content

Instantly share code, notes, and snippets.

@djalmajr
Last active April 16, 2020 18:04
Show Gist options
  • Save djalmajr/d7530c4e25a8b5f5cc88b55bc85310a6 to your computer and use it in GitHub Desktop.
Save djalmajr/d7530c4e25a8b5f5cc88b55bc85310a6 to your computer and use it in GitHub Desktop.
SpringerLink CSV to BIB converter
#!/bin/bash
# csv2bib.sh
#
# SpringerLink CSV to BIB
#
# Author: Djalma Jr.
# Site: djalmajr.com
#
# v0.3.0: Updated download link
# v0.2.0: Added command line options
# v0.1.0: First version
#
name=$(basename "$0")
usage="
Usage: $name [OPTIONS]
[OPTIONS]
-i, --input The CSV input file
-o, --output The BIB output file
-k, --keep Keep downloaded files
-h, --help Show this help screen
-V, --version Show the program version
"
while test -n "$1"; do
case "$1" in
-i | --input)
shift
csv="$1"
;;
-o | --output)
shift
bib="$1"
;;
-k | --keep)
keep=1
;;
-h | --help)
echo "$usage"
exit 0
;;
-V | --version)
echo -n $(basename "$0")
# Extracts the version from script header
grep "^# v" "$0" | head -1 | cut -d : -f 1 | tr -d \#
exit 0
;;
*)
if test -n "$1"; then
echo Invalid option: $1
exit 1
fi
;;
esac
shift # Option processed, remove from queue
done
if [[ -z "$csv" && -z "$bib" ]]; then
echo "$usage"
exit 1
fi
if [[ -z "$csv" || -z "$bib" ]]; then
echo "Missing input or output file"
exit 1
fi
results=$(cat $csv | grep -Eo "(http|https)://[a-zA-Z0-9./?=_-]*")
total=$(echo "$results" | wc -l)
tmpdir="tmp-${name//\.sh//}"
mkdir -p $tmpdir
echo ">>> Downloading files... ";
while ((i++)); read -r url; do
# file="${url//\.com\//\.com\/export-citation\/}.bib"
# code=$(echo "$url" | sed -r -e 's/([sS]?\d[-_.\/]?)+/\1/')
code=$(echo "$url" | sed -r -e 's/.*(article|chapter|referenceworkentry)\/(.*)/\2/')
file="https://citation-needed.springer.com/v2/references/$code?format=bibtex&flavour=citation"
echo "[$i/$total] Downloading $file"
wget -q -N "$file" -P $tmpdir
done < <(echo "$results")
echo ">>> Creating $bib file..."
cat $tmpdir/* > $bib
if [ -z "$keep" ]; then
echo ">>> Cleaning up..."
rm -rf $tmpdir
fi
echo ">>> Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment