summaryrefslogtreecommitdiff
path: root/tracker.jordan.im/update-stats.py
blob: bc370b22c9ec231fbcf9654eeeecdd2b01b5e694 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python3

import fileinput
import sys
import urllib.request

INDEX_PATH = '/var/www/html/tracker.jordan.im/index.html'

stats = dict.fromkeys(['torrents', 'connections', 'uptime'])

conn = urllib.request.Request(
    'http://127.0.0.1:6969/stats?format=txt&mode=conn',
)
r = urllib.request.urlopen(conn).read().decode('utf-8').split('\n')

stats['uptime'] = int(int(r[2].split()[0]) / 3600)
stats['connections'] = r[3].split()[2]

conn = urllib.request.Request(
    'http://127.0.0.1:6969/stats?format=txt'
)

r = urllib.request.urlopen(conn).read().decode('utf-8').split('\n')

stats['torrents'] = r[2].split()[2]

s = (
    f'The tracker has been online for {stats["uptime"]} hrs, '
    f'serving {stats["torrents"]} torrents '
    f'with {stats["connections"]} connections per second.\n'
)

for line in fileinput.input([INDEX_PATH], inplace=True):
    if line.strip().startswith('The tracker has been'):
        line = s
    sys.stdout.write(line)

#import code
#code.interact(local=locals())