From 38ffe9e9639179800d57cbce810b029fe1670005 Mon Sep 17 00:00:00 2001 From: Jeffrey Serio Date: Tue, 5 Nov 2024 16:24:52 -0600 Subject: [PATCH] Use context manager --- add_qbt_trackers.py | 45 --------------------------------------------- qbth.py | 4 ++-- 2 files changed, 2 insertions(+), 47 deletions(-) delete mode 100755 add_qbt_trackers.py diff --git a/add_qbt_trackers.py b/add_qbt_trackers.py deleted file mode 100755 index ed8ae6f..0000000 --- a/add_qbt_trackers.py +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/env python3 - -"""add_qbt_trackers.py - -Description: -This script fetches torrent tracker URLs from plaintext lists hosted on the web -and adds them to each torrent in a qBittorrent instance. - -Usage: - add_qbt_trackers.py (HOSTNAME) (USERNAME) (PASSWORD) - add_qbt_trackers.py -h - -Examples: - add_qbt_trackers.py "http://localhost:8080" "admin" "adminadmin" - -Options: - -h, --help show this help message and exit -""" - -import requests -from docopt import docopt -from qbittorrent import Client - -if __name__ == "__main__": - args = docopt(__doc__) # type: ignore - - # Initialize client and login - qb = Client(args["HOSTNAME"]) - qb.login(username=args["USERNAME"], password=args["PASSWORD"]) - - live_trackers_list_urls = [ - "https://newtrackon.com/api/stable", - "https://trackerslist.com/best.txt", - "https://trackerslist.com/http.txt", - "https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_best.txt", - ] - - combined_trackers_urls = [] - for url in live_trackers_list_urls: - response = requests.get(url, timeout=60) - tracker_urls = [x for x in response.text.splitlines() if x != ""] - combined_trackers_urls.extend(tracker_urls) - - for torrent in qb.torrents(): - qb.add_trackers(torrent.get("hash"), "\n".join(combined_trackers_urls)) # type: ignore diff --git a/qbth.py b/qbth.py index e5ae3db..2d39e7c 100755 --- a/qbth.py +++ b/qbth.py @@ -30,10 +30,10 @@ conn_info = dict( username=args["USERNAME"], password=args["PASSWORD"], ) -qbt_client = qbittorrentapi.Client(**conn_info) try: - qbt_client.auth_log_in() + with qbittorrentapi.Client(**conn_info) as qbt_client: + qbt_client.auth_log_in() except qbittorrentapi.LoginFailed as e: print(e)