diff --git a/Seedbox/40 ČištěníCompleted.py b/Seedbox/40 ČištěníCompleted.py new file mode 100644 index 0000000..0ee121e --- /dev/null +++ b/Seedbox/40 ČištěníCompleted.py @@ -0,0 +1,124 @@ +anoimport pymysql +import qbittorrentapi +from datetime import datetime + +# ============================================================ +# CONFIG +# ============================================================ + +DRY_RUN = False # ← změň na True pokud chceš test + +QBT_URL = "https://vladob.zen.usbx.me/qbittorrent" +QBT_USER = "vladob" +QBT_PASS = "jCni3U6d#y4bfcm" + +DB_CONFIG = { + "host": "192.168.1.50", + "port": 3306, + "user": "root", + "password": "Vlado9674+", + "database": "torrents", + "charset": "utf8mb4", + "autocommit": True, +} + + +# ============================================================ +# CONNECT +# ============================================================ + +def connect_qbt(): + qbt = qbittorrentapi.Client( + host=QBT_URL, + username=QBT_USER, + password=QBT_PASS, + VERIFY_WEBUI_CERTIFICATE=False + ) + qbt.auth_log_in() + return qbt + + +def connect_db(): + return pymysql.connect(**DB_CONFIG) + + +# ============================================================ +# MAIN +# ============================================================ + +def main(): + + print("🚀 UltraCC completed torrent cleanup") + + qbt = connect_qbt() + db = connect_db() + cursor = db.cursor() + + torrents = qbt.torrents_info() + + removed_count = 0 + skipped_count = 0 + + for t in torrents: + + # ---------------------------------------------------- + # Test completed via completion_on + # ---------------------------------------------------- + if not t.completion_on: + continue + + thash = t.hash.lower() + name = t.name + state = t.state + + completion_dt = datetime.fromtimestamp(t.completion_on) + + # ---------------------------------------------------- + # Skip pokud už DB má completion + # ---------------------------------------------------- + cursor.execute(""" + SELECT qb_completed_datetime + FROM torrents + WHERE torrent_hash = %s + """, (thash,)) + + row = cursor.fetchone() + + if row and row[0]: + skipped_count += 1 + continue + + print(f"✔ COMPLETED: {name}") + print(f" Completion time: {completion_dt}") + + # ---------------------------------------------------- + # UPDATE DB + # ---------------------------------------------------- + if not DRY_RUN: + cursor.execute(""" + UPDATE torrents + SET + qb_state = %s, + qb_progress = 1, + qb_completed_datetime = %s, + qb_last_update = NOW() + WHERE torrent_hash = %s + """, (f"completed_removed ({state})", completion_dt, thash)) + + # ---------------------------------------------------- + # REMOVE TORRENT + # ---------------------------------------------------- + if DRY_RUN: + print(" [DRY] Would remove torrent") + else: + qbt.torrents_delete(delete_files=False, torrent_hashes=thash) + removed_count += 1 + + print("------------------------------------------------") + print(f"Removed torrents : {removed_count}") + print(f"Skipped torrents : {skipped_count}") + print(f"DRY_RUN = {DRY_RUN}") + + +if __name__ == "__main__": + main()