Skip to content

Instantly share code, notes, and snippets.

@mildronize
Last active February 7, 2026 02:11
Show Gist options
  • Select an option

  • Save mildronize/de2fe8609529ddeef42fbe28a85ab963 to your computer and use it in GitHub Desktop.

Select an option

Save mildronize/de2fe8609529ddeef42fbe28a85ab963 to your computer and use it in GitHub Desktop.
aria2 downloader (CivitAI)
#!/usr/bin/env bash
set -euo pipefail
URL="${1:-}"
API_KEY="${2:-}"
OUT_DIR="${3:-.}"
if [ -z "$URL" ]; then
echo "Usage: civitai-aria <download_url> [api_key] [output_dir]"
exit 1
fi
# -----------------------------
# auto install aria2 if missing
# -----------------------------
need_bin() { command -v "$1" >/dev/null 2>&1; }
if ! need_bin aria2c; then
echo "aria2c not found → installing..."
if need_bin apt-get; then
apt-get update -y
apt-get install -y aria2 curl python3 ca-certificates
elif need_bin apk; then
apk add --no-cache aria2 curl python3 ca-certificates
elif need_bin yum; then
yum install -y aria2 curl python3 ca-certificates
else
echo "No supported package manager found. Install aria2 manually."
exit 1
fi
fi
mkdir -p "$OUT_DIR"
echo "Resolving filename..."
# Build curl args safely (no eval)
CURL_ARGS=(-sS -I -L)
if [ -n "$API_KEY" ]; then
CURL_ARGS+=(-H "Authorization: Bearer $API_KEY")
fi
CURL_ARGS+=("$URL")
# Extract last Location header (redirect target)
LOCATION="$(
curl "${CURL_ARGS[@]}" \
| tr -d '\r' \
| awk '/^location:/ {print $2}' \
| tail -n 1
)"
# Extract filename from response-content-disposition inside redirect URL
FILENAME="$(
printf "%s" "$LOCATION" \
| sed -n 's/.*filename%3D%22\([^%]*\).*/\1/p' \
| python3 -c 'import sys,urllib.parse; print(urllib.parse.unquote(sys.stdin.read().strip()))'
)"
if [ -z "$FILENAME" ]; then
MODEL_ID="$(basename "${URL%/}")"
FILENAME="civitai-${MODEL_ID}.bin"
fi
echo "Filename: $FILENAME"
echo "Downloading to: $OUT_DIR/$FILENAME"
DOWNLOAD_URL="$URL"
if [ -n "$API_KEY" ]; then
# match your earlier working approach
DOWNLOAD_URL="${URL}?token=${API_KEY}"
fi
aria2c \
--allow-overwrite=true \
--auto-file-renaming=false \
--continue=true \
--max-connection-per-server=16 \
--split=16 \
--min-split-size=1M \
--file-allocation=none \
--summary-interval=1 \
"$DOWNLOAD_URL" \
-d "$OUT_DIR" \
-o "$FILENAME"
echo "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment