#!/usr/bin/env bash # SPDX-License-Identifier: MIT # Copyright (c) 2026 Frost set -u if [ "$#" -eq 0 ]; then echo "Usage: get [url2 ...]" exit 1 fi download_to_file() { local url="$1" local outfile="$2" local tmpfile tmpfile="$(mktemp)" || return 1 if wget -q -O "$tmpfile" "$url"; then mv -f "$tmpfile" "$outfile" else rm -f "$tmpfile" return 1 fi } get_konachan_post_id() { local url="$1" if [[ "$url" =~ %20-%20([0-9]+)%20 ]]; then printf '%s\n' "${BASH_REMATCH[1]}" return 0 fi return 1 } get_danbooru_post_id() { local md5="$1" local id id="$(wget -qO- "https://danbooru.donmai.us/posts.json?tags=md5:${md5}&limit=1" \ | jq -r '.[0].id // empty')" || return 1 [ -n "$id" ] && printf '%s\n' "$id" } process_url() { local url="$1" local clean_url="${url%%\?*}" if [[ "$clean_url" =~ konachan\.(com|net) ]]; then local download_url="${clean_url/konachan.com/konachan.net}" local filename="${clean_url##*/}" local ext="${filename##*.}" local post_id final_file if post_id="$(get_konachan_post_id "$clean_url")"; then final_file="${post_id}k.${ext}" printf "Downloading: %s ... " "$final_file" if download_to_file "$download_url" "$final_file"; then echo "[OK]" else echo "[FAILED]" fi else echo "Could not extract Konachan post ID: $url" fi elif [[ "$clean_url" =~ (danbooru\.donmai\.us|cdn\.donmai\.us) ]]; then local filename="${clean_url##*/}" local ext="${filename##*.}" local md5 post_id final_file if [[ "$clean_url" =~ ([0-9a-f]{32})\.[A-Za-z0-9]+$ ]]; then md5="${BASH_REMATCH[1]}" if post_id="$(get_danbooru_post_id "$md5")"; then final_file="${post_id}d.${ext}" printf "Downloading: %s ... " "$final_file" if download_to_file "$clean_url" "$final_file"; then echo "[OK]" else echo "[FAILED]" fi else echo "Could not find Danbooru ID for MD5: $md5" fi else echo "Could not extract MD5: $url" fi else printf "Downloading: %s ... " "$url" if wget -q "$url"; then echo "[OK]" else echo "[FAILED]" fi fi } # ---------------------- # PARALLEL CONTROL # ---------------------- max_jobs=$(( $# < 5 ? $# : 5 )) pids=() outputs=() for url in "$@"; do tmp_out="$(mktemp)" outputs+=("$tmp_out") ( process_url "$url" ) >"$tmp_out" 2>&1 & pids+=($!) # limit parallel jobs if (( ${#pids[@]} >= max_jobs )); then wait "${pids[0]}" cat "${outputs[0]}" rm -f "${outputs[0]}" pids=("${pids[@]:1}") outputs=("${outputs[@]:1}") fi done # wait remaining for I in "${!pids[@]}"; do wait "${pids[$I]}" cat "${outputs[$I]}" rm -f "${outputs[$I]}" done