Merge remote-tracking branch 'origin/development'

This commit is contained in:
tobi-wan-kenobi 2020-05-29 11:11:56 +02:00
commit 7f7f464193

View file

@ -1,6 +1,9 @@
# pylint: disable=C0111,R0903
"""Displays the unread GitHub notifications for a GitHub user
"""
Displays the unread GitHub notifications count for a GitHub user using the following reasons:
* https://developer.github.com/v3/activity/notifications/#notification-reasons
Requires the following library:
* requests
@ -8,8 +11,11 @@ Requires the following library:
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.
* github.reasons: Comma separated reasons to be parsed (e.g.: github.reasons=mention,team_mention,review_requested)
contributed by `yvesh <https://github.com/yvesh>`_ - many thanks!
contributed by:
* v1 - `yvesh <https://github.com/yvesh>`_ - many thanks!
* v2 - `cristianmiranda <https://github.com/cristianmiranda>`_ - many thanks!
"""
import shutil
@ -20,6 +26,8 @@ import core.widget
import core.decorators
import core.input
import util.format
class Module(core.module.Module):
@core.decorators.every(minutes=5)
@ -27,11 +35,17 @@ class Module(core.module.Module):
super().__init__(config, theme, core.widget.Widget(self.github))
self.__count = 0
self.__label = ""
self.__requests = requests.Session()
self.__requests.headers.update(
{"Authorization": "token {}".format(self.parameter("token", ""))}
)
self.__reasons = []
reasons = self.parameter("reasons", "")
if reasons:
self.__reasons = util.format.aslist(reasons)
cmd = "xdg-open"
if not shutil.which(cmd):
cmd = "x-www-browser"
@ -41,33 +55,61 @@ class Module(core.module.Module):
button=core.input.LEFT_MOUSE,
cmd="{} https://github.com/notifications".format(cmd),
)
core.input.register(self, button=core.input.RIGHT_MOUSE, cmd=self.update)
def github(self, _):
return str(self.__count)
return str(self.__label)
def update(self):
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
notifications = self.__requests.get(url)
except Exception:
self.__count = "n/a"
total = self.__getTotalUnreadNotificationsCount(notifications)
self.__count = total
self.__label = str(total)
counts = []
if len(self.__reasons) > 0:
for reason in self.__reasons:
unread = self.__getUnreadNotificationsCountByReason(
notifications, reason
)
counts.append(str(unread))
self.__label += " - "
self.__label += "/".join(counts)
except Exception as err:
print(err)
self.__label = "n/a"
def __getUnreadNotificationsCountByReason(self, notifications, reason):
return len(
list(
filter(
lambda notification: notification["unread"]
and notification["reason"] == reason,
notifications.json(),
)
)
)
def __getTotalUnreadNotificationsCount(self, notifications):
return len(
list(
filter(
lambda notification: notification["unread"], notifications.json()
)
)
)
def state(self, widget):
state = []
if self.__count > 0:
state.append("warning")
return state
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4