From 710342d7d417212e777e29e06e667a0a9fa78d4f Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Fri, 26 May 2017 13:32:47 +0200 Subject: [PATCH] [modules/github] Minor refactoring * Use requests library instead of curl * Exactly parse the github API responses * Return a string value in order to pass unit tests see #86 --- bumblebee/modules/github.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/bumblebee/modules/github.py b/bumblebee/modules/github.py index 7e0412c..187d266 100644 --- a/bumblebee/modules/github.py +++ b/bumblebee/modules/github.py @@ -15,6 +15,13 @@ import bumblebee.output import bumblebee.engine import re import time +import json + +try: + import requests + from requests.exceptions import RequestException +except ImportError: + pass class Module(bumblebee.engine.Module): def __init__(self, engine, config): @@ -26,7 +33,7 @@ class Module(bumblebee.engine.Module): self._nextcheck = 0 def github(self, widget): - return self._count + return str(self._count) def update(self, widgets): if self._nextcheck < int(time.time()): @@ -37,11 +44,11 @@ class Module(bumblebee.engine.Module): self._count = 0 return - result = bumblebee.util.execute("curl -s https://api.github.com/notifications\?access_token\=" + token) - - pattern = 'unread' - lines = '\n'.join(re.findall(r'^.*%s.*?$'%pattern,result,flags=re.M)) - - self._count = len(lines.split('\n')) + notifications = requests.get("https://api.github.com/notifications?access_token={}".format(token)).text + unread = 0 + for notification in json.loads(notifications): + if "unread" in notification and notification["unread"] == True: + unread += 1 + self._count = unread # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4