Skip to content

Instantly share code, notes, and snippets.

@ryul99
Last active August 26, 2024 09:28
Show Gist options
  • Save ryul99/b51e939a6a459b4de60b361c6ab03818 to your computer and use it in GitHub Desktop.
Save ryul99/b51e939a6a459b4de60b361c6ab03818 to your computer and use it in GitHub Desktop.
google drive downloader with wget and pure shell script
#!/bin/bash
# acknowledgement:
# https://chemicloud.com/blog/download-google-drive-files-using-wget/#how-to-download-google-drive-files-using-wget
# https://stackoverflow.com/a/6174447
function parse-url() {
# extract the protocol
proto="$(echo $1 | grep :// | sed -e's,^\(.*://\).*,\1,g')"
# remove the protocol
url="$(echo ${1/$proto/})"
# extract the path (if any)
file_id="$(echo $url | grep / | cut -d/ -f4)"
echo $file_id
}
FILENAME=$2
if [[ -z $FILENAME ]]; then
echo "File name is not set. Use 'google_file' as name"
FILENAME='google_file'
fi
FILEID=$(parse-url $1)
CONFIRM=$(wget --quiet --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate "https://docs.google.com/uc?export=download&id=$FILEID" -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')
wget --load-cookies /tmp/cookies.txt \
"https://docs.google.com/uc?export=download&confirm=$CONFIRM&id=$FILEID" \
-O $FILENAME && rm -rf /tmp/cookies.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment