2017-05-26 11:06:20 +02:00
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
2017-05-26 11:11:58 +02:00
|
|
|
"""Displays the unread GitHub notifications for a GitHub user
|
2017-05-26 11:06:20 +02:00
|
|
|
|
2017-07-07 17:25:00 +02:00
|
|
|
Requires the following library:
|
|
|
|
* requests
|
2017-05-26 11:06:20 +02:00
|
|
|
|
|
|
|
Parameters:
|
2017-07-08 08:27:59 +02:00
|
|
|
* github.token: GitHub user access token, the token needs to have the 'notifications' scope.
|
2017-07-12 16:06:17 +02:00
|
|
|
The value of the token can also be set using a 'GITHUB_TOKEN' environment variable.
|
2017-05-26 11:06:20 +02:00
|
|
|
* github.interval: Interval in minutes
|
|
|
|
"""
|
|
|
|
|
2017-07-12 16:06:17 +02:00
|
|
|
import os
|
2017-06-05 04:50:22 +02:00
|
|
|
import time
|
2017-07-08 08:22:52 +02:00
|
|
|
import functools
|
2017-05-26 11:06:20 +02:00
|
|
|
import bumblebee.input
|
|
|
|
import bumblebee.output
|
|
|
|
import bumblebee.engine
|
2017-05-26 13:32:47 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
import requests
|
|
|
|
except ImportError:
|
|
|
|
pass
|
2017-05-26 11:06:20 +02:00
|
|
|
|
|
|
|
class Module(bumblebee.engine.Module):
|
|
|
|
def __init__(self, engine, config):
|
|
|
|
super(Module, self).__init__(engine, config,
|
2017-06-05 04:50:22 +02:00
|
|
|
bumblebee.output.Widget(full_text=self.github)
|
|
|
|
)
|
2017-05-26 11:06:20 +02:00
|
|
|
self._count = 0
|
|
|
|
self._interval = int(self.parameter("interval", "5"))
|
|
|
|
self._nextcheck = 0
|
2017-07-07 06:56:16 +02:00
|
|
|
engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE,
|
|
|
|
cmd="x-www-browser https://github.com/notifications")
|
2017-07-08 08:22:52 +02:00
|
|
|
immediate_update = functools.partial(self.update, immediate=True)
|
|
|
|
engine.input.register_callback(self, button=bumblebee.input.RIGHT_MOUSE,
|
|
|
|
cmd=immediate_update)
|
2017-05-26 11:06:20 +02:00
|
|
|
|
2017-06-05 04:50:22 +02:00
|
|
|
def github(self, _):
|
2017-05-26 13:32:47 +02:00
|
|
|
return str(self._count)
|
2017-05-26 11:06:20 +02:00
|
|
|
|
2017-07-08 08:22:52 +02:00
|
|
|
def update(self, _, immediate=False):
|
|
|
|
if immediate or self._nextcheck < int(time.time()):
|
2017-05-26 11:06:20 +02:00
|
|
|
self._nextcheck = int(time.time()) + self._interval * 60
|
2017-07-12 16:06:17 +02:00
|
|
|
token = self.parameter("token", os.environ.get("GITHUB_TOKEN"))
|
2017-05-26 11:06:20 +02:00
|
|
|
|
|
|
|
if not token:
|
2017-07-06 13:47:32 +02:00
|
|
|
self._count = "n/a"
|
2017-06-05 04:50:22 +02:00
|
|
|
return
|
2017-06-05 10:56:28 +02:00
|
|
|
|
2017-06-10 13:59:44 +02:00
|
|
|
try:
|
2017-07-07 06:46:10 +02:00
|
|
|
notifications = requests.get("https://api.github.com/notifications", headers={"Authorization":"token {}".format(token)}).json()
|
|
|
|
self._count = len(filter(lambda notification: notification.get("unread", False), notifications))
|
2017-06-10 13:59:44 +02:00
|
|
|
except Exception:
|
|
|
|
self._count = "n/a"
|
|
|
|
|
2017-05-26 11:06:20 +02:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|