# ============================== # 🧹 HANDLE COMPLETED + DEAD # ============================== def handle_completed_and_dead(): global stat_completed, stat_dead # Načteme info o torrentech torrents = qb.torrents_info() for t in torrents: t_hash = t.hash state = t.state progress = float(t.progress) # Získání dostupnosti (availability) - defaultně -1 pokud není k dispozici availability = float(getattr(t, "availability", -1)) # Získání času přidání added_ts = getattr(t, "added_on", 0) added_dt = datetime.fromtimestamp(added_ts) if added_ts > 0 else datetime.now() age_in_minutes = (datetime.now() - added_dt).total_seconds() / 60 # --------------------------- # 1. ✔ COMPLETED (Hotovo) # --------------------------- # (This logic remains unchanged) if progress >= 1.0 or state in {"completed", "uploading", "stalledUP", "queuedUP"}: stat_completed += 1 deleted_completed.append(t.name) try: # Smažeme z QB, ale necháme data na disku qb.torrents_delete(torrent_hashes=t_hash, delete_files=False) except Exception as e: print(f"⚠️ delete (keep data) failed for {t.name}: {e}") cursor.execute(""" UPDATE torrents SET qb_state='completed', qb_progress=100, qb_completed_datetime=NOW(), qb_last_update=NOW() WHERE qb_hash=%s OR torrent_hash=%s """, (t_hash, t_hash)) continue # --------------------------- # 2. ❌ DEAD (Mrtvý) # --------------------------- # UPDATED LOGIC: # 1. Must be older than limit (3 days) # 2. Availability < 1.0 (nobody has full file) # 3. AND MUST NOT BE QUEUED (queuedDL, queuedUP, etc.) is_old_enough = age_in_minutes > DEAD_TORRENT_MINUTES is_unavailable = availability < 1.0 # Check if state contains 'queued' (covers 'queuedDL', 'queuedUP', etc.) is_queued = "queued" in state if is_old_enough and is_unavailable and not is_queued: stat_dead += 1 deleted_dead.append(f"{t.name} (Avail: {availability:.2f}, State: {state})") try: # Smažeme z QB včetně nedotažených souborů qb.torrents_delete(torrent_hashes=t_hash, delete_files=True) except Exception as e: print(f"⚠️ delete (files) failed for {t.name}: {e}") cursor.execute(""" UPDATE torrents SET qb_state='dead', qb_last_update=NOW() WHERE qb_hash=%s OR torrent_hash=%s """, (t_hash, t_hash))