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
|
|
|
|
|
|
|
Requires the following executable:
|
|
|
|
* curl
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
* github.token: GitHub user access token
|
|
|
|
* github.interval: Interval in minutes
|
|
|
|
"""
|
|
|
|
|
2017-06-05 04:50:22 +02:00
|
|
|
import time
|
|
|
|
import json
|
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-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
|
|
|
|
|
|
|
def update(self, widgets):
|
|
|
|
if self._nextcheck < int(time.time()):
|
|
|
|
self._nextcheck = int(time.time()) + self._interval * 60
|
|
|
|
token = self.parameter("token", "")
|
|
|
|
|
|
|
|
if not token:
|
2017-06-05 04:50:22 +02:00
|
|
|
self._count = 0
|
|
|
|
return
|
2017-06-05 10:56:28 +02:00
|
|
|
|
|
|
|
notifications = requests.get("https://api.github.com/notifications", headers={"Authorization":"token {}".format(token)}).text
|
2017-05-26 13:32:47 +02:00
|
|
|
unread = 0
|
2017-06-10 13:59:44 +02:00
|
|
|
try:
|
|
|
|
for notification in json.loads(notifications):
|
|
|
|
if "unread" in notification and notification["unread"]:
|
|
|
|
unread += 1
|
|
|
|
self._count = unread
|
|
|
|
except Exception:
|
|
|
|
self._count = "n/a"
|
|
|
|
|
2017-05-26 11:06:20 +02:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|