Skip to content

Instantly share code, notes, and snippets.

@balibali
Created March 18, 2011 17:07
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 balibali/876435 to your computer and use it in GitHub Desktop.
Save balibali/876435 to your computer and use it in GitHub Desktop.
"""
WebSocket Client for Growl Notification
Requirements:
- websocket-client:
https://github.com/liris/websocket-client
- Growl:
http://growl.info/documentation/developer/python-support.php
"""
import websocket
import json
import Growl
import urllib2
class MyGrowl:
@classmethod
def get_growl(cls):
if not hasattr(cls, "g"):
cls.g = Growl.GrowlNotifier(applicationName="BalibaliNotifier",
notifications=["Notify"])
cls.g.register()
return cls.g
@classmethod
def notify(cls, title, description, image_url="", sticky=False):
g = cls.get_growl()
g.notify(noteType="Notify",
title=title,
description=description,
icon=cls.get_image(image_url),
sticky=sticky)
@staticmethod
def get_image(url):
if url:
return Growl.Image.imageWithData(urllib2.urlopen(url).read())
def on_message(ws, message):
MyGrowl.notify(**json.loads(message))
if __name__ == "__main__":
# websocket.enableTrace(True)
ws = websocket.WebSocketApp("ws://localhost:3000/", on_message=on_message)
ws.run_forever()
var ws = require("websocket-server");
var server;
var express = require("express");
var app = express.createServer();
app.get("/", function(req, res) {
if (req.query.description) {
server.broadcast(JSON.stringify({
"title": req.query.title,
"description": req.query.description,
"image_url": req.query.image_url,
"sticky": req.query.sticky
}));
res.send("notified");
} else {
res.send("description not found");
}
});
server = ws.createServer({server: app, debug: true});
server.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment