Created
January 28, 2016 14:00
-
-
Save SunRed/6adb6f367c2e9d8ce858 to your computer and use it in GitHub Desktop.
This script easily downloads public files from Google Drive onto your disk using a file identifier.
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 | |
########### | |
# Download public files from Google Drive using file identifier | |
if [ $# -lt 1 ]; then | |
echo "No argument defined. Usage: $0 fileID [destination]" >&2 | |
echo "Try '$0 --help' for more information." | |
exit 1 | |
elif [ $# -gt 2 ]; then | |
echo "Too many arguments. Usage: $0 fileID [destination]" >&2 | |
echo "Try '$0 --help' for more information." | |
exit 1 | |
elif [ "$1" = '--help' ]; then | |
echo -e "$(tput setaf 3)GDrive Download-Tool v1.0$(tput sgr0)" | |
echo -e "This script easily downloads $(tput smul)public files$(tput rmul) from Google Drive onto your disk." | |
echo -e "Downloaded files will be stored to the directory where this script is placed in if no second argument is given." | |
echo -e "\nOnly the file identifier is needed of the file you want to download and to be appended to the script name on your command line." | |
echo -e "Therefore the usage is as the following: $(tput bold)$0 $(tput setaf 3)fileID$(tput sgr0) [destination]" | |
exit 0 | |
elif [[ "$1" =~ [^0-9A-Za-z]+ ]]; then | |
echo "Invalid characters found in fileID" | |
exit 1 | |
fi | |
# Continue if no errors occurred | |
if [ -z "$2" ]; then | |
directory="$(dirname $0)" | |
else | |
directory="$2" | |
fi | |
directory="${directory%/}" | |
if [ ! -d "$directory" ]; then | |
echo "'$directory' is not a valid directory" | |
exit 1 | |
fi | |
filename="$(wget --quiet -O - https://docs.google.com/file/d/$1/edit?usp=sharing | sed -n -e 's!.*<title>\(.*\) - Google Drive</title>.*!\1!p')" | |
if [ -n "$filename" ]; then | |
file="$directory/$filename" | |
if [ ! -f "$file" ]; then | |
wget https://googledrive.com/host/$1 -O "$file" | |
else | |
echo "File '$file' already exists on disk. Aborting..." | |
exit 1 | |
fi | |
else | |
echo "Given fileID '$1' does not point to a valid file. Aborting..." | |
exit 1 | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Created this script quite a while ago to download a backup I stored on Google Drive.