aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiquel Lionel <lionelmiquel@sfr.fr>2020-11-18 23:59:16 +0100
committerroot <root@localhost>2020-11-18 23:59:16 +0100
commite9db9113f538803873571464c84dce5586f9afe1 (patch)
tree996b0db24be659164320cfb0e61f1a3ec62cfb4f
parent46c435b5a431dc0346a95acbe102133375e31867 (diff)
downloadrandopitons-e9db9113f538803873571464c84dce5586f9afe1.tar.gz
randopitons-e9db9113f538803873571464c84dce5586f9afe1.zip
Various modifications of the script.
-rw-r--r--randopitons.7.gzbin466 -> 800 bytes
-rw-r--r--randopitons.sh117
2 files changed, 71 insertions, 46 deletions
diff --git a/randopitons.7.gz b/randopitons.7.gz
index 7b6feff..c585aeb 100644
--- a/randopitons.7.gz
+++ b/randopitons.7.gz
Binary files differ
diff --git a/randopitons.sh b/randopitons.sh
index 3cba118..2863c9b 100644
--- a/randopitons.sh
+++ b/randopitons.sh
@@ -1,42 +1,72 @@
#!/usr/bin/env bash
-# Bash3 Boilerplate. Copyright (c) 2014, kvz.io
#set -o errexit
#set -o pipefail
#set -o nounset
#set -o xtrace
+BOLD='\033[01m'
+UNDL='\033[04m'
+GREEN='\033[32m'
+RED='\033[31m'
+STYLE_END='\033[0m'
# Set magic variables for current file & dir
-__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
-__file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
-__base="$(basename ${__file} .sh)"
-__root="$(cd "$(dirname "${__dir}")" && pwd)" # <-- change this as it depends on your app
-REGIONS=$(cat ${__dir}/regions.txt)
-REGIONFILE="${__dir}/regions.txt"
+DATADIR="$HOME/.local/share/randopitons"
+COOKIES_FILE="$DATADIR/cookies.txt"
+REGIONFILE="$DATADIR/regions.txt"
+S256_REGIONFILE="20eb46831f436bb467b850835c7efde1d51742434fcea9addf4754b9545aae47"
+MATCHEDFILE="$(mktemp)"
MAPTYPE="gpx"
RDPUSER=
RDPUSERPASS=
LOGINOK=
+
#Filechecks
-printf "Cirque de Cilaos\nCirque de Mafate\nCirque de Salazie\nEst\nNord\nOuest\nSud\nVolcan\nAilleurs\nAll" > $REGIONFILE
+mkdir -p $DATADIR
+if [ ! -w $REGIONFILE ]; then
+ printf "\nRegenerating $REGIONFILE...\n"
+ printf "Cirque de Cilaos\nCirque de Mafate\nCirque de Salazie\nEst\nNord\nOuest\nSud\nVolcan\nAilleurs\nAll" > $REGIONFILE
+fi
+
+if [ "$S256_REGIONFILE" != "$(sha256sum $REGIONFILE | cut -d' ' -f1)" ]; then
+ printf "\n$REGIONFILE sha256sum not matching, regenerating...\n"
+ printf "Cirque de Cilaos\nCirque de Mafate\nCirque de Salazie\nEst\nNord\nOuest\nSud\nVolcan\nAilleurs\nAll" > $REGIONFILE
+fi
#https://stackoverflow.com/questions/4813092/how-to-read-entire-line-from-bash
#so we don't use a while loop
IFS=$'\n'
-_download()
+_check_connectivity()
{
+ ping -c 1 -q -W 3 example.com > /dev/null 2>&1 || (printf "No internet connectivity. Check your network settings and status.\n" && return 1)
+}
+_download()
+{
+ _check_connectivity || exit 1
while read linefromfile; do
- mkdir -p ${__dir}/randopfiles/"${linefromfile}"
- cd ${__dir}/randopfiles/"${linefromfile}"
+ if [ ! -s $COOKIES_FILE ]; then
+ printf "\nCookie file not detected. Re-login using the -u switch to regenerate it.\n"
+ exit 1
+ fi
+
+ DOWNLOAD_DIR=${__dir}/randopfiles/"${linefromfile}"/${MAPTYPE}
+ printf "Trace files for the ${WEBREGION} with extension '${MAPTYPE}' will be downloaded to ${BOLD}$DOWNLOAD_DIR${STYLE_END}"
+
+ mkdir -p $DOWNLOAD_DIR
+ cd $DOWNLOAD_DIR
WEBREGION=$(echo ${linefromfile} | sed 's/ de /\-/g' | tr '[:upper:]' '[:lower:]')
- FILENBRS=$(wget -qO- https://randopitons.re/randonnees/region/${WEBREGION} | grep "<tr rid" | cut -d \" -f2)
- for nbr in $FILENBRS; do
- wget -w 3 --content-disposition --load-cookies ${__dir}/cookiejar.txt "https://randopitons.re/randonnee/${nbr}/trace/${MAPTYPE}"
+ printf "\nExtracting URL to download geo files from...\n"
+ TRACES=$(curl --silent https://randopitons.re/randonnees/region/${WEBREGION} | grep "<tr rid" | cut -d \" -f2) || (printf "Failed extraction. Aborting.\n" && exit 1)
+
+ for trace in $TRACES; do
+ sleep 3
+ wget --quiet --no-config --content-disposition --load-cookies $COOKIES_FILE "https://randopitons.re/randonnee/${trace}/trace/${MAPTYPE}" && printf "Succesfully download trace ${trace}\n" || (printf "Failed downloading trace ${trace} ...\n")
done
- cd ..
+
+ cd ${__dir}
done <$1
}
@@ -49,7 +79,10 @@ _help()
_credentials()
{
-
+ while [ -z $RDPUSER ]; do
+ printf "No username detected on -u switch. Enter it: "
+ read -r RDPUSER
+ done
printf "\nYour username is $RDPUSER. (You can press CTRL+C to cancel the script if this info is incorrect)."
printf "\nPassword (for randopitons.re): "
read -s RDPUSERPASS
@@ -57,7 +90,8 @@ _credentials()
_logincheck()
{
- LOGINTEST=$(wget -qO- --save-cookies ${__dir}/cookiejar.txt --keep-session-cookies --post-data="mail=${RDPUSER}&password=${RDPUSERPASS}" --delete-after https://randopitons.re/connexion | grep "L'adresse mail ou le mot de passe ne correspondent pas.")
+ _check_connectivity || exit 1
+ LOGINTEST=$(wget --no-config -qO- --save-cookies $COOKIES_FILE --keep-session-cookies --post-data="mail=${RDPUSER}&password=${RDPUSERPASS}" --delete-after https://randopitons.re/connexion | grep "L'adresse mail ou le mot de passe ne correspondent pas.")
if [ -z "$LOGINTEST" ];then
echo "Login is successful."
LOGINOK=true
@@ -65,16 +99,12 @@ _logincheck()
else
echo "Login is not successful."
LOGINOK=false
- rm ${__dir}/cookiejar.txt
+ rm $COOKIES_FILE
exit
fi
}
-if [ "$1" = "" ];then
- _help
-fi
-
while [ "$1" != "" ]; do
case $1 in
@@ -90,56 +120,51 @@ while [ "$1" != "" ]; do
MAPTYPE="$1"
while [ $MAPTYPE != "gpx" -a $MAPTYPE != "trk" -a $MAPTYPE != "kml" ]
do
- printf "\nMaptype supplied is not correct"
+ printf "\nThe supplied map extension is not correct"
printf "Which map filetype you want to set : gpx(default),trk or kml ?"
read -N 3 MAPTYPE
done
- printf "\nThe $MAPTYPE maptype is a valid choice !"
+ printf "\nThe $MAPTYPE map extension has been selected !\n"
;;
-lr | --list-regions)
- printf "Here's a list:\n"
- cat $REGIONFILE
+ printf "\n%s\n" "$(cat $REGIONFILE)"
;;
-lm | --list-maptypes)
- printf "These are the valid maptypes:\ngpx\nkml\ntrk"
+ printf "These are the valid maptypes:\ngpx\nkml\ntrk\n"
;;
-r | --region )
shift
- MATCHEDFILE="${__dir}/matched.txt"
- grep -iE "$1" $REGIONFILE > $MATCHEDFILE
+
- if [ -s $MATCHEDFILE ];then
- echo "Nothing matched or you entered an empty value."
- _help
+ if ! grep -iE "$1" $REGIONFILE > $MATCHEDFILE; then
+ printf "No region matched or you entered an empty value.\n"
+ exit 1
fi
-
- printf "Your region choice was '$1'\nMatches are:\n"
- cat $MATCHEDFILE
- _download $MATCHEDFILE
+
+ printf "Argument '$1' matched. Proceeding with download...\n"
+ if [ "$1" != 'All' ]; then
+ _download $MATCHEDFILE
+ else
+ _download $REGIONFILE
+ fi
;;
-a | --all )
printf "\nThis will download all the hitchiking routes from all
regions.\nIf no extension is specified (with -mp or --maptype), it will
- default to gpx.\nPausing for 5 seconds before downloading..."
- sleep 5
+ default to gpx."
_download $REGIONFILE
;;
- -em | --elevator-music )
- echo "Initiating elevator music"
- mpv --no-audio --really-quiet https://www.youtube.com/watch?v=KfNXGY9O5VY || printf "\nmpv is not installed."
- ;;
-
-h | --help )
_help
;;
-
- * )
- _help
+ * )
+ _help
+ ;;
esac
shift
done