From f7e37ef7d73ebd1bf110b1ab008faaf25447fa62 Mon Sep 17 00:00:00 2001 From: Cristian Miranda Date: Thu, 28 May 2020 15:59:39 -0300 Subject: [PATCH 1/2] [modules/github] - Removed legacy module --- bumblebee_status/modules/contrib/github.py | 73 ---------------------- 1 file changed, 73 deletions(-) delete mode 100644 bumblebee_status/modules/contrib/github.py diff --git a/bumblebee_status/modules/contrib/github.py b/bumblebee_status/modules/contrib/github.py deleted file mode 100644 index 8c01afd..0000000 --- a/bumblebee_status/modules/contrib/github.py +++ /dev/null @@ -1,73 +0,0 @@ -# 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. - -contributed by `yvesh `_ - many thanks! -""" - -import shutil -import requests - -import core.module -import core.widget -import core.decorators -import core.input - - -class Module(core.module.Module): - @core.decorators.every(minutes=5) - def __init__(self, config, theme): - super().__init__(config, theme, core.widget.Widget(self.github)) - - self.__count = 0 - self.__requests = requests.Session() - self.__requests.headers.update( - {"Authorization": "token {}".format(self.parameter("token", ""))} - ) - - cmd = "xdg-open" - if not shutil.which(cmd): - cmd = "x-www-browser" - - core.input.register( - self, - 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) - - 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 - - except Exception: - self.__count = "n/a" - - -# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 From c2d869278a83bb64c1d0578efd3d22d12b4c93a2 Mon Sep 17 00:00:00 2001 From: Cristian Miranda Date: Thu, 28 May 2020 16:00:36 -0300 Subject: [PATCH 2/2] [modules/github] - New module supporting unread notifications count by reason with backward compatibility --- bumblebee_status/modules/contrib/github.py | 115 +++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 bumblebee_status/modules/contrib/github.py diff --git a/bumblebee_status/modules/contrib/github.py b/bumblebee_status/modules/contrib/github.py new file mode 100644 index 0000000..5422ee6 --- /dev/null +++ b/bumblebee_status/modules/contrib/github.py @@ -0,0 +1,115 @@ +# pylint: disable=C0111,R0903 + +""" +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 + +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: + * v1 - `yvesh `_ - many thanks! + * v2 - `cristianmiranda `_ - many thanks! +""" + +import shutil +import requests + +import core.module +import core.widget +import core.decorators +import core.input + +import util.format + + +class Module(core.module.Module): + @core.decorators.every(minutes=5) + def __init__(self, config, theme): + 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" + + core.input.register( + self, + button=core.input.LEFT_MOUSE, + cmd="{} https://github.com/notifications".format(cmd), + ) + + def github(self, _): + return str(self.__label) + + def update(self): + try: + url = "https://api.github.com/notifications" + notifications = self.__requests.get(url) + + 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