forked from ebraminio/aiosseclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.py
executable file
·78 lines (61 loc) · 2.08 KB
/
sample.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
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
72
73
74
75
76
77
78
#!/usr/bin/env python3
'''Asynchronous Server Side Events (SSE) Client'''
import asyncio
import json
import aiohttp
from aiosseclient import aiosseclient
wikis = {}
# count: int = 0
async def fetch(session, d, url):
'''Fetch a url'''
try:
resp = await session.get(url)
doc = await resp.text()
d['content'] = doc
with open('stream.json', 'a', encoding='utf8') as ostream:
json.dump(d, ostream)
ostream.write('\n')
return 'ok'
except TimeoutError as e:
print(e)
return await fetch(session, d, url)
async def read_stream(session):
'''Main loop'''
try:
async for event in aiosseclient('https://stream.wikimedia.org/v2/stream/recentchange'):
d = json.loads(event.data)
w = d['wiki']
if 'revision' in d:
_id = d['revision']['old']
else:
continue
if _id is None:
continue
if w not in wikis:
wikis[w] = {'min': _id, 'max': _id, 'count': 1}
if wikis[w]['min'] > _id:
wikis[w]['min'] = _id
# if wikis[w]['max'] < _id:
# wikis[w]['max'] = _id
wikis[w]['count'] = wikis[w]['count'] + 1
# pprint.pprint([w, wikis[w]])
# pprint.pprint(d)
server_script_path = d['server_script_path']
server_url = d['server_url']
url = server_url + server_script_path + f'/index.php?oldid={str(_id)}&action=raw'
status = await fetch(session, d, url)
print('status', status)
# global count
# count = count + 1
# if count % 1000 == 0:
# print('.')
except TimeoutError as e:
print(e)
return await read_stream(session)
async def main():
'''Main'''
async with aiohttp.ClientSession() as session:
never = await read_stream(session)
print(never)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())