diff options
author | jwansek <eddie.atten.ea29@gmail.com> | 2021-02-02 12:24:06 +0000 |
---|---|---|
committer | jwansek <eddie.atten.ea29@gmail.com> | 2021-02-02 12:24:06 +0000 |
commit | 1dac1ee189bdcf70a5dd933d5f6a69a15d5a8d7e (patch) | |
tree | 0bff6312d7e3bcc364d7632da708f74d15cbd0f3 /services.py | |
parent | 6eb416ac7784ce08b6959e3db06ccd404736516f (diff) | |
download | boymoder.blog-1dac1ee189bdcf70a5dd933d5f6a69a15d5a8d7e.tar.gz boymoder.blog-1dac1ee189bdcf70a5dd933d5f6a69a15d5a8d7e.zip |
added the program files for the first time
Diffstat (limited to 'services.py')
-rw-r--r-- | services.py | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/services.py b/services.py new file mode 100644 index 0000000..16268ef --- /dev/null +++ b/services.py @@ -0,0 +1,68 @@ +import qbittorrent +import docker +import clutch +import app +import json + +def humanbytes(B): + 'Return the given bytes as a human friendly KB, MB, GB, or TB string' + B = float(B) + KB = float(1024) + MB = float(KB ** 2) # 1,048,576 + GB = float(KB ** 3) # 1,073,741,824 + TB = float(KB ** 4) # 1,099,511,627,776 + + if B < KB: + return '{0} {1}'.format(B,'Bytes' if 0 == B > 1 else 'Byte') + elif KB <= B < MB: + return '{0:.2f} KB'.format(B/KB) + elif MB <= B < GB: + return '{0:.2f} MB'.format(B/MB) + elif GB <= B < TB: + return '{0:.2f} GB'.format(B/GB) + elif TB <= B: + return '{0:.2f} TB'.format(B/TB) + +def get_docker_stats(): + client = docker.DockerClient(base_url = "tcp://%s:%s" % (app.CONFIG["docker"]["url"], app.CONFIG["docker"]["port"])) + return { + container.name: container.status + for container in client.containers.list(all = True) + } + +def get_qbit_stats(): + numtorrents = 0 + bytes_dl = 0 + bytes_up = 0 + qb = qbittorrent.Client('http://%s:%s/' % (app.CONFIG["qbittorrent"]["url"], app.CONFIG["qbittorrent"]["port"])) + qb.login(username = app.CONFIG["qbittorrent"]["user"], password = app.CONFIG["qbittorrent"]["passwd"]) + + for torrent in qb.torrents(): + numtorrents += 1 + bytes_up += torrent["uploaded"] + bytes_dl += torrent["downloaded"] + + return { + "bytes_dl": humanbytes(bytes_dl), + "bytes_up": humanbytes(bytes_up), + "num": numtorrents, + "ratio": bytes_up / bytes_dl + } + +def get_trans_stats(): + client = clutch.client.Client( + address = "http://%s:%s/transmission/rpc" % (app.CONFIG["transmission"]["url"], app.CONFIG["transmission"]["port"]), + username = app.CONFIG["transmission"]["user"], + password = app.CONFIG["transmission"]["passwd"] + ) + stats = json.loads(client.session.stats().json()) + return { + "bytes_dl": humanbytes(stats["arguments"]["cumulative_stats"]["downloaded_bytes"]), + "bytes_up": humanbytes(stats["arguments"]["cumulative_stats"]["uploaded_bytes"]), + "num": stats["arguments"]["torrent_count"], + "ratio": stats["arguments"]["cumulative_stats"]["uploaded_bytes"] / stats["arguments"]["cumulative_stats"]["downloaded_bytes"] + } + + +if __name__ == "__main__": + print(get_trans_stats())
\ No newline at end of file |