download.sh 1.09 KB
Newer Older
mikhailnov's avatar
mikhailnov committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#!/usr/bin/env bash
# Quick, dirty ang ugly script to download from knigavuhe.com

set -e

input="$1"
input="${input:-"$(pwd)/input.txt"}"
download_dir="${download_dir:-"$(pwd)/DOWNLOADED"}"

cat_list(){
	cat "$input" | sed -e 's,\",\n,g' | grep '\.mp3$' | sed -e 's,\\,,g'
}
 
mk_unique_list(){
	cat_list | awk -F '/' '{print $(NF-1)}' | sort -u
}

mk_dirs(){
	while read -r line
	do
		if [ ! -d "${download_dir}/${line}" ]; then
			mkdir -p "${download_dir}/${line}"
		fi
	done < <(mk_unique_list)
}

download(){
	pushd "$download_dir"
	while read -r line
	do
		if [ ! -f "$line" ]; then
			axel -n 5 "$line"
		fi
	done < <(cat_list)
	popd
}

parallel_download(){
	pushd "$download_dir"
	while read -r unique_dir
	do
		pushd "$unique_dir"
		parallel -a <(cat_list | grep "/${unique_dir}/") -P 16 wget "{1}"
		popd
	done < <(mk_unique_list)
	popd
}

mv_to_dirs(){
	pushd "$download_dir"
	while read -r unique
	do
		while read -r line1
		do
			mv -v "$line1" "${download_dir}/${unique}"
		done < <(find . -maxdepth 1 -type f -iname "*${unique}*")
	done < <(mk_unique_list)
	popd
}

mk_dirs
#download
parallel_download