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-05-26 11:06:20 +02:00
|
|
|
* github.interval: Interval in minutes
|
|
|
|
"""
|
|
|
|
|
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-25 06:52:28 +02:00
|
|
|
self._requests = requests.Session()
|
|
|
|
self._requests.headers.update({"Authorization":"token {}".format(self.parameter("token", ""))})
|
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-06-10 13:59:44 +02:00
|
|
|
try:
|
2017-07-25 06:52:28 +02:00
|
|
|
self._count = 0
|
|
|
|
url = "https://api.github.com/notifications"
|
|
|
|
while True:
|
|
|
|
notifications = self._requests.get(url)
|
2017-09-18 00:46:40 +02:00
|
|
|
self._count += len(list(filter(lambda notification: notification['unread'], notifications.json())))
|
2017-07-25 06:52:28 +02:00
|
|
|
next_link = notifications.links.get('next')
|
|
|
|
if next_link is not None:
|
|
|
|
url = next_link.get('url')
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
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
|