Skip to content

Instantly share code, notes, and snippets.

@mikeguidry
Last active June 21, 2020 14:54
Show Gist options
  • Save mikeguidry/8bf7b2116f92978fb02283035b9ddb81 to your computer and use it in GitHub Desktop.
Save mikeguidry/8bf7b2116f92978fb02283035b9ddb81 to your computer and use it in GitHub Desktop.
#!/bin/bash
#do we wanna limit bandwidth? pass argument BW:limit (ie: BW:10)
if [[ "$1" == "BW:"* ]]; then
NEWBW=`echo $1|cut -d: -f2`
if [ "$NEWBW" != "" ]; then
BW="--limit-rate=${NEWBW}k"
echo using bw limit ${NEWBW}
fi
shift
fi
if [ "$2" == "" ]; then
echo $0 gdrive_id output_file
exit
fi
# trap ctrl-c and call ctrl_c() (so we can ctrl-c with the temp file and still have things function properly)
trap ctrl_c INT
function ctrl_c() {
#echo "** Trapped CTRL-C, so the rest of the script after wget executes (so partial content gets used next time)"
WANT_EXIT=1
}
G_ID=$1
HASH=`echo ${G_ID}|md5sum -|cut -d\ -f1`
TMPFILE=${HASH}.tmp
OUTFILE=$2
COOKIEFILE=/tmp/${HASH}.cookies
WANT_EXIT=0
#resume download to temp file (using temp in case we get some weird html page back.. dont wanna ruin real data)
#inf loop and check if ctrl-c was pressed, or completed, etc
#uses temp file because google returning that html/text page can mess up the download
while :
do
if [ "$WANT_EXIT" == "1" ]; then
exit
fi
CONFIRM=`wget -U "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36" --quiet --save-cookies /tmp/${HASH}.cookies --keep-session-cookies --no-check-certificate "https://docs.google.com/uc?export=download&id=$G_ID" -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p'` 1>/dev/null 2>&-
#had html overwrite on one time... so this can help in case it happens
if [ -f "${OUTFILE}" ]; then
rm -f ${TMPFILE}
cp -f ${OUTFILE} ${TMPFILE}
fi
wget -t 9999 -c $BW -U "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36" --load-cookies /tmp/${HASH}.cookies "https://docs.google.com/uc?export=download&confirm=$CONFIRM&id=$G_ID" -O ${TMPFILE} 2>&1 | tee ${HASH}.log
rm -rf /tmp/${HASH}.cookies
echo "********************************"
#google sometimes returns some confirmation page as text/html..its a failure..dont let it mess up our operation
BAD=`grep "text/html" ${HASH}.log`
#416 means no more range available.. means we have the entire file
SURE=`grep "416 R" ${HASH}.log`
#forbidden is when the confirmation above for cookies expires...
FORBIDDEN=`grep Forbid ${HASH}.log`
#if not bad...
if [ "$BAD" == "" ]; then
#and temp file exists...
if [ -f "$TMPFILE" ]; then
#and outfile exists..
if [ -f "${OUTFILE}" ]; then
#remove outfile backup if its there (this was mainly during dev, maybe not needed anymore)
rm -f ${OUTFILE}.backup
#and make backup... (i haven thad any errors since i started checking for BAD/SURE/FORBIDDEN)
mv ${OUTFILE} ${OUTFILE}.backup
fi
#move temp file to real output file
mv ${TMPFILE} ${OUTFILE}
#forbidden can happen after several retries.. so we want to do temp file replacing beore restarting
if [ "$FORBIDDEN" != "" ]; then
#forbidden = restart loop w new cookies
continue
fi
#if we received 416.. its the full file... we can exit
if [ "$SURE" != "" ]; then
echo "completed"
rm -f ${OUTFILE}.backup
rm -f ${HASH}.log
exit
fi
fi
fi
done
@h3ct0r
Copy link

h3ct0r commented Jul 15, 2019

Thanks! It worked for me :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment