71 lines
2.2 KiB
Python
71 lines
2.2 KiB
Python
import qbittorrentapi
|
|
import logging
|
|
from .config import settings
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
class QbittorrentManager:
|
|
def __init__(self):
|
|
self.url = settings.qbittorrent_url
|
|
self.username = settings.qbittorrent_username
|
|
self.password = settings.qbittorrent_password
|
|
self.client = None
|
|
|
|
def connect(self):
|
|
if not self.url:
|
|
raise Exception("qBittorrent URL not configured")
|
|
|
|
try:
|
|
self.client = qbittorrentapi.Client(
|
|
host=self.url,
|
|
username=self.username,
|
|
password=self.password,
|
|
VERIFY_WEBUI_CERTIFICATE=False
|
|
)
|
|
self.client.auth_log_in()
|
|
except qbittorrentapi.LoginFailed as e:
|
|
logger.error(f"qBittorrent Login Failed: {e}")
|
|
raise Exception("qBittorrent Login Failed")
|
|
except Exception as e:
|
|
logger.error(f"qBittorrent Connection Failed: {e}")
|
|
raise
|
|
|
|
def get_torrents(self):
|
|
self.connect()
|
|
# Return list of dicts with relevant info
|
|
torrents = self.client.torrents_info()
|
|
return [
|
|
{
|
|
"hash": t.hash,
|
|
"name": t.name,
|
|
"state": t.state,
|
|
"save_path": t.save_path,
|
|
"progress": t.progress,
|
|
"size": t.size,
|
|
"dlspeed": t.dlspeed,
|
|
"upspeed": t.upspeed,
|
|
"eta": t.eta
|
|
}
|
|
for t in torrents
|
|
]
|
|
|
|
def pause_torrents(self, hashes):
|
|
self.connect()
|
|
self.client.torrents_pause(torrent_hashes=hashes)
|
|
|
|
def resume_torrents(self, hashes):
|
|
self.connect()
|
|
self.client.torrents_resume(torrent_hashes=hashes)
|
|
|
|
def delete_torrents(self, hashes, delete_files=False):
|
|
self.connect()
|
|
self.client.torrents_delete(torrent_hashes=hashes, delete_files=delete_files)
|
|
|
|
def recheck_torrents(self, hashes):
|
|
self.connect()
|
|
self.client.torrents_recheck(torrent_hashes=hashes)
|
|
|
|
def set_location(self, hashes, location):
|
|
self.connect()
|
|
self.client.torrents_set_location(location=location, torrent_hashes=hashes)
|