Rewrite seed-armbian-torrents in Python

This commit is contained in:
Jeffrey Serio 2024-07-08 23:37:10 -05:00
parent 99378945d5
commit e98a8379ae
2 changed files with 79 additions and 38 deletions

View File

@ -0,0 +1,50 @@
#!/usr/bin/env bash
#
# Armbian torrents seed script
#
# This script will download Armbian torrent files and add them to a qBittorrent
# instance. It's intended to be run under /etc/cron.weekly.
#
# Based on https://docs.armbian.com/Community_Torrent/
set -euo pipefail
# Dependency check
missing_deps=()
for bin in "wget" "unzip" "jq" "parallel" "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
fi
TEMP_DIR=$(mktemp -d || echo "Failed to make temp dir; exit 1")
chmod 700 "${TEMP_DIR}"
trap 'rm -rf "${TEMP_DIR}" ; exit 0' 0 1 2 3 15
wget -qO- -O "${TEMP_DIR}/armbian-torrents.zip" https://dl.armbian.com/torrent/all-torrents.zip
# Test zip file for corruption
if ! unzip -t "${TEMP_DIR}/armbian-torrents.zip" >/dev/null 2>&1; then
echo "Error in armbian-torrents.zip"
exit
fi
# Extract torrent files from zip
unzip -o "${TEMP_DIR}/armbian-torrents.zip" -d "${TEMP_DIR}/torrent-tmp" >/dev/null 2>&1
# Get list of Armbian torrents active on qBittorrent instance
active_torrents=$(qbt torrent list -F json | jq '.[].hash' | tr -d '"')
# Remove active old Armbian torrents to make room for new/updated ones
echo "$active_torrents" | parallel 'qbt torrent delete -f {}'
# Add new/updated Armbian torrent files
new_torrents=$(find "${TEMP_DIR}/torrent-tmp" -type f -name "*.torrent")
echo "$new_torrents" | parallel 'qbt torrent add file {}'
exit 0

View File

@ -1,50 +1,41 @@
#!/usr/bin/env bash #!/usr/bin/env python3
# #
# Armbian torrents seed script # Armbian torrents seed script
# #
# This script will download Armbian torrent files and add them to a qBittorrent # This script will download Armbian torrent files and add them to a qBittorrent
# instance. It's intended to be run under /etc/cron.weekly. # instance. It's intended to be run under /etc/cron.weekly or used in a systemd
# timer.
# #
# Based on https://docs.armbian.com/Community_Torrent/ # Python implementation of https://docs.armbian.com/Community_Torrent/
set -euo pipefail import os
from io import BytesIO
from tempfile import TemporaryDirectory
from zipfile import ZipFile
# Dependency check import requests
missing_deps=() from qbittorrent import Client
for bin in "wget" "unzip" "jq" "parallel" "qbt"; do
if ! test -x "$(command -v $bin)"; then
missing_deps+=("$bin")
fi
done
if (( "${#missing_deps[@]}" != 0 )); then if __name__ == "__main__":
echo "Missing dependencies:" "${missing_deps[@]}"
exit
fi
TEMP_DIR=$(mktemp -d || echo "Failed to make temp dir; exit 1") qb = Client("http://10.0.0.81:8080/")
chmod 700 "${TEMP_DIR}" qb.login()
trap 'rm -rf "${TEMP_DIR}" ; exit 0' 0 1 2 3 15
wget -qO- -O "${TEMP_DIR}/armbian-torrents.zip" https://dl.armbian.com/torrent/all-torrents.zip with TemporaryDirectory() as tmp_dir:
req = requests.get(
"https://dl.armbian.com/torrent/all-torrents.zip", stream=True
)
zip_file = ZipFile(BytesIO(req.content))
zip_file.extractall(tmp_dir)
# Test zip file for corruption torrents = qb.torrents()
if ! unzip -t "${TEMP_DIR}/armbian-torrents.zip" >/dev/null 2>&1; then for torrent in torrents:
echo "Error in armbian-torrents.zip" if "Armbian" in torrent["name"]:
exit qb.delete_permanently(torrent["hash"])
fi print(f"Removed {torrent['name']}")
# Extract torrent files from zip torrent_files = os.listdir(tmp_dir)
unzip -o "${TEMP_DIR}/armbian-torrents.zip" -d "${TEMP_DIR}/torrent-tmp" >/dev/null 2>&1 for torrent_file in torrent_files:
with open(os.path.join(tmp_dir, torrent_file), "rb") as tf:
# Get list of Armbian torrents active on qBittorrent instance qb.download_from_file(tf, category="distro")
active_torrents=$(qbt torrent list -F json | jq '.[].hash' | tr -d '"') print(f"Added {os.path.basename(os.path.join(tmp_dir, torrent_file))}")
# Remove active old Armbian torrents to make room for new/updated ones
echo "$active_torrents" | parallel 'qbt torrent delete -f {}'
# Add new/updated Armbian torrent files
new_torrents=$(find "${TEMP_DIR}/torrent-tmp" -type f -name "*.torrent")
echo "$new_torrents" | parallel 'qbt torrent add file {}'
exit 0