1 """Helper functions for qBittorrent."""
3 from datetime
import UTC, datetime
4 from typing
import Any, cast
6 from qbittorrentapi
import Client, TorrentDictionary, TorrentInfoList
9 def setup_client(url: str, username: str, password: str, verify_ssl: bool) -> Client:
10 """Create a qBittorrent client."""
12 url, username=username, password=password, VERIFY_WEBUI_CERTIFICATE=verify_ssl
14 client.auth_log_in(username, password)
19 """Convert seconds to HH:MM:SS format."""
20 if seconds == 8640000:
23 minutes, seconds = divmod(seconds, 60)
24 hours, minutes = divmod(minutes, 60)
25 return f
"{int(hours):02}:{int(minutes):02}:{int(seconds):02}"
29 """Format a UNIX timestamp to a human-readable date."""
30 dt_object = datetime.fromtimestamp(timestamp, tz=UTC)
31 return dt_object.isoformat()
35 """Format the progress of a torrent."""
36 progress = cast(float, torrent[
"progress"]) * 100
37 return f
"{progress:.2f}"
41 torrents: TorrentInfoList,
42 ) -> dict[str, dict[str, Any]]:
43 """Format a list of torrents."""
45 for torrent
in torrents:
52 """Format a single torrent."""
54 value[
"id"] = torrent[
"hash"]
57 value[
"status"] = torrent[
"state"]
59 ratio = cast(float, torrent[
"ratio"])
60 value[
"ratio"] = f
"{ratio:.2f}"
str format_progress(TorrentDictionary torrent)
dict[str, Any] format_torrent(TorrentDictionary torrent)
str format_unix_timestamp(timestamp)
str seconds_to_hhmmss(seconds)
Client setup_client(str url, str username, str password, bool verify_ssl)
dict[str, dict[str, Any]] format_torrents(TorrentInfoList torrents)