2020-04-18 16:59:50 +02:00
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
|
|
|
"""Displays the unread GitHub notifications for a GitHub user
|
|
|
|
|
|
|
|
Requires the following library:
|
|
|
|
* requests
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
* github.token: GitHub user access token, the token needs to have the 'notifications' scope.
|
|
|
|
* github.interval: Interval in minutes between updates, default is 5.
|
2020-05-08 20:58:35 +02:00
|
|
|
|
|
|
|
contributed by `yvesh <https://github.com/yvesh>`_ - many thanks!
|
2020-04-18 16:59:50 +02:00
|
|
|
"""
|
|
|
|
|
2020-04-23 12:59:08 +02:00
|
|
|
import shutil
|
2020-04-18 17:05:19 +02:00
|
|
|
import requests
|
|
|
|
|
|
|
|
import core.module
|
|
|
|
import core.widget
|
|
|
|
import core.decorators
|
|
|
|
import core.input
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-04-18 17:05:19 +02:00
|
|
|
class Module(core.module.Module):
|
|
|
|
@core.decorators.every(minutes=5)
|
2020-04-26 16:39:24 +02:00
|
|
|
def __init__(self, config, theme):
|
|
|
|
super().__init__(config, theme, core.widget.Widget(self.github))
|
2020-04-18 17:05:19 +02:00
|
|
|
|
|
|
|
self.__count = 0
|
|
|
|
self.__requests = requests.Session()
|
2020-05-03 11:15:52 +02:00
|
|
|
self.__requests.headers.update(
|
|
|
|
{"Authorization": "token {}".format(self.parameter("token", ""))}
|
|
|
|
)
|
2020-04-18 17:05:19 +02:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
cmd = "xdg-open"
|
2020-04-23 12:59:08 +02:00
|
|
|
if not shutil.which(cmd):
|
2020-05-03 11:15:52 +02:00
|
|
|
cmd = "x-www-browser"
|
2020-04-23 12:59:08 +02:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
core.input.register(
|
|
|
|
self,
|
|
|
|
button=core.input.LEFT_MOUSE,
|
|
|
|
cmd="{} https://github.com/notifications".format(cmd),
|
|
|
|
)
|
2020-04-18 17:05:19 +02:00
|
|
|
core.input.register(self, button=core.input.RIGHT_MOUSE, cmd=self.update)
|
2020-04-18 16:59:50 +02:00
|
|
|
|
|
|
|
def github(self, _):
|
2020-04-18 17:05:19 +02:00
|
|
|
return str(self.__count)
|
2020-04-18 16:59:50 +02:00
|
|
|
|
2020-04-18 17:05:19 +02:00
|
|
|
def update(self):
|
2020-04-18 16:59:50 +02:00
|
|
|
try:
|
2020-04-18 17:05:19 +02:00
|
|
|
self.__count = 0
|
2020-05-03 11:15:52 +02:00
|
|
|
url = "https://api.github.com/notifications"
|
2020-04-18 16:59:50 +02:00
|
|
|
while True:
|
2020-04-18 17:05:19 +02:00
|
|
|
notifications = self.__requests.get(url)
|
2020-05-03 11:15:52 +02:00
|
|
|
self.__count += len(
|
|
|
|
list(
|
|
|
|
filter(
|
|
|
|
lambda notification: notification["unread"],
|
|
|
|
notifications.json(),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
next_link = notifications.links.get("next")
|
2020-04-18 16:59:50 +02:00
|
|
|
if next_link is not None:
|
2020-05-03 11:15:52 +02:00
|
|
|
url = next_link.get("url")
|
2020-04-18 16:59:50 +02:00
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
|
|
|
except Exception:
|
2020-05-03 11:15:52 +02:00
|
|
|
self.__count = "n/a"
|
|
|
|
|
2020-04-18 16:59:50 +02:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|