Skip to content

Commit a96f886

Browse files
author
Ben
committed
test
1 parent f82ff9f commit a96f886

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed

.github/workflows/gen-stats.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Fetch Torrents and Update Stats
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
workflow_dispatch:
8+
9+
jobs:
10+
fetch_torrents:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout repository
15+
uses: actions/checkout@v4
16+
17+
- name: Set up Python
18+
uses: actions/setup-python@v5
19+
with:
20+
python-version: '3.x'
21+
22+
- name: Install dependencies
23+
run: |
24+
python -m pip install --upgrade pip
25+
pip install requests
26+
27+
- name: Run script
28+
env:
29+
QB_USERNAME: ${{ secrets.QB_USERNAME }}
30+
QB_PASSWORD: ${{ secrets.QB_PASSWORD }}
31+
HTTP_USERNAME: ${{ secrets.HTTP_USERNAME }}
32+
HTTP_PASSWORD: ${{ secrets.HTTP_PASSWORD }}
33+
QB_URL_1: ${{ secrets.QB_URL_1 }}
34+
QB_URL_2: ${{ secrets.QB_URL_2 }}
35+
run: |
36+
python tools/gen-stats.py

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@ This repository aims to provide macOS ISOs for security researchers, developers,
138138

139139
</details>
140140

141+
#### Seeding status 🌱
142+
143+
<!--- STATS_START --->
144+
145+
<!--- STATS_END --->
146+
141147
## FAQ ❓
142148

143149
### How to download the ISOs?

tools/gen-stats.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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

Comments
 (0)