Rewrite qbt-sum-size in Python

This commit is contained in:
Jeffrey Serio 2024-07-06 22:14:58 -05:00
parent a8814373af
commit d7c600ae3c
3 changed files with 83 additions and 25 deletions

32
bin/.archived/qbt-sum-size.bash Executable file
View File

@ -0,0 +1,32 @@
#!/usr/bin/env bash
#
# qbt-sum-size - get the sum total size of all torrents in qBittorrent instance
set -euo pipefail
# Dependency check
missing_deps=()
command -v jq >/dev/null || missing_deps+=(jq)
command -v qbt >/dev/null || missing_deps+=(qbt)
if (( "${#missing_deps[@]}" != 0 )); then
echo "Missing dependencies:" "${missing_deps[@]}"
exit 1
fi
total_completed_bytes=$(qbt torrent list -F json | jq '[.[] | select(.state=="queuedUP") | .total_size | tonumber] | add')
if [ "${#total_completed_bytes}" -ge 13 ]; then
total_completed=$(echo "$total_completed_bytes" | awk '{print $1/1024/1024/1024/1024 " TiB "}')
else
total_completed=$(echo "$total_completed_bytes" | awk '{print $1/1024/1024/1024 " GiB "}')
fi
total_added_bytes=$(qbt torrent list -F json | jq '[.[].total_size | tonumber] | add')
if [ "${#total_added_bytes}" -ge 13 ]; then
total_added=$(echo "$total_added_bytes" | awk '{print $1/1024/1024/1024/1024 " TiB "}')
else
total_added=$(echo "$total_added_bytes" | awk '{print $1/1024/1024/1024 " GiB "}')
fi
printf "\nTotal completed size: %s\n" "$total_completed"
printf "Total added size: %s\n\n" "$total_added"

View File

@ -1,32 +1,58 @@
#!/usr/bin/env bash
#!/usr/bin/env python3
#
# qbt-sum-size - get the sum total size of all torrents in qBittorrent instance
# Dependencies: python-qbittorrent
# pip install --user python-qbittorrent
#
# On Debian:
# pip install --user python-qbittorrent --break-system-packages
set -euo pipefail
from qbittorrent import Client
# Dependency check
missing_deps=()
command -v jq >/dev/null || missing_deps+=(jq)
command -v qbt >/dev/null || missing_deps+=(qbt)
if (( "${#missing_deps[@]}" != 0 )); then
echo "Missing dependencies:" "${missing_deps[@]}"
exit 1
fi
# convert byte units
def human_bytes(bites: int) -> str:
B = float(bites)
KiB = float(1024)
MiB = float(KiB**2)
GiB = float(KiB**3)
TiB = float(KiB**4)
total_completed_bytes=$(qbt torrent list -F json | jq '[.[] | select(.state=="queuedUP") | .total_size | tonumber] | add')
if [ "${#total_completed_bytes}" -ge 13 ]; then
total_completed=$(echo "$total_completed_bytes" | awk '{print $1/1024/1024/1024/1024 " TiB "}')
else
total_completed=$(echo "$total_completed_bytes" | awk '{print $1/1024/1024/1024 " GiB "}')
fi
match B:
case B if B < KiB:
return "{0} {1}".format(B, "bytes" if 0 == B > 1 else "byte")
case B if KiB <= B < MiB:
return "{0:.2f} KiB".format(B / KiB)
case B if MiB <= B < GiB:
return "{0:.2f} MiB".format(B / MiB)
case B if GiB <= B < TiB:
return "{0:.2f} GiB".format(B / GiB)
case B if TiB <= B:
return "{0:.2f} TiB".format(B / TiB)
case _:
return ""
total_added_bytes=$(qbt torrent list -F json | jq '[.[].total_size | tonumber] | add')
if [ "${#total_added_bytes}" -ge 13 ]; then
total_added=$(echo "$total_added_bytes" | awk '{print $1/1024/1024/1024/1024 " TiB "}')
else
total_added=$(echo "$total_added_bytes" | awk '{print $1/1024/1024/1024 " GiB "}')
fi
printf "\nTotal completed size: %s\n" "$total_completed"
printf "Total added size: %s\n\n" "$total_added"
if __name__ == "__main__":
# Instantiate a qbittorrent client
qb = Client("http://localhost:8080/")
# username and password not required when 'Bypass from localhost' setting is active.
qb.login()
# get torrents
torrents = qb.torrents()
# get total_completed_bytes
completed_torrent_sizes = list()
for torrent in torrents:
if torrent["state"] == "queuedUP":
completed_torrent_sizes.append(torrent["total_size"])
total_completed_bytes = sum(completed_torrent_sizes)
# get total_added_bytes
total_added_bytes = sum([torrent["total_size"] for torrent in torrents])
# print the results
print(f"\nTotal completed size: {human_bytes(total_completed_bytes)}")
print(f"Total added size: {human_bytes(total_added_bytes)}\n")