diff --git a/api.py b/api.py index 57d9d1a..48bc2c7 100644 --- a/api.py +++ b/api.py @@ -4,7 +4,7 @@ import time import datetime -def getData(phase,room,month): +def getDataObject(phase,room,month): regexRowUsage = '(.*)(.*)(.*)(.*)' regexUsage = 'Total combiné:(.*)' @@ -56,5 +56,9 @@ def getData(phase,room,month): data['left']=left data['maximum']=maximum - dthandler = lambda obj: obj.isoformat() if isinstance(obj, datetime.datetime) else None - return json.dumps(data,default=dthandler) \ No newline at end of file + return data + +def getData(phase,room,month): + data = getDataObject(phase,room,month) + dthandler = lambda obj: obj.isoformat() if isinstance(obj, datetime.datetime) else None + return json.dumps(data,default=dthandler) \ No newline at end of file diff --git a/netusage.py b/netusage.py index 1ba7793..fdddc9e 100644 --- a/netusage.py +++ b/netusage.py @@ -19,16 +19,18 @@ def getUsage(type,phase,room): room must be an existing room in the block """ - return api.getData(phase,room,datetime.now().month) - + data = api.getDataObject(phase,room,datetime.now().month) + pct = (data["maximum"]-data["left"])*100/data["maximum"] if type == "percent": return "{:0.2f}%".format(pct) if type == "left": - return "{:0.2f}GB".format(left) - if type =="usage": - return "{:0.2f}GB".format(usage/1024) - if type =="all": - return "Used :\t\t{:0.2f}GB ({:0.2f}%)\nLeft :\t\t{:0.2f}GB ({:0.2f}%)\nTotal :\t\t{:0.2f}GB".format(usage/1024,pct,left,100-pct,max/1024) + return "{:0.2f}GB".format(data["left"]) + if type == "usage": + return "{:0.2f}GB".format(data["usage"]) + if type == "json": + return api.getData(phase,room,datetime.now().month) + if type =="all": + return "Used :\t\t{:0.2f}GB ({:0.2f}%)\nLeft :\t\t{:0.2f}GB ({:0.2f}%)\nTotal :\t\t{:0.2f}GB".format(data["usage"],pct,data["left"],100-pct,data["maximum"]) raise Exception('Must choose between "percent" and "left" ')