Created
November 18, 2024 23:42
-
-
Save adapowers/94df85a8320fdc9d5c440a7d6107339e to your computer and use it in GitHub Desktop.
qbitstats.py - get UL/DL stats by tracker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests | |
| from collections import defaultdict | |
| from urllib.parse import urlparse | |
| def human_readable_bytes(size): | |
| for unit in ['B', 'KB', 'MB', 'GB', 'TB']: | |
| if size < 1024: | |
| return f"{size:.2f} {unit}" | |
| size /= 1024 | |
| return f"{size:.2f} PB" | |
| # # qbittorrent settings | |
| url = "http://localhost:8080" | |
| username = "your_username" | |
| password = "your_password" | |
| # authenticate | |
| session = requests.Session() | |
| session.post(f"{url}/api/v2/auth/login", data={"username": username, "password": password}) | |
| # get torrents | |
| response = session.get(f"{url}/api/v2/torrents/info") | |
| torrents = response.json() | |
| # aggregate by tracker | |
| stats = defaultdict(lambda: {"uploaded": 0, "downloaded": 0}) | |
| for torrent in torrents: | |
| tracker_url = torrent.get("tracker", "unknown") | |
| domain = urlparse(tracker_url).netloc.split(':')[0] # strip to domain.tld | |
| stats[domain]["uploaded"] += torrent.get("uploaded", 0) | |
| stats[domain]["downloaded"] += torrent.get("downloaded", 0) | |
| # print stats | |
| for tracker, data in stats.items(): | |
| uploaded = human_readable_bytes(data["uploaded"]) | |
| downloaded = human_readable_bytes(data["downloaded"]) | |
| print(f"tracker: {tracker}, uploaded: {uploaded}, downloaded: {downloaded}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment