def get_gtfs_rt_data_feed( self, params: Optional[GTFSRTParams] = None ) -> Union[FeedMessage, APIError]: """ Returns a FeedMessage of vehicles currently providing Automatic Vehicle Locations in BODS. Args: bounding_box: Limit vehicles to those within the BoundingBox. route_id: Limit vehicles to those with this route id. start_time_after: Limit vehicles to those with start time after this datetime. start_time_before: Limit vehicles to with the start time before this datetime. """ if params is None: params = GTFSRTParams() params = json.loads(params.json(by_alias=True, exclude_none=True)) response = self._make_request(self.gtfs_rt_endpoint, params=params) if response.status_code == HTTPStatus.OK: message = FeedMessage() message.ParseFromString(response.content) return message return APIError(status_code=response.status_code, reason=response.content)
def get_gtfs_rt_from_archive(self) -> Union[FeedMessage, APIError]: """ Returns a FeedMessage of vehicles currently providing Automatic Vehicle Locations bulk download URL in BODS. """ response = self._make_request(self.gtfs_rt_zip_endpoint) if response.status_code == HTTPStatus.OK: with zipfile.ZipFile(io.BytesIO(response.content)) as zf: with zf.open("gtfsrt.bin") as f: message = FeedMessage() message.ParseFromString(f.read()) return message return APIError(status_code=response.status_code, reason=response.content)
def restore_feed_from_redis(self) -> None: _raw = self.redis_server.hget("realtime:feeds", self.id_) if not _raw: return _feed = FeedMessage() try: _feed.ParseFromString(_raw) self.latest_feed = _feed self.latest_timestamp = _feed.header.timestamp except (DecodeError, SystemError, RuntimeWarning) as err: u.log.error( "%s: unable to parse feed %s restored from redis", err, self.id_, )