admin-scripts/bin/seed_armbian_torrents.py

64 lines
1.9 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2024-07-29 08:48:44 +02:00
"""seed_armbian_torrents.py
2024-07-24 03:02:06 +02:00
Description:
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 or used in a systemd
timer.
This is a Python implementation of https://docs.armbian.com/Community_Torrent/
for qBittorrent.
Usage:
2024-07-29 08:48:44 +02:00
seed_armbian_torrents.py (HOSTNAME) (USERNAME) (PASSWORD)
seed_armbian_torrents.py -h
2024-07-24 03:02:06 +02:00
Examples:
2024-07-29 08:48:44 +02:00
seed_armbian_torrents.py "http://localhost:8080" "admin" "adminadmin"
seed_armbian_torrents.py "https://cat.seedhost.eu/lol/qbittorrent" "lol" "pw"
2024-07-24 03:02:06 +02:00
Options:
-h, --help show this help message and exit.
"""
import os
from io import BytesIO
from tempfile import TemporaryDirectory
from zipfile import ZipFile
import requests
2024-07-24 03:02:06 +02:00
from docopt import docopt
from qbittorrent import Client
if __name__ == "__main__":
2024-07-24 03:02:06 +02:00
args = docopt(__doc__)
2024-07-24 03:02:06 +02:00
# Initialize client and login
qb = Client(args["HOSTNAME"])
qb.login(username=args["USERNAME"], password=args["PASSWORD"])
with TemporaryDirectory() as tmp_dir:
req = requests.get(
2024-07-24 03:02:06 +02:00
"https://dl.armbian.com/torrent/all-torrents.zip",
stream=True,
timeout=60,
)
2024-07-24 03:02:06 +02:00
with ZipFile(BytesIO(req.content)) as zip_file:
zip_file.extractall(tmp_dir)
2024-07-28 04:23:03 +02:00
for torrent in qb.torrents():
2024-07-27 18:33:18 +02:00
if "Armbian" in torrent.get("name"): # type: ignore
qb.delete_permanently(torrent.get("hash")) # type: ignore
print(f"Removed {torrent.get('name')}") # type: ignore
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")
2024-07-24 03:02:06 +02:00
print(
f"Added {os.path.basename(os.path.join(tmp_dir, torrent_file))}"
)