def update(self): """Get the latest data from rejseplanen.""" import rjpl self.info = [] try: results = rjpl.departureBoard(int(self.stop_id), timeout=5) except rjpl.rjplAPIError as error: _LOGGER.debug("API returned error: %s", error) self.info = self.empty_result() return except (rjpl.rjplConnectionError, rjpl.rjplHTTPError): _LOGGER.debug("Error occured while connecting to the API") self.info = self.empty_result() return # Filter result results = [d for d in results if 'cancelled' not in d] if self.route: results = [d for d in results if d['name'] in self.route] if self.direction: results = [d for d in results if d['direction'] in self.direction] if self.departure_type: results = [d for d in results if d['type'] in self.departure_type] for item in results: route = item.get('name') due_at_date = item.get('rtDate') due_at_time = item.get('rtTime') if due_at_date is None: due_at_date = item.get('date') # Scheduled date if due_at_time is None: due_at_time = item.get('time') # Scheduled time if (due_at_date is not None and due_at_time is not None and route is not None): due_at = '{} {}'.format(due_at_date, due_at_time) departure_data = { ATTR_DUE_IN: due_in_minutes(due_at), ATTR_DUE_AT: due_at, ATTR_TYPE: item.get('type'), ATTR_ROUTE: route, ATTR_DIRECTION: item.get('direction'), ATTR_STOP_NAME: item.get('stop') } self.info.append(departure_data) if not self.info: _LOGGER.debug("No departures with given parameters") self.info = self.empty_result() # Sort the data by time self.info = sorted(self.info, key=itemgetter(ATTR_DUE_IN))
def update(self): """Get the latest data from rejseplanen.""" import rjpl self.info = [] try: results = rjpl.departureBoard(int(self.stop_id), timeout=5) except rjpl.rjplAPIError as error: _LOGGER.debug("API returned error: %s", error) self.info = self.empty_result() return except (rjpl.rjplConnectionError, rjpl.rjplHTTPError): _LOGGER.debug("Error occured while connecting to the API") self.info = self.empty_result() return # Filter result results = [d for d in results if 'cancelled' not in d] if self.route: results = [d for d in results if d['name'] in self.route] if self.direction: results = [d for d in results if d['direction'] in self.direction] if self.departure_type: results = [d for d in results if d['type'] in self.departure_type] for item in results: route = item.get('name') due_at_date = item.get('rtDate') due_at_time = item.get('rtTime') if due_at_date is None: due_at_date = item.get('date') # Scheduled date if due_at_time is None: due_at_time = item.get('time') # Scheduled time if (due_at_date is not None and due_at_time is not None and route is not None): due_at = '{} {}'.format(due_at_date, due_at_time) departure_data = {ATTR_DUE_IN: due_in_minutes(due_at), ATTR_DUE_AT: due_at, ATTR_TYPE: item.get('type'), ATTR_ROUTE: route, ATTR_DIRECTION: item.get('direction'), ATTR_STOP_NAME: item.get('stop')} self.info.append(departure_data) if not self.info: _LOGGER.debug("No departures with given parameters") self.info = self.empty_result() # Sort the data by time self.info = sorted(self.info, key=itemgetter(ATTR_DUE_IN))
def update(self): """Get the latest data from rejseplanen.""" self.info = [] def intersection(lst1, lst2): """Return items contained in both lists.""" return list(set(lst1) & set(lst2)) # Limit search to selected types, to get more results all_types = not bool(self.departure_type) use_train = all_types or bool(intersection(TRAIN_TYPES, self.departure_type)) use_bus = all_types or bool(intersection(BUS_TYPES, self.departure_type)) use_metro = all_types or bool(intersection(METRO_TYPES, self.departure_type)) try: results = rjpl.departureBoard( int(self.stop_id), timeout=5, useTrain=use_train, useBus=use_bus, useMetro=use_metro, ) except rjpl.rjplAPIError as error: _LOGGER.debug("API returned error: %s", error) return except (rjpl.rjplConnectionError, rjpl.rjplHTTPError): _LOGGER.debug("Error occurred while connecting to the API") return # Filter result results = [d for d in results if "cancelled" not in d] if self.route: results = [d for d in results if d["name"] in self.route] if self.direction: results = [d for d in results if d["direction"] in self.direction] if self.departure_type: results = [d for d in results if d["type"] in self.departure_type] for item in results: route = item.get("name") scheduled_date = item.get("date") scheduled_time = item.get("time") real_time_date = due_at_date = item.get("rtDate") real_time_time = due_at_time = item.get("rtTime") if due_at_date is None: due_at_date = scheduled_date if due_at_time is None: due_at_time = scheduled_time if ( due_at_date is not None and due_at_time is not None and route is not None ): due_at = f"{due_at_date} {due_at_time}" scheduled_at = f"{scheduled_date} {scheduled_time}" departure_data = { ATTR_DIRECTION: item.get("direction"), ATTR_DUE_IN: due_in_minutes(due_at), ATTR_DUE_AT: due_at, ATTR_FINAL_STOP: item.get("finalStop"), ATTR_ROUTE: route, ATTR_SCHEDULED_AT: scheduled_at, ATTR_STOP_NAME: item.get("stop"), ATTR_TYPE: item.get("type"), } if real_time_date is not None and real_time_time is not None: departure_data[ ATTR_REAL_TIME_AT ] = f"{real_time_date} {real_time_time}" if item.get("rtTrack") is not None: departure_data[ATTR_TRACK] = item.get("rtTrack") self.info.append(departure_data) if not self.info: _LOGGER.debug("No departures with given parameters") # Sort the data by time self.info = sorted(self.info, key=itemgetter(ATTR_DUE_IN))
def update(self): """Get the latest data from rejseplanen.""" import rjpl self.info = [] def intersection(lst1, lst2): """Return items contained in both lists""" return list(set(lst1) & set(lst2)) # Limit search to selected types, to get more results all_types = not bool(self.departure_type) use_train = all_types or bool( intersection(TRAIN_TYPES, self.departure_type)) use_bus = all_types or bool( intersection(BUS_TYPES, self.departure_type)) use_metro = all_types or bool( intersection(METRO_TYPES, self.departure_type)) try: results = rjpl.departureBoard(int(self.stop_id), timeout=5, useTrain=use_train, useBus=use_bus, useMetro=use_metro) except rjpl.rjplAPIError as error: _LOGGER.debug("API returned error: %s", error) self.info = self.empty_result() return except (rjpl.rjplConnectionError, rjpl.rjplHTTPError): _LOGGER.debug("Error occured while connecting to the API") self.info = self.empty_result() return # Filter result results = [d for d in results if 'cancelled' not in d] if self.route: results = [d for d in results if d['name'] in self.route] if self.direction: results = [d for d in results if d['direction'] in self.direction] if self.departure_type: results = [d for d in results if d['type'] in self.departure_type] for item in results: route = item.get('name') due_at_date = item.get('rtDate') due_at_time = item.get('rtTime') if due_at_date is None: due_at_date = item.get('date') # Scheduled date if due_at_time is None: due_at_time = item.get('time') # Scheduled time if (due_at_date is not None and due_at_time is not None and route is not None): due_at = '{} {}'.format(due_at_date, due_at_time) departure_data = { ATTR_DUE_IN: due_in_minutes(due_at), ATTR_DUE_AT: due_at, ATTR_TYPE: item.get('type'), ATTR_ROUTE: route, ATTR_DIRECTION: item.get('direction'), ATTR_STOP_NAME: item.get('stop') } self.info.append(departure_data) if not self.info: _LOGGER.debug("No departures with given parameters") self.info = self.empty_result() # Sort the data by time self.info = sorted(self.info, key=itemgetter(ATTR_DUE_IN))