|
| 1 | +import requests |
| 2 | +from requests.auth import HTTPBasicAuth |
| 3 | +import os |
| 4 | +import json |
| 5 | + |
| 6 | +qb_urls = [ |
| 7 | + {"url": os.getenv("QB_URL_1"), "auth_type": "normal"}, |
| 8 | + {"url": os.getenv("QB_URL_2"), "auth_type": "http"}, |
| 9 | +] |
| 10 | + |
| 11 | +def get_uploaded(qb_url, auth_type): |
| 12 | + session = requests.Session() |
| 13 | + active = False |
| 14 | + total_uploaded = 0 |
| 15 | + |
| 16 | + try: |
| 17 | + response = session.get(qb_url["url"]) |
| 18 | + if response.status_code != 200: |
| 19 | + raise Exception("Server not reachable") |
| 20 | + |
| 21 | + if auth_type == "http": |
| 22 | + auth = HTTPBasicAuth(os.getenv("HTTP_USERNAME"), os.getenv("HTTP_PASSWORD")) |
| 23 | + response = session.get(f"{qb_url['url']}/api/v2/torrents/info", auth=auth) |
| 24 | + else: |
| 25 | + response = session.post(f"{qb_url['url']}/api/v2/auth/login", data={"username": os.getenv("QB_USERNAME"), "password": os.getenv("QB_PASSWORD")}) |
| 26 | + if response.text != "Ok.": |
| 27 | + raise Exception("Login failed") |
| 28 | + response = session.get(f"{qb_url['url']}/api/v2/torrents/info") |
| 29 | + |
| 30 | + active = True |
| 31 | + total_uploaded = sum(torrent["uploaded"] for torrent in response.json() if any(keyword.lower() in torrent["name"].lower() for keyword in ["macOS", "OS X"])) |
| 32 | + except Exception as e: |
| 33 | + active = False |
| 34 | + print(f"Error accessing {qb_url['url']}: {e}") |
| 35 | + |
| 36 | + return active, total_uploaded |
| 37 | + |
| 38 | +def bytes_to_human_readable(num_bytes): |
| 39 | + for unit in ["B", "KB", "MB", "GB", "TB"]: |
| 40 | + if num_bytes < 1024: |
| 41 | + return f"{num_bytes:.2f} {unit}" |
| 42 | + num_bytes /= 1024 |
| 43 | + return f"{num_bytes:.2f} TB" |
| 44 | + |
| 45 | +def update_readme(stats): |
| 46 | + readme_path = "README.md" |
| 47 | + with open(readme_path, "r") as file: |
| 48 | + readme_content = file.read() |
| 49 | + |
| 50 | + start_marker = "<!--- STATS_START --->" |
| 51 | + end_marker = "<!--- STATS_END --->" |
| 52 | + stats_table = f""" |
| 53 | +| Server | Active | Total Upload | |
| 54 | +|--------|--------|--------------| |
| 55 | +| Server1 | {stats['server1']['active']} | {stats['server1']['total_upload']} | |
| 56 | +| Server2 | {stats['server2']['active']} | {stats['server2']['total_upload']} | |
| 57 | +| **Combined** | - | {stats['combined_total_upload']} | |
| 58 | + """ |
| 59 | + |
| 60 | + new_readme_content = readme_content.split(start_marker)[0] + start_marker + stats_table + end_marker + readme_content.split(end_marker)[1] |
| 61 | + |
| 62 | + with open(readme_path, "w") as file: |
| 63 | + file.write(new_readme_content) |
| 64 | + |
| 65 | +server1_active, server1_total_upload = get_uploaded(qb_urls[0], qb_urls[0]["auth_type"]) |
| 66 | +server2_active, server2_total_upload = get_uploaded(qb_urls[1], qb_urls[1]["auth_type"]) |
| 67 | + |
| 68 | +combined_total_upload = server1_total_upload + server2_total_upload |
| 69 | + |
| 70 | +stats = { |
| 71 | + "server1": { |
| 72 | + "active": "Yes" if server1_active else "No", |
| 73 | + "total_upload": bytes_to_human_readable(server1_total_upload) |
| 74 | + }, |
| 75 | + "server2": { |
| 76 | + "active": "Yes" if server2_active else "No", |
| 77 | + "total_upload": bytes_to_human_readable(server2_total_upload) |
| 78 | + }, |
| 79 | + "combined_total_upload": bytes_to_human_readable(combined_total_upload) |
| 80 | +} |
| 81 | + |
| 82 | +print(json.dumps(stats, indent=2)) |
| 83 | + |
| 84 | +update_readme(stats) |
0 commit comments