8chan file downloader Bash
#!/bin/bash args=$* fileNames="-" range="false" rangeStart="" rangeEnd="" useTor="false" imageIndex="0" invalidArgs="" skipPrompt="false" function download { if [[ "$useTor" == "true" ]]; then var=$(echo $* | sed "s#https://#http://#g" | sed "s#media.8ch.net#oxwugzccvk3dk6tj.onion#g" | sed "s#8ch.net#oxwugzccvk3dk6tj.onion#g") torsocks wget $var else var=$(echo $* | sed "s#http://#https://#g" | sed "s#oxwugzccvk3dk6tj.onion#8ch.net#g") wget $var fi } function endScript { echo "$*" rm *.html exit 1 } while read a; do if [[ "$a" == "-"* ]]; then if [[ "$a" == "-h" ]] || [[ "$a" == "--help" ]]; then echo " -b[board]:[thread] - Board and thread used to download files." echo " -n - Download files using numbered names. (1.jpg, 2.jpg, ...)" echo " -o - Download files using their original filename. (overrides -n)" echo " -r[begin]:[end] - Range of files to be downloaded." echo " -t - Download files using 8chan's Tor site." echo " -u[url] - Url of 8chan thread to download. Uses the Tor url if needed. (overrides -b)" echo " -y - Assumes yes to download prompt." echo " -h or --help - Displays this help menu." exit 0 elif [[ "$a" == "-b"* ]]; then arg=$(echo "$a::" | sed "s#-b##") board=$(echo $arg | cut -d':' -f1) thread=$(echo $arg | cut -d':' -f2 | cut -d'.' -f1) if [[ -n "$board" ]] && [[ -n "$thread" ]]; then url="https://8ch.net/$board/res/$thread.html" fi elif [[ "$a" == "-n" ]]; then fileNames="numbered" elif [[ "$a" == "-o" ]]; then fileNames="original"; elif [[ "$a" == "-r"* ]]; then arg=$(echo "$a::" | sed "s#-r##") rangeStart=$(echo "$arg" | cut -d':' -f1) rangeEnd=$(echo "$arg" | cut -d':' -f2) if [[ -n "$rangeStart" ]] || [[ -n "$rangeEnd" ]]; then range="true" fi elif [[ "$a" == "-t" ]]; then useTor="true" elif [[ "$a" == "-u"* ]]; then arg=$(echo "$a" | sed "s#-u##") var=$(printf "$arg" | grep -o "onion") [[ -n "$var" ]] && useTor="true" thread=$(echo $arg | sed "s#http://#https://#g" | sed "s#oxwugzccvk3dk6tj.onion#8ch.net#g") if [[ $thread == *"://8ch.net"* ]]; then url="$thread" elif [[ $thread == "8ch.net"* ]]; then url=https://$thread else echo "Invalid thread: $thread" exit 0 fi board=$(echo $url | cut -d'/' -f4) thread=$(echo $url | cut -d'/' -f6 | cut -d'.' -f1) elif [[ "$a" == "-y" ]]; then skipPrompt="true" else invalidArgs="$invalidArgs $a" fi fi done < <(echo $args | tr ' ' '\n') args=$(echo "$args" | sed "s#--help##g" | sed "s#-h##g") if [[ -n "$invalidArgs" ]]; then echo "Invalid arguments:$invalidArgs" echo "Use --help to show valid arguments." exit 0 fi if [[ -z "$url" ]]; then read -p "Enter 8chan thread: " thread if [[ $thread =~ ^-?[0-9]+$ ]]; then read -p "Enter board: " board url="https://8ch.net/$board/res/$thread.html" else var=$(printf "$thread" | grep -o "onion") [[ -n "$var" ]] && useTor="true" thread=$(echo $thread | sed "s#http://#https://#g" | sed "s#oxwugzccvk3dk6tj.onion#8ch.net#g") if [[ $thread == *"://8ch.net"* ]]; then url=$thread elif [[ $thread == "8ch.net"* ]]; then url=https://$thread else echo Invalid thread. exit 0 fi board=$(echo $url | cut -d'/' -f4) thread=$(echo $url | cut -d'/' -f6 | cut -d'.' -f1) fi fi if [[ -z "$args" ]]; then read -p "Download using Tor (y/n)? " choice case "$choice" in y|Y|yes|Yes ) useTor="true";; esac if [[ "$fileNames" == "-" ]]; then read -p "Save files with original names (y/n)? " choice case "$choice" in y|Y|yes|Yes ) fileNames="original";; esac fi if [[ "$fileNames" == "-" ]]; then read -p "Save files with numbered names [1.jpg, 2.jpg, ...] (y/n)? " choice case "$choice" in y|Y|yes|Yes ) fileNames="numbered";; esac fi read -p "Save files within a given range (y/n)? " choice case "$choice" in y|Y|yes|Yes ) range="true";; esac if [[ "$range" == "true" ]]; then read -p "Enter starting image (you may leave this blank): " rangeStart read -p "Enter ending image (you may leave this blank): " rangeEnd fi fi if [[ "$useTor" == "true" ]] && [[ -z "$(which torsocks)" ]]; then read -p "Torsocks not found. Download without Tor? (y/n)? " choice case "$choice" in n|N|no|No ) exit 0;; esac useTor="false" fi if [[ "$skipPrompt" == "false" ]]; then var=$url [[ "$useTor" == "true" ]] && var=$(echo $var | sed "s#https://8ch.net/#http://oxwugzccvk3dk6tj.onion/#g") echo "" echo "Downloading files from: $var" [[ "$fileNames" != "-" ]] && echo "Save filenames as: $fileNames" [[ "$range" == "true" ]] && echo "Range: '$rangeStart' - '$rangeEnd'" read -p "Is this correct (y/n)? " choice case "$choice" in n|N|no|No ) exit 0;; esac fi threadDir="$board-$thread" [[ "$range" == "true" ]] && threadDir=$threadDir"_"$rangeStart"-"$rangeEnd [ -d "$threadDir" ] || mkdir "$threadDir" || endScript "Unable to create directory." cd "$threadDir" download --content-on-error "$url" -O "index.html" checkCF=$(cat index.html | grep -o "Please complete the security check to access") [[ ! $checkCF =~ "o" ]] || endScript "Blocked by CloudFlare." oldIFS=$IFS IFS=$'\n' cat index.html | sed "s#File: <a#>\n#g" | sed "s#</p>#>\n#g" | grep postfilename > posts.html imgArray=( $(cat posts.html | cut -d'"' -f2)) nameArray=( $(cat posts.html | cut -d'>' -f5 | cut -d'<' -f1)) count=1 withinRange="false" endOfRange="false" for index in $(seq 0 $((${#imgArray[@]} - 1))); do img="${imgArray[$index]}" [[ -z "$img" ]] && continue [[ "$img" == "/"* ]] && img="https://8ch.net$img" if [[ "$range" == "true" ]]; then imgCompare=${img##*/} [[ -z "$rangeStart" ]] && withinRange="true" if [[ "$withinRange" == "true" ]] && [[ -n "$rangeEnd" ]]; then if [[ $imgCompare == *"$rangeEnd"* ]]; then endOfRange="true" else [[ "$endOfRange" == "true" ]] && break fi fi if [[ "$withinRange" == "false" ]]; then if [[ $imgCompare == *"$rangeStart"* ]]; then withinRange="true" else continue fi fi fi if [[ "$fileNames" == "original" ]]; then name=${nameArray[$index]} name=$(echo $name | tr -d '\r\n') download "$img" # "name" causes problems for wget if it has spaces # this is the best workaround for now that still allows wget commands to be passed on file=$(basename $img) mv -f "$file" "$name" elif [[ "$fileNames" == "numbered" ]]; then ext="${img##*.}" name=$count.$ext download "$img" -O "$name" else download "$img" fi (( count++ )) done rm *.html