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.
|
2019-05-09 09:21:30 +02:00
|
|
|
* github.interval: Interval in minutes between updates, default is 5.
|
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
|
|
|
|
2020-04-23 07:48:23 +02:00
|
|
|
import bumblebee.util
|
|
|
|
|
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
|
2019-10-29 20:20:19 +01:00
|
|
|
self.interval_factor(60)
|
2018-01-07 20:27:11 +01:00
|
|
|
self.interval(5)
|
2017-07-25 06:52:28 +02:00
|
|
|
self._requests = requests.Session()
|
2020-04-23 07:48:23 +02:00
|
|
|
|
|
|
|
cmd = "xdg-open"
|
|
|
|
if not bumblebee.util.which(cmd):
|
|
|
|
cmd = "x-www-browser"
|
2017-07-25 06:52:28 +02:00
|
|
|
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,
|
2020-04-23 07:48:23 +02:00
|
|
|
cmd="{} https://github.com/notifications".format(cmd))
|
2018-10-29 11:32:00 +01:00
|
|
|
engine.input.register_callback(self, button=bumblebee.input.RIGHT_MOUSE, cmd=self.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
|
|
|
|
2018-10-29 11:32:00 +01:00
|
|
|
def update(self, _):
|
2018-01-07 20:27:11 +01:00
|
|
|
try:
|
|
|
|
self._count = 0
|
|
|
|
url = "https://api.github.com/notifications"
|
|
|
|
while True:
|
|
|
|
notifications = self._requests.get(url)
|
|
|
|
self._count += len(list(filter(lambda notification: notification['unread'], notifications.json())))
|
|
|
|
next_link = notifications.links.get('next')
|
|
|
|
if next_link is not None:
|
|
|
|
url = next_link.get('url')
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
|
|
|
except Exception:
|
|
|
|
self._count = "n/a"
|
2017-06-10 13:59:44 +02:00
|
|
|
|
2017-05-26 11:06:20 +02:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|