Last active
September 4, 2023 18:02
-
-
Save wernerb/7864141 to your computer and use it in GitHub Desktop.
Sync folder with Google Drive. Recursively downloads with wget and Google Drive public links. Used to sync ebooks to my eReaders (Kobo). Be warned that public links are dangerous as they could allow someone to download your files. Be sure to use this only in secure environments. This has been tested to be used in POSIX shells as well as in bash.
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/sh | |
#input sharelink here. Example: https://drive.google.com/folderview?id=0B1g-MbiD2F6vdtOT92b3MoerO&usp=sharing | |
SHARELINK="https://drive.google.com/folderview?id=idU&usp=sharing" | |
DESTINATION="/full/path/to/folder" | |
# Change following to false when you don't want to delete files when they are missing from google drive. This can | |
REMOVEFILES=true | |
# Begin code | |
download () { | |
url=$1 | |
folder=$2 | |
changed=$3 | |
wgetoutput="`wget --no-check-certificate -qO- $url 2>&1`" | |
mkdir -p "$folder" | |
commands="$(echo "$wgetoutput" | grep -Eo '\[,,\"(.*?)\",,,,,\"(.*?)\"')" | |
missingfiles="$(find "$folder" -type f -depth 1 -print)" | |
missingdirectories="$(find "$folder" -type d -depth 1 -print)" | |
#Go to next | |
subfolders="$(echo "$wgetoutput" | grep -Eo '\[,".*?\",\".*?folder' | cut -c 4- | cut -d\" -f 1)" | |
IFS=" | |
" | |
for subfolder in $subfolders; do | |
name=$(echo "$wgetoutput" | grep -Eo 'entry-'$subfolder'.*?</div></div><div class=\"flip-entry-title\">(.*?)</div>' | grep -Eo 'entry-title">.*?</div>$' | cut -c 14- | sed 's/<\/div>//') | |
newdest="$2/$name" | |
escapedfile="$(echo "$newdest" | sed -e 's/[]\$*.^|[]/\\&/g')" | |
missingdirectories=$(echo "$missingdirectories" | sed -e "s@$escapedfile@@g" | sed '/^$/d') | |
output=$(download "https://drive.google.com/folderview?id=$subfolder&usp=sharing" "$newdest" $changed) | |
changed=$output | |
done | |
#Download files | |
echo "Downloading for $folder..." 1>&2; | |
for line in $commands; do | |
id=$(echo "$line" | grep -Eio '\"[0-9a-z]+-[0-9a-z]+\"' | cut -c 2- | sed s'/.$//') | |
name=$(echo "$line" | sed -e 's/\[,,\"//' | sed s/\".*//'') | |
file="$folder/$name" | |
cmd=$(echo "wget --no-check-certificate -nc -q --no-cookies -O \"$file\" \"https://docs.google.com/uc?export=download&id="$id"\"") | |
eval "$cmd" | |
commandresult=$? | |
if [ "$commandresult" -eq 0 ]; then | |
echo "Downloaded: $file" 1>&2; | |
changed=$(($changed + 1)) | |
else | |
echo "Skipped (Already exists or error): $file" 1>&2; | |
fi | |
escapedfile=$(echo "$file" | sed -e 's/[]@$*.^|[]/\\&/g') | |
missingfiles=$(echo "$missingfiles" | sed 's@'$escapedfile'@@g' | sed '/^$/d') | |
done | |
echo "Finished downloading!" 1>&2; | |
#Remove missing files | |
if [ $REMOVEFILES = true ]; then | |
echo "Checking files to remove from $folder..." 1>&2; | |
i=0 | |
for line in $missingfiles; do | |
echo "Removing file: $line" 1>&2; | |
rm -f "$line" | |
changed=$(($changed + 1)) | |
done | |
for line in $missingdirectories; do | |
echo "Removing directory: $line" 1>&2; | |
rm -rf "$line" | |
changed=$(($changed + 1)) | |
done | |
echo "Finished scan!" 1>&2; | |
fi | |
echo $changed | |
} | |
result=$(download "$SHARELINK" "$DESTINATION" 0) | |
echo "Changed: $result files/folders" | |
if [ $result -ne 0 ]; then | |
echo "Sync made changes to disk!" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This error I could fix moving
-depth 1
to before-type
. Now, I'm got a errorfind: paths must precede expression: '1'
.