mirror of
https://codeberg.org/hyperreal/admin-scripts
synced 2024-11-01 16:03:06 +01:00
Rewrite seed-armbian-torrents in Python
This commit is contained in:
parent
99378945d5
commit
e98a8379ae
50
bin/.archived/seed-armbian-torrents.bash
Executable file
50
bin/.archived/seed-armbian-torrents.bash
Executable 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
|
@ -1,50 +1,41 @@
|
||||
#!/usr/bin/env bash
|
||||
#!/usr/bin/env python3
|
||||
#
|
||||
# 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.
|
||||
# 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
|
||||
missing_deps=()
|
||||
for bin in "wget" "unzip" "jq" "parallel" "qbt"; do
|
||||
if ! test -x "$(command -v $bin)"; then
|
||||
missing_deps+=("$bin")
|
||||
fi
|
||||
done
|
||||
import requests
|
||||
from qbittorrent import Client
|
||||
|
||||
if (( "${#missing_deps[@]}" != 0 )); then
|
||||
echo "Missing dependencies:" "${missing_deps[@]}"
|
||||
exit
|
||||
fi
|
||||
if __name__ == "__main__":
|
||||
|
||||
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
|
||||
qb = Client("http://10.0.0.81:8080/")
|
||||
qb.login()
|
||||
|
||||
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
|
||||
if ! unzip -t "${TEMP_DIR}/armbian-torrents.zip" >/dev/null 2>&1; then
|
||||
echo "Error in armbian-torrents.zip"
|
||||
exit
|
||||
fi
|
||||
torrents = qb.torrents()
|
||||
for torrent in torrents:
|
||||
if "Armbian" in torrent["name"]:
|
||||
qb.delete_permanently(torrent["hash"])
|
||||
print(f"Removed {torrent['name']}")
|
||||
|
||||
# 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
|
||||
torrent_files = os.listdir(tmp_dir)
|
||||
for torrent_file in torrent_files:
|
||||
with open(os.path.join(tmp_dir, torrent_file), "rb") as tf:
|
||||
qb.download_from_file(tf, category="distro")
|
||||
print(f"Added {os.path.basename(os.path.join(tmp_dir, torrent_file))}")
|
||||
|
Loading…
Reference in New Issue
Block a user