From d2256b18e4a8d82bc30355deaf65117b8eb13a43 Mon Sep 17 00:00:00 2001 From: Jeffrey Serio <23226432+hyperreal64@users.noreply.github.com> Date: Tue, 28 May 2024 16:11:04 -0500 Subject: [PATCH] Whatever. It gets the job done. --- qbth | 226 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 226 insertions(+) create mode 100755 qbth diff --git a/qbth b/qbth new file mode 100755 index 0000000..9430a7b --- /dev/null +++ b/qbth @@ -0,0 +1,226 @@ +#!/usr/bin/env bash + +# qbth - qbittorrent helper + +# Dependency check +missing_deps=() +for bin in "gum" "jq" "lynx" "qbt"; do + if ! test -x "$(command -v $bin)"; then + missing_deps+=("$bin") + fi +done + +if (( "${#missing_deps[@]}" != 0 )); then + echo "Missing dependencies:" "${missing_deps[@]}" + exit 1 +fi + +# Add torrent +function add_torrents() { + torrents=("$@") + + for url in "${torrents[@]}"; do + if qbt torrent add url "$url"; then + gum log -l info -t ansic "Added $(basename "$url")" + else + gum log -l error -t ansic "Error adding $(basename "$url")" + fi + done +} + +function delete_torrents() { + if test -n "$1"; then + echo "$1" | xargs -I _ sh -c "qbt torrent delete --with-files _ && gum log -l info -t ansic 'Deleted' _" + else + gum log -l info -t ansic "Nothing to do." + fi +} + +export -f add_torrents +export -f delete_torrents + +distro_selection=$(gum choose --limit=1 --header="Available torrents" --height=13 "AlmaLinux" "Debian" "Devuan" "Fedora" "FreeBSD" "Kali Linux" "NetBSD" "NixOS" "Qubes" "Rocky Linux" "Tails") +relver=$(gum input --placeholder="Enter $distro_selection release version") +action=$(gum choose --limit=1 --header="Choose an action: Add, Delete" "Add" "Delete") + +case "$distro_selection" in + "AlmaLinux") + case "$action" in + "Add") + urls=( + "https://almalinux-mirror.dal1.hivelocity.net/${relver}/isos/aarch64/AlmaLinux-${relver}-aarch64.torrent" + "https://almalinux-mirror.dal1.hivelocity.net/${relver}/isos/ppc64le/AlmaLinux-${relver}-ppc64le.torrent" + "https://almalinux-mirror.dal1.hivelocity.net/${relver}/isos/s390x/AlmaLinux-${relver}-s390x.torrent" + "https://almalinux-mirror.dal1.hivelocity.net/${relver}/isos/x86_64/AlmaLinux-${relver}-x86_64.torrent" + ) + + add_torrents "${urls[@]}" + ;; + + "Delete") + torrent_hashes=$(qbt torrent list -F csv | awk -v pat="AlmaLinux-$relver" -F, '$0~pat{print $1}') + delete_torrents "$torrent_hashes" + ;; + esac + ;; + + "Debian") + case "$action" in + "Add") + urls=( + "https://cdimage.debian.org/debian-cd/current/amd64/bt-dvd/debian-${relver}-amd64-DVD-1.iso.torrent" + "https://cdimage.debian.org/debian-cd/current/arm64/bt-dvd/debian-${relver}-arm64-DVD-1.iso.torrent" + "https://cdimage.debian.org/debian-cd/current/armel/bt-dvd/debian-${relver}-armel-DVD-1.iso.torrent" + "https://cdimage.debian.org/debian-cd/current/armhf/bt-dvd/debian-${relver}-armhf-DVD-1.iso.torrent" + "https://cdimage.debian.org/debian-cd/current/mips64el/bt-dvd/debian-${relver}-mips64el-DVD-1.iso.torrent" + "https://cdimage.debian.org/debian-cd/current/mipsel/bt-dvd/debian-${relver}-mipsel-DVD-1.iso.torrent" + "https://cdimage.debian.org/debian-cd/current/ppc64el/bt-dvd/debian-${relver}-ppc64el-DVD-1.iso.torrent" + "https://cdimage.debian.org/debian-cd/current/s390x/bt-dvd/debian-${relver}-s390x-DVD-1.iso.torrent" + ) + + add_torrents "${urls[@]}" + ;; + + "Delete") + torrent_hashes=$(qbt torrent list -F csv | awk -v pat="debian-$relver" -F, '$0~pat{print $1}') + delete_torrents "$torrent_hashes" + ;; + esac + ;; + + "Devuan") + case "$action" in + "Add") + urls=("https://files.devuan.org/devuan_${relver}.torrent") + add_torrents "${urls[@]}" + ;; + + "Delete") + torrent_hash=$(qbt torrent list -F csv | awk -v pat="devuan_$relver" -F, '$0~pat{print $1}') + delete_torrents "$torrent_hash" + ;; + esac + ;; + + "Fedora") + case "$action" in + "Add") + mapfile -t urls < <(lynx -dump -listonly=on -nonumbers "https://torrent.fedoraproject.org/" | grep "${relver}.torrent") + add_torrents "${urls[@]}" + ;; + + "Delete") + torrent_hashes=$(qbt torrent list -F json | jq '.[] | select(.name | startswith("Fedora") and endswith("$relver")) | .hash' | tr -d '"') + delete_torrents "$torrent_hashes" + ;; + esac + ;; + + "FreeBSD") + case "$action" in + "Add") + mapfile -t urls < <(curl -sSL "https://people.freebsd.org/~jmg/FreeBSD-${relver}-R-magnet.txt" | grep "magnet:") + add_torrents "${urls[@]}" + ;; + + "Delete") + torrent_hashes=$(qbt torrent list -F csv | awk -v pat="FreeBSD-$relver" -F, '$0~pat{print $1}') + delete_torrents "$torrent_hashes" + ;; + esac + ;; + + "Kali Linux") + case "$action" in + "Add") + mapfile -t urls < <(lynx -dump -listonly=on -nonumbers https://kali.download/base-images/current/ | grep ".torrent") + add_torrents "${urls[@]}" + ;; + + "Delete") + torrent_hashes=$(qbt torrent list -F json | jq '.[] | select(.name | contains("kali")) | .hash' | tr -d '"') + delete_torrents "$torrent_hashes" + ;; + esac + ;; + + "NetBSD") + case "$action" in + "Add") + mapfile -t urls < <(lynx -dump -listonly=on -nonumbers "https://cdn.netbsd.org/pub/NetBSD/NetBSD-${relver}/images/" | grep ".torrent") + add_torrents "${urls[@]}" + ;; + + "Delete") + torrent_hashes=$(qbt torrent list -F csv | awk -v pat="NetBSD-$relver" -F, '$0~pat{print $1}') + delete_torrents "$torrent_hashes" + ;; + esac + ;; + + "NixOS") + case "$action" in + "Add") + mapfile -t urls < <(curl -sSL https://api.github.com/repos/AnimMouse/NixOS-ISO-Torrents/releases/latest | jq '.assets[].browser_download_url' | tr -d '"') + add_torrents "${urls[@]}" + ;; + + "Delete") + torrent_hashes=$(qbt torrent list -F json | jq '.[] | select(.name | contains("nixos")) | .hash' | tr -d '"') + delete_torrents "$torrent_hashes" + ;; + esac + ;; + + "Qubes") + case "$action" in + "Add") + urls=("https://mirrors.edge.kernel.org/qubes/iso/Qubes-R${relver}-x86_64.torrent") + add_torrents "${urls[@]}" + ;; + + "Delete") + torrent_hash=$(qbt torrent list -F csv | awk -v pat="Qubes-R$relver" -F, '$0~pat{print $1}') + delete_torrents "$torrent_hash" + ;; + esac + ;; + + "Rocky Linux") + case "$action" in + "Add") + urls=( + "https://download.rockylinux.org/pub/rocky/${relver}/isos/aarch64/Rocky-${relver}-aarch64-dvd.torrent" + "https://download.rockylinux.org/pub/rocky/${relver}/isos/ppc64le/Rocky-${relver}-ppc64le-dvd.torrent" + "https://download.rockylinux.org/pub/rocky/${relver}/isos/s390x/Rocky-${relver}-s390x-dvd.torrent" + "https://download.rockylinux.org/pub/rocky/${relver}/isos/x86_64/Rocky-${relver}-x86_64-dvd.torrent" + ) + + add_torrents "${urls[@]}" + ;; + + "Delete") + torrent_hashes=$(qbt torrent list -F csv | awk -v pat="Rocky-$relver" -F, '$0~pat{print $1}') + delete_torrents "$torrent_hashes" + ;; + esac + ;; + + "Tails") + case "$action" in + "Add") + urls=( + "https://tails.net/torrents/files/tails-amd64-${relver}.img.torrent" + "https://tails.net/torrents/files/tails-amd64-${relver}.iso.torrent" + ) + + add_torrents "${urls[@]}" + ;; + + "Delete") + torrent_hashes=$(qbt torrent list -F json | jq '.[] | select(.name | contains("tails")) | .hash' | tr -d '"') + delete_torrents "$torrent_hashes" + ;; + esac + ;; +esac