-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-mirror-status.py
46 lines (39 loc) · 1.59 KB
/
update-mirror-status.py
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
40
41
42
43
44
45
46
#!/bin/python
if __name__ == '__main__':
import sys
import time
import requests
from pathlib import Path
lines = []
lines.append('<script src="./time.js"></script>')
lines.append('# Mirror status')
lines.append(f'Last check: <script type="text/javascript">localize({time.time()});</script>')
lines.append('')
lines.append('|Country|Mirror|Last update|')
lines.append('|:------|:-----|:----------|')
mirrorlist = Path(sys.argv[1]) / 'mirrorlist.arch4edu'
with open(mirrorlist) as f:
_lines = f.readlines()
session = requests.session()
for _line in _lines:
_line = _line.strip('\n')
if _line.startswith('## '):
country = _line[3:]
elif _line.startswith('#Server = '):
mirror = _line[10:-5]
print('Checking', mirror)
try:
last_update = session.get(f'{mirror}/lastupdate', timeout=10)
if last_update.status_code != 200:
status = f'Response {last_update.status_code}'
else:
last_update = last_update.content.decode('utf-8').strip('\n')
status = f'<script type="text/javascript">localize({last_update});</script>'
except Exception as e:
status = type(e).__name__
lines.append('|'.join(['', country, mirror, status, '']))
lines.append('')
lines.append('<script src="./tablefilter/tablefilter.js"></script>')
lines.append('<script src="./table.js"></script>')
with open('mirrors.md', 'w') as f:
f.write('\n'.join(lines))