Skip to content

Instantly share code, notes, and snippets.

@eggplants
Last active September 1, 2023 07:42
Show Gist options
  • Save eggplants/f638ba6a6208e4e37f49ccae94cc948e to your computer and use it in GitHub Desktop.
Save eggplants/f638ba6a6208e4e37f49ccae94cc948e to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -euo pipefail
if ! [[ $# == 2 ]]
then
echo "$0 FILE_ID SAVE_PATH" >&2
exit 1
fi
if ! command -v wget &>/dev/null
then
echo "Error: Install wget" >&2
exit 1
fi
file_id="$1"
save_path="$2"
if [[ -z "$file_id" ]]
then
echo "Error: Invalid FILE_ID -- '${file_id}'" >&2
echo "seems to be empty, aight?" >&2
exit 1
fi
if ! [[ -e "$save_path" ]]
then
:
elif [[ -f "$save_path" ]]
then
echo "File already exists. Overwrite?([y]:n)" >&2
read yn
if [[ "$yn" =~ ^n ]]
then
exit 0
fi
else
echo "Error: Invalid SAVE_PATH -- '${save_path}'" >&2
exit 1
fi
u="https://docs.google.com/uc?export=download&id=${file_id}"
cs="$(
wget \
--quiet \
--keep-session-cookies \
--no-check-certificate \
--save-cookies /tmp/cookies.txt "$u" -O- |
sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p'
)"
wget --load-cookies /tmp/cookies.txt "${u}&confirm=${cs}" -O "$save_path"
echo "[DONE]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment