-
Notifications
You must be signed in to change notification settings - Fork 50
/
check-lastupdate
executable file
·71 lines (61 loc) · 1.55 KB
/
check-lastupdate
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/python3
import asyncio
from urllib.parse import urljoin
import yaml
import aiohttp
def humantime(t: int) -> str:
'''seconds -> XhYmZs'''
m, s = divmod(t, 60)
h, m = divmod(m, 60)
d, h = divmod(h, 24)
ret = ''
if d:
ret += '%dd' % d
if h:
ret += '%dh' % h
if m:
ret += '%dm' % m
if s:
ret += '%ds' % s
if not ret:
ret = '0s'
return ret
async def get_lastupdate(session, name, url):
try:
res = await session.get(url)
content = await res.text()
dt = int(content)
print(f'{name} done.')
return name, dt
except Exception as e:
return name, e
async def main():
with open('mirrors.yaml') as f:
data = yaml.load(f, Loader=yaml.FullLoader)
async with aiohttp.ClientSession(
timeout = aiohttp.ClientTimeout(total=10),
) as session:
futures = []
for mirror in data:
url = urljoin(mirror['url'], 'lastupdate')
fu = get_lastupdate(session, mirror['provider'], url)
futures.append(fu)
fu = get_lastupdate(session, 'tier0', 'https://repo.archlinuxcn.org/lastupdate')
futures.append(fu)
results = await asyncio.gather(*futures)
done = {}
error = {}
for name, dt in results:
if isinstance(dt, int):
done[name] = dt
else:
error[name] = dt
print('\nLags:')
tier0 = done.pop('tier0')
for name, dt in sorted(done.items(), reverse=True, key=lambda x: x[1]):
print(f'{name} {humantime(tier0-dt)}')
print('\nErrors:')
for name, e in error.items():
print(f'{name} {e!r}')
if __name__ == '__main__':
asyncio.run(main())