Created
July 11, 2015 21:25
-
-
Save pabluk/39d41da7e39861ae4fcd to your computer and use it in GitHub Desktop.
Display desktop notifications using the n10n web service.
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 | |
"""Display desktop notifications using the n10n web service.""" | |
import logging | |
import os | |
import requests | |
import time | |
from gi.repository import Notify | |
__author__ = "Pablo Seminario" | |
__copyright__ = "Copyright 2015, Pablo Seminario" | |
__license__ = "GPLv3" | |
APP_NAME = 'n10n-desktop' | |
ICON = 'applications-chat' | |
INTERVAL = 10 | |
URL = 'http://n10n.debugstack.com/api/message/unread/' | |
USER = os.environ['N10N_USER'] | |
PASS = os.environ['N10N_PASS'] | |
logger = logging.getLogger(APP_NAME) | |
logger.setLevel(logging.DEBUG) | |
formatter = logging.Formatter('[%(asctime)s] %(levelname)s::%(message)s') | |
ch = logging.StreamHandler() | |
ch.setFormatter(formatter) | |
logger.addHandler(ch) | |
def main(): | |
logger.debug("Start daemon.") | |
Notify.init(APP_NAME) | |
seen_messages = [] | |
while True: | |
credentials = (USER, PASS) | |
try: | |
r = requests.get(URL, auth=credentials) | |
except requests.exceptions.ConnectionError: | |
logger.debug("Connection error.") | |
else: | |
content = r.json() | |
for m in content['messages']: | |
if m['id'] in seen_messages: | |
logger.debug("Message %s already seen." % m['id']) | |
continue | |
else: | |
seen_messages.append(m['id']) | |
n = Notify.Notification.new(m['source_name'], | |
m['message'], ICON) | |
logger.debug("Display message %s." % m['id']) | |
n.show() | |
logger.debug("Sleeping...") | |
time.sleep(INTERVAL) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment