From 680e66fc1bdf74fbfd7b748a56d2d8660585034d Mon Sep 17 00:00:00 2001 From: Fredrik Baberg Date: Sat, 29 Sep 2018 19:42:21 +0200 Subject: [PATCH 1/2] Minimal change, log error message if API call fails. --- sl.py | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/sl.py b/sl.py index 65d8cdd..a744bb6 100755 --- a/sl.py +++ b/sl.py @@ -152,21 +152,24 @@ def update(self): self._data.update() board = [] - for i,traffictype in enumerate(['Metros','Buses','Trains','Trams', 'Ships']): - for idx, value in enumerate(self._data.data['ResponseData'][traffictype]): - direction = value['JourneyDirection'] or 0 - displaytime = value['DisplayTime'] or '' - destination = value['Destination'] or '' - linenumber = value['LineNumber'] or '' - - if (int(self._data._direction) == 0 or int(direction) == int(self._data._direction)): - if(self._data._lines is None or (linenumber in self._data._lines)): - diff = self.parseDepartureTime(displaytime) - board.append({"line":linenumber,"departure":displaytime,"destination":destination, 'time': diff}) + if self._data.data['StatusCode'] != 0: + _LOGGER.error("Status code: {}, {}".format(self._data.data['StatusCode'], self._data.data['Message'])) + else: + for i,traffictype in enumerate(['Metros','Buses','Trains','Trams', 'Ships']): + for idx, value in enumerate(self._data.data['ResponseData'][traffictype]): + direction = value['JourneyDirection'] or 0 + displaytime = value['DisplayTime'] or '' + destination = value['Destination'] or '' + linenumber = value['LineNumber'] or '' + + if (int(self._data._direction) == 0 or int(direction) == int(self._data._direction)): + if(self._data._lines is None or (linenumber in self._data._lines)): + diff = self.parseDepartureTime(displaytime) + board.append({"line":linenumber,"departure":displaytime,"destination":destination, 'time': diff}) self._board = sorted(board, key=lambda k: k['time']) - _LOGGER.info(self._board) + _LOGGER.debug(self._board) class SlDepartureBoardData(object): @@ -183,7 +186,7 @@ def __init__(self, apikey, siteid, lines, direction): def update(self, **kwargs): """Get the latest data for this site from the API.""" try: - _LOGGER.info("fetching SL Data for '%s'", self._siteid) + _LOGGER.debug("fetching SL Data for '%s'", self._siteid) url = "https://api.sl.se/api2/realtimedeparturesV4.json?key={}&siteid={}". \ format(self._apikey, self._siteid) From 7426dd44a9b298c784f6695f13f9c0c3a0006756 Mon Sep 17 00:00:00 2001 From: Fredrik Baberg Date: Sat, 29 Sep 2018 19:42:21 +0200 Subject: [PATCH 2/2] Minimal change, log error message if API call fails. --- sl.py | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/sl.py b/sl.py index 65d8cdd..a744bb6 100755 --- a/sl.py +++ b/sl.py @@ -152,21 +152,24 @@ def update(self): self._data.update() board = [] - for i,traffictype in enumerate(['Metros','Buses','Trains','Trams', 'Ships']): - for idx, value in enumerate(self._data.data['ResponseData'][traffictype]): - direction = value['JourneyDirection'] or 0 - displaytime = value['DisplayTime'] or '' - destination = value['Destination'] or '' - linenumber = value['LineNumber'] or '' - - if (int(self._data._direction) == 0 or int(direction) == int(self._data._direction)): - if(self._data._lines is None or (linenumber in self._data._lines)): - diff = self.parseDepartureTime(displaytime) - board.append({"line":linenumber,"departure":displaytime,"destination":destination, 'time': diff}) + if self._data.data['StatusCode'] != 0: + _LOGGER.error("Status code: {}, {}".format(self._data.data['StatusCode'], self._data.data['Message'])) + else: + for i,traffictype in enumerate(['Metros','Buses','Trains','Trams', 'Ships']): + for idx, value in enumerate(self._data.data['ResponseData'][traffictype]): + direction = value['JourneyDirection'] or 0 + displaytime = value['DisplayTime'] or '' + destination = value['Destination'] or '' + linenumber = value['LineNumber'] or '' + + if (int(self._data._direction) == 0 or int(direction) == int(self._data._direction)): + if(self._data._lines is None or (linenumber in self._data._lines)): + diff = self.parseDepartureTime(displaytime) + board.append({"line":linenumber,"departure":displaytime,"destination":destination, 'time': diff}) self._board = sorted(board, key=lambda k: k['time']) - _LOGGER.info(self._board) + _LOGGER.debug(self._board) class SlDepartureBoardData(object): @@ -183,7 +186,7 @@ def __init__(self, apikey, siteid, lines, direction): def update(self, **kwargs): """Get the latest data for this site from the API.""" try: - _LOGGER.info("fetching SL Data for '%s'", self._siteid) + _LOGGER.debug("fetching SL Data for '%s'", self._siteid) url = "https://api.sl.se/api2/realtimedeparturesV4.json?key={}&siteid={}". \ format(self._apikey, self._siteid)