Skip to content

Instantly share code, notes, and snippets.

@phuedx
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phuedx/0e13b0d7f86c57bbdd77 to your computer and use it in GitHub Desktop.
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.
#!/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()
@phuedx
Copy link
Author

phuedx commented Aug 10, 2014

This example is based on Aaron Halfaker's examples/listen.py, which is available as part of his MediaWiki-events library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment