#!/bin/bash
chan_thread=$1;
# Get domain of the site (8chan.moe, 8chan.se, etc)
chan_domain=$(echo $chan_thread | cut -d"/" -f1-3);
# Download the thread's html and embedded elements
wget --no-cookies --header 'Cookie: TOS=1' --referer "$chan_domain" -N --convert-links --page-requisites $chan_thread;
# Make the thread locally accessible
local_thread=$(echo $chan_thread | cut -d"/" -f3-);
# Download the files uploaded to the thread
wget --no-cookies --header 'Cookie: TOS=1' --referer "$chan_domain" -N --mirror $(grep -o -i -P 'href=".+/.media/[a-f0-9]{64}(\.[^"]+)?' $local_thread | sed 's/href="//g' | sort -u);
# Make the html point to the files saved with the previous command
sed -i 's/\(href="\).\+\(\/.media\/[a-f0-9]\{64\}\(\.[^"]\+\)\?\)/\1..\/..\2/g' $local_thread;