#!/bin/bash set -f # No globbing set -C # No clobbering set -e set -o pipefail printurls= if [[ "$1" == '--urls' ]] then printurls=1 shift fi dodownload=1 if [[ "$1" == '--nodl' ]] then dodownload= shift fi function printurl { if [[ "${printurls}" ]]; then echo "$1"; fi } if [[ ( $# -ne 1 && $# -ne 2 ) || ( ! "$1" =~ ^https://gofile\.io/d/[0-9a-zA-Z]+$ && ! "$1" =~ ^https://gofile\.io/\?c=[0-9a-zA-Z]+$ ) ]] then echo 'Usage: gofile.io-dl [--urls] [--nodl] URL [PASSWORD]' >&2 exit 1 fi url="$1" password="$2" printurl "$url" if [[ "${url}" == *'?c='* ]] then code="${url##*=}" else code="${url##*/}" fi folderUrl="https://api.gofile.io/getFolder?folderId=${code}" printurl "${folderUrl}" if [[ "${password}" ]] then password="$(echo -n "${password}" | sha256sum | sed 's,\s.*$,,')" folderUrl="${folderUrl}&password=${password}&cache=true" printurl "${folderUrl}" fi curl -s --fail "${folderUrl}" | python3 -c 'import json,sys; obj = json.loads(sys.stdin.read().strip())'$'\n''for f in obj["data"]["contents"].values():'$'\n'' print(f["size"], f["md5"], f["name"], f["link"])' | \ while read -r size md5 name link do if [[ "${name}" == *'/'* || "${link}" == *' '* || ! "${link}" =~ ^https://((srv-)?file[0-9]+|srv-store[0-9]+)\.gofile\.io/download/ ]] then echo 'Cannot download file:' >&2 echo "name: ${name}" >&2 echo "link: ${link}" >&2 echo "size: ${size}" >&2 echo "md5: ${md5}" >&2 exit 1 fi printurl "${link}" if [[ -z "${dodownload}" ]] then continue fi if [[ -e "./${name}" ]] then echo "./${name} already exists" >&2 exit 1 fi echo "Downloading ${link} to ./${name}..." >&2 curl "${link}" >"./${name}" declare -i actualSize=$(stat -c %s "./${name}") if [[ ${actualSize} -ne ${size} ]] then echo "Size mismatch: expected ${size}, got ${actualSize}" >&2 exit 1 fi md5sum -c <<<"${md5} ./${name}" done