Skip to content

Instantly share code, notes, and snippets.

@allenyllee
Forked from ppetraki/fetch_gdrive_file.sh
Last active November 10, 2017 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allenyllee/ac42730e9c756e0aeb9b2f07424b68dd to your computer and use it in GitHub Desktop.
Save allenyllee/ac42730e9c756e0aeb9b2f07424b68dd to your computer and use it in GitHub Desktop.
allows you do non-interactively download large public files from gdrive
#!/bin/bash
SOURCE="$1"
if [ "${SOURCE}" == "" ]; then
echo "Must specify a source url"
exit 1
fi
DEST="$2"
#if [ "${DEST}" == "" ]; then
# echo "Must specify a destination filename"
# exit 1
#fi
FILEID=$(echo $SOURCE | rev | cut -d= -f1 | rev)
COOKIES=$(mktemp)
CODE=$(wget --save-cookies $COOKIES --keep-session-cookies --no-check-certificate "https://docs.google.com/uc?export=download&id=${FILEID}" -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/Code: \1\n/p')
# cleanup the code, format is 'Code: XXXX'
CODE=$(echo $CODE | rev | cut -d: -f1 | rev | xargs)
# if no $DEST, auto fetch filename, otherwise use $DEST as filename
if [ -z "$DEST" ]; then
# option --content-disposition to auto fetch filename
wget --content-disposition --load-cookies $COOKIES "https://docs.google.com/uc?export=download&confirm=${CODE}&id=${FILEID}"
else
wget --load-cookies $COOKIES "https://docs.google.com/uc?export=download&confirm=${CODE}&id=${FILEID}" -O $DEST
fi
rm -f $COOKIES
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment