From f75d321b200217514cde901cc15cc2b798e3dcfe Mon Sep 17 00:00:00 2001 From: Frederic Junod Date: Fri, 27 Oct 2017 14:52:46 +0200 Subject: [PATCH] Add new hipchat module Displays the unread messages count for an HipChat user --- bumblebee/modules/hipchat.py | 55 ++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 bumblebee/modules/hipchat.py diff --git a/bumblebee/modules/hipchat.py b/bumblebee/modules/hipchat.py new file mode 100644 index 0000000..b360d2b --- /dev/null +++ b/bumblebee/modules/hipchat.py @@ -0,0 +1,55 @@ +"""Displays the unread messages count for an HipChat user + +Requires the following library: + * requests + +Parameters: + * hipchat.token: HipChat user access token, the token needs to have the 'View Messages' scope. + * hipchat.interval: Refresh interval in minutes (defaults to 5) +""" + +import time +import functools +import bumblebee.input +import bumblebee.output +import bumblebee.engine + +try: + import requests +except ImportError: + pass + +HIPCHAT_API_URL = "https://www.hipchat.com/v2/readstate?expand=items.unreadCount" + +class Module(bumblebee.engine.Module): + def __init__(self, engine, config): + super(Module, self).__init__(engine, config, + bumblebee.output.Widget(full_text=self.output) + ) + self._count = 0 + self._interval = int(self.parameter("interval", "5")) + self._nextcheck = 0 + + self._requests = requests.Session() + self._requests.headers.update({"Authorization":"Bearer {}".format(self.parameter("token", ""))}) + + immediate_update = functools.partial(self.update, immediate=True) + engine.input.register_callback(self, button=bumblebee.input.RIGHT_MOUSE, cmd=immediate_update) + + def output(self, _): + return str(self._count) + + def update(self, _, immediate=False): + if immediate or self._nextcheck < int(time.time()): + self._nextcheck = int(time.time()) + self._interval * 60 + + try: + self._count = 0 + items = self._requests.get(HIPCHAT_API_URL).json().get('items') + self._count = sum([item.get('unreadCount').get('count') for item in items]) + + except Exception: + self._count = "n/a" + + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4