Update fetch_torrents.py

This commit is contained in:
Kasper Plougmann 2025-07-10 12:44:14 +02:00 committed by GitHub
parent 58ce93eb37
commit b605bfd9b8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -7,6 +7,7 @@ import logging
import time
import shutil
from bs4 import BeautifulSoup
from transmission_rpc import Client
# Configure logging
log_file = "/logs/fetch_torrents.log"
@ -146,9 +147,7 @@ def fetch_kali_latest():
logging.error("Kali fetch error: %s", exc)
return False
def log_seed_ratios_via_http(rpc_url="http://localhost:9091/transmission/rpc",
auth: tuple | None = None):
def log_seed_ratios_via_http(rpc_url="http://localhost:9091/transmission/rpc", auth: tuple | None = None):
r = requests.post(rpc_url)
headers = {"X-Transmission-Session-Id": r.headers["X-Transmission-Session-Id"]}
payload = {
@ -160,6 +159,30 @@ def log_seed_ratios_via_http(rpc_url="http://localhost:9091/transmission/rpc",
for t in r.json()["arguments"]["torrents"]:
logging.info("[ratio] %s%.3f", t["name"], t["uploadRatio"])
# Example: find all torrents for a distro, keep only the latest
def cleanup_old_versions():
tc = Client(host='localhost', port=9091)
torrents = tc.get_torrents()
# Collect all torrents matching the distro prefix
matched = []
version_re = re.compile(rf"(\d+\.\d+)\.iso", re.IGNORECASE)
for torrent in torrents:
m = version_re.search(torrent.name)
if m:
matched.append((torrent, m.group(1)))
if not matched:
return
# Sort by version number, keep the latest
def version_key(t):
# Convert version like '1.10' to tuple (1, 10)
return tuple(map(int, t[1].split('.')))
matched.sort(key=version_key, reverse=True)
# Keep the first (latest), remove the rest
for torrent, version in matched[1:]:
logging.info(f"Removing old version: {torrent.name}")
tc.remove_torrent(torrent.id, delete_data=True)
if __name__ == "__main__":
start_time = time.time()
logging.info("Starting torrent fetch run.")