Skip to content

Commit

Permalink
refactor scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
wildone committed Oct 27, 2019
1 parent 1d20376 commit 0589dcb
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 140 deletions.
43 changes: 43 additions & 0 deletions scripts/download/download-gdrive.sh
Original file line number Diff line number Diff line change
@@ -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


}

187 changes: 92 additions & 95 deletions scripts/download/download.sh
Original file line number Diff line number Diff line change
@@ -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 "$@"
45 changes: 0 additions & 45 deletions scripts/download/gdrive.sh

This file was deleted.

0 comments on commit 0589dcb

Please sign in to comment.