|
| 1 | +#!/bin/python |
| 2 | + |
| 3 | +if __name__ == '__main__': |
| 4 | + import sys |
| 5 | + import time |
| 6 | + import requests |
| 7 | + from pathlib import Path |
| 8 | + |
| 9 | + lines = [] |
| 10 | + lines.append('<script src="./time.js"></script>') |
| 11 | + lines.append('# Mirror status') |
| 12 | + lines.append(f'Last check: <script type="text/javascript">localize({time.time()});</script>') |
| 13 | + lines.append('') |
| 14 | + lines.append('|Country|Mirror|Last update|') |
| 15 | + lines.append('|:------|:-----|:----------|') |
| 16 | + |
| 17 | + mirrorlist = Path(sys.argv[1]) / 'mirrorlist.arch4edu' |
| 18 | + with open(mirrorlist) as f: |
| 19 | + _lines = f.readlines() |
| 20 | + |
| 21 | + session = requests.session() |
| 22 | + for _line in _lines: |
| 23 | + _line = _line.strip('\n') |
| 24 | + if _line.startswith('## '): |
| 25 | + country = _line[3:] |
| 26 | + elif _line.startswith('#Server = '): |
| 27 | + mirror = _line[10:-5] |
| 28 | + print('Checking', mirror) |
| 29 | + try: |
| 30 | + last_update = session.get(f'{mirror}/lastupdate', timeout=10) |
| 31 | + if last_update.status_code != 200: |
| 32 | + status = f'Response {last_update.status_code}' |
| 33 | + else: |
| 34 | + last_update = last_update.content.decode('utf-8').strip('\n') |
| 35 | + status = f'<script type="text/javascript">localize({last_update});</script>' |
| 36 | + except Exception as e: |
| 37 | + status = type(e).__name__ |
| 38 | + lines.append('|'.join(['', country, mirror, status, ''])) |
| 39 | + |
| 40 | + lines.append('') |
| 41 | + lines.append('<script src="./tablefilter/tablefilter.js"></script>') |
| 42 | + lines.append('<script src="./table.js"></script>') |
| 43 | + |
| 44 | + |
| 45 | + with open('mirrors.md', 'w') as f: |
| 46 | + f.write('\n'.join(lines)) |
0 commit comments