Created
November 30, 2019 23:40
-
-
Save filipecosta90/ee3b4b08bac1cdadfd76cbdd6276bd30 to your computer and use it in GitHub Desktop.
Module and script to forward events from wikimedia stream to Redis pub-sub.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
"""Module and script to forward events from wikimedia stream to Redis pub-sub.""" | |
import asyncio as aio | |
import json | |
import aioredis | |
from aiohttp_sse_client import client as sse_client | |
from wikiutils import pubsub_channel, redis_url | |
wikimedia_stream = "https://stream.wikimedia.org/v2/stream/recentchange" | |
async def publish(): | |
"""Read events from pub-sub channel.""" | |
redis = await aioredis.create_redis_pool(redis_url) | |
while True: | |
async with sse_client.EventSource(wikimedia_stream) as event_source: | |
try: | |
async for event in event_source: | |
json.loads(event.data) | |
await redis.publish(pubsub_channel, event.data) | |
except (ConnectionError, aio.TimeoutError, aio.CancelledError): | |
pass | |
if __name__ == "__main__": | |
aio.run(publish(), debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment