Advertisement
Guest User

Untitled

a guest
Apr 11th, 2012
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. """
  2. WebSocket Client for Growl Notification
  3.  
  4. Requirements:
  5. - websocket-client:
  6. https://github.com/liris/websocket-client
  7. - Growl:
  8. http://growl.info/documentation/developer/python-support.php
  9. """
  10.  
  11. import websocket
  12. import json
  13. import Growl
  14. import urllib2
  15.  
  16. class MyGrowl:
  17. @classmethod
  18. def get_growl(cls):
  19. if not hasattr(cls, "g"):
  20. cls.g = Growl.GrowlNotifier(applicationName="BalibaliNotifier",
  21. notifications=["Notify"])
  22. cls.g.register()
  23. return cls.g
  24.  
  25. @classmethod
  26. def notify(cls, title, description, image_url="", sticky=False):
  27. g = cls.get_growl()
  28. g.notify(noteType="Notify",
  29. title=title,
  30. description=description,
  31. icon=cls.get_image(image_url),
  32. sticky=sticky)
  33.  
  34. @staticmethod
  35. def get_image(url):
  36. if url:
  37. return Growl.Image.imageWithData(urllib2.urlopen(url).read())
  38.  
  39. def on_message(ws, message):
  40. MyGrowl.notify(**json.loads(message))
  41.  
  42. if __name__ == "__main__":
  43. # websocket.enableTrace(True)
  44. ws = websocket.WebSocketApp("ws://localhost:3000/", on_message=on_message)
  45. ws.run_forever()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement