[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
This commit is contained in:
Tobias Witek 2017-05-26 13:32:47 +02:00
parent 320b4b466e
commit 710342d7d4

View file

@ -15,6 +15,13 @@ import bumblebee.output
import bumblebee.engine import bumblebee.engine
import re import re
import time import time
import json
try:
import requests
from requests.exceptions import RequestException
except ImportError:
pass
class Module(bumblebee.engine.Module): class Module(bumblebee.engine.Module):
def __init__(self, engine, config): def __init__(self, engine, config):
@ -26,7 +33,7 @@ class Module(bumblebee.engine.Module):
self._nextcheck = 0 self._nextcheck = 0
def github(self, widget): def github(self, widget):
return self._count return str(self._count)
def update(self, widgets): def update(self, widgets):
if self._nextcheck < int(time.time()): if self._nextcheck < int(time.time()):
@ -37,11 +44,11 @@ class Module(bumblebee.engine.Module):
self._count = 0 self._count = 0
return return
result = bumblebee.util.execute("curl -s https://api.github.com/notifications\?access_token\=" + token) notifications = requests.get("https://api.github.com/notifications?access_token={}".format(token)).text
unread = 0
pattern = 'unread' for notification in json.loads(notifications):
lines = '\n'.join(re.findall(r'^.*%s.*?$'%pattern,result,flags=re.M)) if "unread" in notification and notification["unread"] == True:
unread += 1
self._count = len(lines.split('\n')) self._count = unread
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4