Skip to content

Instantly share code, notes, and snippets.

@shawnli87
Last active April 11, 2024 22:43
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save shawnli87/49122dcc5cf2a65fe4580888347a7946 to your computer and use it in GitHub Desktop.
Save shawnli87/49122dcc5cf2a65fe4580888347a7946 to your computer and use it in GitHub Desktop.
Zsh script to download files from gofile.io
#!/bin/zsh
url="$1"
until [ ! -z "$url" ] ; do
read "url?url="
done
id=$(rev <<< "$url" | cut -d'/' -f1 | rev)
token=$(curl -s 'https://api.gofile.io/createAccount' | jq -r '.data.token' 2>/dev/null)
[ "$?" -ne 0 ] && echo "Creating guest account failed, please try again later"
websiteToken=$(curl -s 'https://gofile.io/dist/js/alljs.js' | grep 'fetchData.wt' | awk '{ print $3 }' | jq -r)
[ "$?" -ne 0 ] && echo "Getting website token failed, please try again later"
resp=$(curl 'https://api.gofile.io/getContent?contentId='"$id"'&token='"$token"'&wt='"$websiteToken"'&cache=true' 2>/dev/null)
code="$?"
if [[ $(jq -r '.status' <<< "$resp" 2>/dev/null) == "error-passwordRequired" ]] ; then
until [ ! -z "$password" ] ; do
read "password?password="
password=$(printf "$password" | sha256sum | cut -d' ' -f1)
resp=$(curl 'https://api.gofile.io/getContent?contentId='"$id"'&token='"$token"'&wt='"$websiteToken"'&cache=true&password='"$password" 2>/dev/null)
code="$?"
done
fi
[ "$code" -ne 0 ] && echo "URL unreachable, check provided link" && exit 1
mkdir "$id" 2>/dev/null
cd "$id"
wget '--header=Cookie: accountToken='"$token" --delete-after "$url" -q -c -T 5 -t 1
[ "$?" -ne 0 ] && echo "Fetching page failed, check provided link"
for i in $(jq '.data.contents | keys | .[]' <<< "$resp"); do
filename=$(jq -r '.data.contents['"$i"'].name' <<< "$resp")
link=$(jq -r '.data.contents['"$i"'].link' <<< "$resp")
if [ ! -f "$filename" ] ; then
wget '--header=Cookie: accountToken='"$token" -O "$filename" "$link" -q --show-progress -c -T 5 -t 1
[ "$?" -ne 0 ] && echo "Downloading ""$filename"" failed, please try again later" && rm "$filename"
fi
done
@Singustromo
Copy link

Singustromo commented May 24, 2023

Why have it like this? Ignoring the continue functionality of wget through the -c parameter.

  if [ ! -f "$filename" ] ; then
    wget '--header=Cookie: accountToken='"$token" -O "$filename" "$link" -q --show-progress -c -T 5 -t 1
    [ "$?" -ne 0 ] && echo "Downloading ""$filename"" failed, please try again later" && rm "$filename"
  fi

I do not see a reason why the file should be deleted or should not exist in the first place.
The continue feature of wget is vastly useful, especially with a slow or unstable connection.
You can also make use of the OR control operator to streamline the logic throughout the script instead of comparing or even saving the return code.

  wget '--header=Cookie: accountToken='"$token" -O "$filename" "$link" -q --show-progress -c -T 5 -t 1 \
    || printf "Downloading "%s" failed, please try again later" "$filename"

Thank you for this script. Saved me a lot of hassle :-)

@shawnli87
Copy link
Author

Why have it like this? Ignoring the continue functionality of wget through the -c parameter.

  if [ ! -f "$filename" ] ; then
    wget '--header=Cookie: accountToken='"$token" -O "$filename" "$link" -q --show-progress -c -T 5 -t 1
    [ "$?" -ne 0 ] && echo "Downloading ""$filename"" failed, please try again later" && rm "$filename"
  fi

I do not see a reason why the file should be deleted or should not exist in the first place. The continue feature of wget is vastly useful, especially with a slow or unstable connection.

I honestly don't recall the reason now, but I remember I had to do that to make the code functional. It was one of those "lesser of two evils" situations.

@lllsssyyy123
Copy link

Hi, how to use this script? ./gofile_dl http://gofile.me/5rpBN/xYrGYLjQQ didn't work, told me: ./gofile_dl:9: command not found: rev

@shawnli87
Copy link
Author

Hi, how to use this script? ./gofile_dl http://gofile.me/5rpBN/xYrGYLjQQ didn't work, told me: ./gofile_dl:9: command not found: rev

This is zsh script, mainly for MacOS. If you need the bash version, use: https://gist.github.com/shawnli87/d416b7c9030293cabfcf4c225cdc5a15

@shawnli87
Copy link
Author

Updated to fix the websiteToken renaming to wt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment