Created
January 14, 2019 09:14
-
-
Save pkit/9c6137f8297fdd24ceed95c9515cb11d to your computer and use it in GitHub Desktop.
Download from Google Drive in Linux command line (with curl)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
url="$1" | |
id=$(echo "$url" | grep -oP '(?<=https://drive.google.com/file/d/)[^/]+') | |
if [ -z "$id" ]; then | |
id=$(echo "$url" | grep -oP '(?<=https://drive.google.com/open\?id=)[^/]+') | |
if [ -z "$id" ]; then | |
echo "Cannot parse url: '$url'" | |
exit 1 | |
fi | |
fi | |
# some of my machines do not have jq, that's why using python below | |
#meta=$(curl -L "https://drive.google.com/open?id=$id" | perl -0777 -ne 'print $1 if(m#(_initProjector\([^)]+\))#)' | grep https://drive.google.com/uc | sed 's/^,//;s/$/]/' | jq '{ file: .[1], url: .[18], size: (.[-1]|.[-1])}') | |
meta=$(curl -L "https://drive.google.com/open?id=$id" | perl -0777 -ne 'print $1 if(m#(_initProjector\([^)]+\))#)' | grep https://drive.google.com/uc | sed 's/^,//;s/$/]/' | python -c "import sys, json; data = json.load(sys.stdin); res = {}; res['file'] = data[1]; res['url'] = data[18]; res['size'] = data[-1][-1]; print(json.dumps(res))") | |
file=$(echo "$meta" | grep -oP '(?<="file": ")[^"]+') | |
dl_url=$(echo "$meta" | grep -oP '(?<="url": ")[^"]+') | |
size=$(echo "$meta" | grep -oP '(?<="size": ")[^"]+') | |
echo "Downloading $file ($size bytes) url='$dl_url'" | |
curl -L -c /tmp/cookie-$id.txt -b /tmp/cookie-$id.txt "$dl_url" > "$file" | |
dl_size=$(stat -c '%s' "$file") | |
if [[ "$size" != "$dl_size" ]]; then | |
confirm=$(grep -oP 'confirm=[^\&]+' "$file") | |
echo "Confirming download $file ($size bytes) url='$dl_url&$confirm'" | |
curl -L -c /tmp/cookie-$id.txt -b /tmp/cookie-$id.txt "$dl_url&$confirm" > "$file" | |
dl_size=$(stat -c '%s' "$file") | |
if [[ "$size" != "$dl_size" ]]; then | |
echo "Failed to dl "$file", size does not match" | |
exit 1 | |
fi | |
fi | |
rm -f /tmp/cookie-$id.txt | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment