Last active
August 29, 2015 14:05
-
-
Save phuedx/0e13b0d7f86c57bbdd77 to your computer and use it in GitHub Desktop.
Listen to a wiki's recentchanges feed and publishes them as standardised JSON strings via a ØMQ PUB socket.
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 | |
# -*- coding: utf-8 -*- | |
""" | |
Listens to a wiki's recentchanges feed and publishes them as standardised JSON | |
events, converted to strings, via a ØMQ PUB socket. | |
Usage: | |
./firehose.py <api_url> <endpoint> | |
Options: | |
<api_url> The URL of the wiki's API. | |
<endpoint> The endpoint of the ØMQ PUB socket. | |
""" | |
from docopt import docopt | |
from mwevents.sources import API | |
import zmq | |
import json | |
def main(): | |
args = docopt(__doc__) | |
run(args['<api_url>'], args['<endpoint>']) | |
def run(api_url, endpoint): | |
api_source = API.from_api_url(api_url) | |
context = zmq.Context() | |
socket = context.socket(zmq.PUB) | |
socket.bind(endpoint) | |
try: | |
listener = api_source.listener() | |
for event in listener: | |
socket.send_string(json.dumps(event.to_json())) | |
except KeyboardInterrupt: | |
print("Keyboard interrupt received. Shutting down.") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This example is based on Aaron Halfaker's
examples/listen.py
, which is available as part of his MediaWiki-events library.