From 0589dcb09b2d15945d07f8757020f9b28857d1a4 Mon Sep 17 00:00:00 2001 From: wildone Date: Sun, 27 Oct 2019 19:07:46 +1100 Subject: [PATCH] refactor scripts --- scripts/download/download-gdrive.sh | 43 +++++++ scripts/download/download.sh | 187 ++++++++++++++-------------- scripts/download/gdrive.sh | 45 ------- 3 files changed, 135 insertions(+), 140 deletions(-) create mode 100755 scripts/download/download-gdrive.sh delete mode 100755 scripts/download/gdrive.sh diff --git a/scripts/download/download-gdrive.sh b/scripts/download/download-gdrive.sh new file mode 100755 index 0000000..02d410a --- /dev/null +++ b/scripts/download/download-gdrive.sh @@ -0,0 +1,43 @@ +#!/bin/bash + +function downloadGDriveHelp() { + echo "Usage:" + echo " ./gdrive.sh [ACTION] [FILE ID] [FILE NAME]" + echo "" + echo " ACTIONS" + echo " - download" +} + +function doDownloadGDrive() { + + local FILEID=${1?Need file id} + local FILENAME=${2?Need file name} + echo "download: $FILEID" + + if [[ ! -d tmp ]]; then + echo "Creating temp folder" + mkdir tmp + fi + + curl -c ./tmp/cookie -s -L "https://drive.google.com/uc?export=download&id=${FILEID}" > /dev/null + curl -Lb ./tmp/cookie "https://drive.google.com/uc?export=download&confirm=`awk '/download/ {print $NF}' ./tmp/cookie`&id=${FILEID}" -o ${FILENAME} + +} + +function downloadGDrive() { + local ACTION=${1?Need action} + local FILEID=${2?Need file id} + local FILENAME=${3?Need file name} + + case $ACTION in + download) + doDownloadGDrive "$FILEID" "$FILENAME" + ;; + *) + downloadGDriveHelp + ;; + esac + + +} + diff --git a/scripts/download/download.sh b/scripts/download/download.sh index 969f5ab..3a99cfa 100755 --- a/scripts/download/download.sh +++ b/scripts/download/download.sh @@ -1,120 +1,117 @@ #!/bin/bash -set -e - -# get current script location -CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" - -function help() { -echo "Usage:" -echo " ./download.sh {[FILE NAME PREFIX] [AUTH] [MODULE] [URL]}..." -echo "" -echo " FILENAME_PREFIX:" -echo " - filename prefix to use, [-] = none" -echo " AUTH:" -echo " - auth to use, [-] = none" -echo " MODULE:" -echo " - module to use, [-] = none" -echo " URL:" -echo " - url to get" -} -function download() { +function downloadHelp() { + echo "Usage:" + echo " ./download.sh {[FILE NAME PREFIX] [AUTH] [MODULE] [URL]}..." + echo "" + echo " FILENAME_PREFIX:" + echo " - filename prefix to use, [-] = none" + echo " AUTH:" + echo " - auth to use, [-] = none" + echo " MODULE:" + echo " - module to use, [-] = none" + echo " URL:" + echo " - url to get" +} - local FILENAME_PREFIX=${1?Need file name prefix} - local FILEURL=${2?Need file url} - local MODULE=${3:-} - echo "download: $FILEURL" - echo "module: $MODULE" - - local FILENAME=$(basename "$FILEURL") - - if [[ ! "$MODULE" == "" && ! "$MODULE" == "-" ]]; then - MODULE_SCRIPT="${CURRENT_DIR}/$(echo $MODULE | sed -e 's/\(.*\):.*/\1/').py" - echo "script: ${MODULE_SCRIPT}" - - if [[ ! -f "${MODULE_SCRIPT}" ]]; then - echo "module: error, could not find module script" - exit 0 - fi - - FILTER=$(echo $MODULE | sed -e 's/.*:\(.*\)/\1/') - echo "filter: ${FILTER}" - echo "url: ${FILEURL}" - FILEURL_FILTER_URL=$(${MODULE_SCRIPT} ${FILTER} ${FILEURL}) - echo "FILEURL_FILTER_URL:" - echo ${FILEURL_FILTER_URL} - if [[ "${FILEURL_FILTER_URL}" == "" ]]; then - echo "module: error, could not get url from module" - exit 0 - fi - FILENAME=$(basename "${FILEURL_FILTER_URL}") - FILEURL=${FILEURL_FILTER_URL} - fi - - echo "DOWNLOADING $FILEURL to ${FILENAME_PREFIX}${FILENAME}" - curl \ - --connect-timeout 30 \ - --retry 300 \ - --retry-delay 5 \ - -L "${FILEURL}" -o ${FILENAME_PREFIX}${FILENAME} +function doDownload() { + + # get current script location + local CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" + + local FILENAME_PREFIX=${1?Need file name prefix} + local FILEURL=${2?Need file url} + local MODULE=${3:-} + echo "download: $FILEURL" + echo "module: $MODULE" + + local FILENAME=$(basename "$FILEURL") + + if [[ ! "$MODULE" == "" && ! "$MODULE" == "-" ]]; then + MODULE_SCRIPT="${CURRENT_DIR}/$(echo $MODULE | sed -e 's/\(.*\):.*/\1/').py" + echo "script: ${MODULE_SCRIPT}" + + if [[ ! -f "${MODULE_SCRIPT}" ]]; then + echo "module: error, could not find module script" + exit 0 + fi + + FILTER=$(echo $MODULE | sed -e 's/.*:\(.*\)/\1/') + echo "filter: ${FILTER}" + echo "url: ${FILEURL}" + FILEURL_FILTER_URL=$(${MODULE_SCRIPT} ${FILTER} ${FILEURL}) + echo "FILEURL_FILTER_URL:" + echo ${FILEURL_FILTER_URL} + if [[ "${FILEURL_FILTER_URL}" == "" ]]; then + echo "module: error, could not get url from module" + exit 0 + fi + FILENAME=$(basename "${FILEURL_FILTER_URL}") + FILEURL=${FILEURL_FILTER_URL} + fi + + echo "DOWNLOADING $FILEURL to ${FILENAME_PREFIX}${FILENAME}" + curl \ + --connect-timeout 30 \ + --retry 300 \ + --retry-delay 5 \ + -L "${FILEURL}" -o ${FILENAME_PREFIX}${FILENAME} } -function downloadAuth() { +function doDownloadAuth() { - local FILENAME_PREFIX=${1?Need file name prefix} - local BASICCREDS=${2?Need username password} - local FILEURL=${3?Need file url} - local MODULE=${4:-} - echo "download: $FILEURL" - echo "module: not supported" + local FILENAME_PREFIX=${1?Need file name prefix} + local BASICCREDS=${2?Need username password} + local FILEURL=${3?Need file url} + local MODULE=${4:-} + echo "download: $FILEURL" + echo "module: not supported" - local FILENAME=$(basename "$FILEURL") + local FILENAME=$(basename "$FILEURL") - echo "DOWNLOADING $FILENAME into ${FILENAME_PREFIX}${FILENAME}" - curl \ - --connect-timeout 30 \ - --retry 300 \ - --retry-delay 5 \ - -A "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)" \ - -k \ - -u "${BASICCREDS}" -L "${FILEURL}" -o ${FILENAME_PREFIX}${FILENAME} + echo "DOWNLOADING $FILENAME into ${FILENAME_PREFIX}${FILENAME}" + curl \ + --connect-timeout 30 \ + --retry 300 \ + --retry-delay 5 \ + -A "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)" \ + -k \ + -u "${BASICCREDS}" -L "${FILEURL}" -o ${FILENAME_PREFIX}${FILENAME} } -function main() { +function download() { - if [[ $# -eq 0 ]]; then - help - exit 1 - fi + if [[ $# -eq 0 ]]; then + downloadHelp + exit 1 + fi - local ACTIONS_COUNT=$# - local ACTIONS=($@) + local ACTIONS_COUNT=$# + local ACTIONS=($@) - for (( i=0; i<=$ACTIONS_COUNT; i+=3 )) - do + for (( i=0; i<=$ACTIONS_COUNT; i+=3 )) + do - local FILENAME_PREFIX=${ACTIONS[$i]} - local AUTH=${ACTIONS[$(($i + 1))]} - local FLAGS=${ACTIONS[$(($i + 2))]} - local URL=${ACTIONS[$(($i + 3))]} + local FILENAME_PREFIX=${ACTIONS[$i]} + local AUTH=${ACTIONS[$(($i + 1))]} + local FLAGS=${ACTIONS[$(($i + 2))]} + local URL=${ACTIONS[$(($i + 3))]} - if [[ ! $FILENAME_PREFIX == "" && ! $AUTH == "" && ! $URL == "" ]]; then + if [[ ! $FILENAME_PREFIX == "" && ! $AUTH == "" && ! $URL == "" ]]; then - if [[ $AUTH == "-" ]]; then - download "$FILENAME_PREFIX" "$URL" "$FLAGS" - else - downloadAuth "$FILENAME_PREFIX" "$AUTH" "$URL" "$FLAGS" - fi + if [[ $AUTH == "-" ]]; then + doDownload "$FILENAME_PREFIX" "$URL" "$FLAGS" + else + doDownloadAuth "$FILENAME_PREFIX" "$AUTH" "$URL" "$FLAGS" + fi - fi + fi - done + done } - -main "$@" diff --git a/scripts/download/gdrive.sh b/scripts/download/gdrive.sh deleted file mode 100755 index 06a57d1..0000000 --- a/scripts/download/gdrive.sh +++ /dev/null @@ -1,45 +0,0 @@ -#!/bin/bash - -function help() { -echo "Usage:" -echo " ./gdrive.sh [ACTION] [FILE ID] [FILE NAME]" -echo "" -echo " ACTIONS" -echo " - download" -} - -function download() { - - local FILEID=${1?Need file id} - local FILENAME=${2?Need file name} - echo "download: $FILEID" - - if [[ ! -d tmp ]]; then - echo "Creating temp folder" - mkdir tmp - fi - - curl -c ./tmp/cookie -s -L "https://drive.google.com/uc?export=download&id=${FILEID}" > /dev/null - curl -Lb ./tmp/cookie "https://drive.google.com/uc?export=download&confirm=`awk '/download/ {print $NF}' ./tmp/cookie`&id=${FILEID}" -o ${FILENAME} - -} - -function main() { - local ACTION=${1?Need action} - local FILEID=${2?Need file id} - local FILENAME=${3?Need file name} - - case $ACTION in - download) - download "$FILEID" "$FILENAME" - ;; - *) - help - ;; - esac - - -} - - -main "$@"