def _get_real_time_passage(self, xml_journey): """ :return RealTimePassage: object real time passage :param xml_journey: journey information exceptions : ValueError: Unable to parse datetime, day is out of range for month (for example) """ dt = DateTimeFormat()(xml_journey.get('dateTime')) utc_dt = self.timezone.normalize(self.timezone.localize(dt)).astimezone(pytz.utc) passage = RealTimePassage(utc_dt) passage.is_real_time = xml_journey.get('realTime') == 'yes' return passage
def _get_passages(self, tree, ns, route_point): stop = route_point.fetch_stop_id(self.object_id_tag) line = route_point.fetch_line_id(self.object_id_tag) route = route_point.fetch_route_id(self.object_id_tag) next_passages = [] for visit in tree.findall('.//siri:MonitoredStopVisit', ns): cur_stop = visit.find('.//siri:StopPointRef', ns).text if stop != cur_stop: continue cur_line = visit.find('.//siri:LineRef', ns).text if line != cur_line: continue cur_route = visit.find('.//siri:DirectionName', ns).text if route != cur_route: continue # TODO? we should ignore MonitoredCall with a DepartureStatus set to "Cancelled" cur_destination = visit.find('.//siri:DestinationName', ns).text cur_dt = visit.find('.//siri:ExpectedDepartureTime', ns).text # TODO? fallback on siri:AimedDepartureTime if there is no ExpectedDepartureTime # In that case we may want to set realtime to False cur_dt = aniso8601.parse_datetime(cur_dt) next_passages.append(RealTimePassage(cur_dt, cur_destination)) return next_passages
def _get_passages(self, timeo_resp, current_dt, line_uri=None): logging.getLogger(__name__).debug( 'timeo response: {}'.format(timeo_resp), extra={'rt_system_id': unicode(self.rt_system_id)}) st_responses = timeo_resp.get('StopTimesResponse') # by construction there should be only one StopTimesResponse if not st_responses or len(st_responses) != 1: logging.getLogger(__name__).warning( 'invalid timeo response: {}'.format(timeo_resp), extra={'rt_system_id': unicode(self.rt_system_id)}) raise RealtimeProxyError('invalid response') next_st = st_responses[0]['NextStopTimesMessage'] next_passages = [] for next_expected_st in next_st.get('NextExpectedStopTime', []): # for the moment we handle only the NextStop and the direction dt = self._get_dt(next_expected_st['NextStop'], current_dt) direction = self._get_direction_name( line_uri=line_uri, object_code=next_expected_st.get('Terminus'), default_value=next_expected_st.get('Destination')) next_passage = RealTimePassage(dt, direction) next_passages.append(next_passage) return next_passages
def _get_passages(self, route_point, resp): logging.getLogger(__name__).debug('sirilite response: {}'.format(resp)) line_code = route_point.fetch_line_id(self.object_id_tag) monitored_stop_visit = resp.get('siri', {})\ .get('serviceDelivery', {})\ .get('stopMonitoringDelivery', {})\ .get('monitoredStopVisit', []) schedules = (vj for vj in monitored_stop_visit if vj.get('monitoredVehicleJourney', {}).get( 'lineRef', {}).get('value', '') == line_code) #TODO: we should use the destination to find the correct route if schedules: next_passages = [] for next_expected_st in schedules: # for the moment we handle only the NextStop and the direction dt = self._get_dt(next_expected_st['monitoredVehicleJourney'] ['monitoredCall']['expectedDepartureTime']) direction = next_expected_st.get('destinationName', {}).get('value') is_real_time = True next_passage = RealTimePassage(dt, direction, is_real_time) next_passages.append(next_passage) return next_passages else: return None
def _get_passages(self, xml, route_point): ns = {'siri': 'http://www.siri.org.uk/siri'} try: root = et.fromstring(xml) except et.ParseError as e: logging.getLogger(__name__).exception("invalid xml") raise RealtimeProxyError('invalid xml') stop = route_point.fetch_stop_id(self.object_id_tag) line = route_point.fetch_line_id(self.object_id_tag) route = route_point.fetch_route_id(self.object_id_tag) next_passages = [] for visit in root.findall('.//siri:MonitoredStopVisit', ns): cur_stop = visit.find('.//siri:StopPointRef', ns).text if stop != cur_stop: continue cur_line = visit.find('.//siri:LineRef', ns).text if line != cur_line: continue cur_route = visit.find('.//siri:DirectionName', ns).text if route != cur_route: continue cur_destination = visit.find('.//siri:DestinationName', ns).text cur_dt = visit.find('.//siri:ExpectedDepartureTime', ns).text cur_dt = aniso8601.parse_datetime(cur_dt) next_passages.append(RealTimePassage(cur_dt, cur_destination)) return next_passages
def _get_passages(self, route_point, resp): logging.getLogger(__name__).debug( 'sytralrt response: {}'.format(resp), extra={'rt_system_id': six.text_type(self.rt_system_id)}) # One line navitia can be multiple lines on the SAE side line_ids = route_point.fetch_all_line_id(self.object_id_tag) line_uri = route_point.fetch_line_uri() departures = resp.get('departures', []) next_passages = [] for next_expected_st in departures: if next_expected_st['line'] not in line_ids: continue dt = self._get_dt(next_expected_st['datetime']) direction_name = next_expected_st.get('direction_name') is_real_time = next_expected_st.get('type') == 'E' direction = self._get_direction( line_uri=line_uri, object_code=next_expected_st.get('direction'), default_value=direction_name) next_passage = RealTimePassage(dt, direction_name, is_real_time, direction.uri) next_passages.append(next_passage) return next_passages
def _create_next_passages(passages): next_passages = [] for next_expected_st, direction in passages: t = datetime.datetime.strptime(next_expected_st, "%H:%M:%S") dt = datetime.datetime(year=2016, month=1, day=2, hour=t.hour, minute=t.minute, second=t.second, tzinfo=pytz.UTC) next_passage = RealTimePassage(dt, direction) next_passages.append(next_passage) return next_passages
def _get_passages(self, route_point, resp): logging.getLogger(__name__).debug( 'sytralrt response: {}'.format(resp), extra={'rt_system_id': unicode(self.rt_system_id)}) line_code = route_point.fetch_line_id(self.object_id_tag) departures = resp.get('departures', []) next_passages = [] for next_expected_st in departures: if line_code != next_expected_st['line']: continue dt = self._get_dt(next_expected_st['datetime']) direction = next_expected_st.get('direction_name') is_real_time = next_expected_st.get('type') == 'E' next_passage = RealTimePassage(dt, direction, is_real_time) next_passages.append(next_passage) return next_passages
def _get_passages(self, route_point, cleverage_resp): logging.getLogger(__name__).debug('cleverage response: {}'.format(cleverage_resp)) line_code = route_point.fetch_line_id(self.object_id_tag) schedules = next((line['schedules'] for line in cleverage_resp if line['code'].lower() == line_code.lower()), None) if schedules: next_passages = [] for next_expected_st in schedules: # for the moment we handle only the NextStop and the direction dt = self._get_dt(next_expected_st['departure']) direction = next_expected_st.get('destination_name') is_real_time = next_expected_st.get('realtime') == '1' next_passage = RealTimePassage(dt, direction, is_real_time) next_passages.append(next_passage) return next_passages else: return None
def _get_passages(self, timeo_resp): logging.getLogger(__name__).debug( 'timeo response: {}'.format(timeo_resp)) st_responses = timeo_resp.get('StopTimesResponse') # by construction there should be only one StopTimesResponse if not st_responses or len(st_responses) != 1: logging.getLogger(__name__).warning( 'invalid timeo response: {}'.format(timeo_resp)) return None next_st = st_responses[0]['NextStopTimesMessage'] next_passages = [] for next_expected_st in next_st.get('NextExpectedStopTime', []): # for the moment we handle only the NextStop and the direction dt = self._get_dt(next_expected_st['NextStop']) direction = next_expected_st.get('Destination') next_passage = RealTimePassage(dt, direction) next_passages.append(next_passage) return next_passages
def passage(dt_str, **kwargs): return RealTimePassage(dt(dt_str), **kwargs)
def _get_passages(self, response, current_dt, line_uri=None): status_code = response.status_code timeo_resp = response.json() logging.getLogger(__name__).debug( 'timeo response: {}'.format(timeo_resp), extra={'rt_system_id': six.text_type(self.rt_system_id)}) # Handling http error if status_code != 200: logging.getLogger(__name__).error( 'Timeo RT service unavailable, impossible to query : {}'. format(response.url), extra={ 'rt_system_id': six.text_type(self.rt_system_id), 'status_code': status_code }, ) raise RealtimeProxyError('non 200 response') # internal timeo error handling message_responses = timeo_resp.get('MessageResponse') for message_response in message_responses: if ('ResponseCode' in message_response and int(message_response['ResponseCode']) >= self.INTERNAL_TIMEO_ERROR_CODE_LIMIT): resp_code = message_response['ResponseCode'] if 'ResponseComment' in message_response: resp_comment = message_response['ResponseComment'] else: resp_comment = '' self.record_internal_failure( 'Timeo RT internal service error', 'ResponseCode: {} - ResponseComment: {}'.format( resp_code, resp_comment), ) timeo_internal_error_message = 'Timeo RT internal service error, ResponseCode: {} - ResponseComment: {}'.format( resp_code, resp_comment) logging.getLogger(__name__).error(timeo_internal_error_message) raise RealtimeProxyError(timeo_internal_error_message) st_responses = timeo_resp.get('StopTimesResponse') # by construction there should be only one StopTimesResponse if not st_responses or len(st_responses) != 1: logging.getLogger(__name__).warning( 'invalid timeo response: {}'.format(timeo_resp), extra={'rt_system_id': six.text_type(self.rt_system_id)}, ) raise RealtimeProxyError('invalid response') next_st = st_responses[0]['NextStopTimesMessage'] next_passages = [] for next_expected_st in next_st.get('NextExpectedStopTime', []): # for the moment we handle only the NextStop and the direction dt = self._get_dt(next_expected_st['NextStop'], current_dt) direction = self._get_direction_name( line_uri=line_uri, object_code=next_expected_st.get('Terminus'), default_value=next_expected_st.get('Destination'), ) next_passage = RealTimePassage(dt, direction) next_passages.append(next_passage) return next_passages
def _get_passages(self, route_point, resp): self.log.debug('sirilite response: {}'.format(resp)) line_code = route_point.fetch_line_id(self.object_id_tag) stop_deliveries = resp.get('Siri', {}).get('ServiceDelivery', {}).get('StopMonitoringDelivery', []) schedules = [ vj for d in stop_deliveries for vj in d.get('MonitoredStopVisit', []) if vj.get('MonitoredVehicleJourney', {}).get('LineRef', {}).get( 'value') == line_code ] if not schedules: self.record_additional_info('no_departure') return [] next_passages = [] for next_expected_st in schedules: destination = next_expected_st.get('MonitoredVehicleJourney', {})\ .get('DestinationRef', {}).get('value') if not destination: self.log.debug( 'no destination for next st {} for routepoint {}, skipping departure' .format(next_expected_st, route_point)) self.record_additional_info('no_destination') continue possible_routes = self.get_matching_routes( destination=destination, line=route_point.fetch_line_uri(), start=route_point.pb_stop_point.uri) if possible_routes and len(possible_routes) > 1: self.log.info( 'multiple possible routes for destination {} and routepoint {}, we add the ' 'passages on all the routes'.format( destination, route_point)) if route_point.pb_route.uri not in possible_routes: # the next passage does not concern our route point, we can skip it self.record_additional_info('no_route_found') continue # for the moment we handle only the NextStop and the direction expected_dt = next_expected_st.get('MonitoredVehicleJourney', {})\ .get('MonitoredCall', {})\ .get('ExpectedDepartureTime') if not expected_dt: # TODO, if needed we could add a check on the line opening/closing time self.record_additional_info('no_departure_time') continue dt = self._get_dt(expected_dt) destination = next_expected_st.get('MonitoredVehicleJourney', {}).get('DestinationName', []) if destination: direction = destination[0].get('value') else: direction = None is_real_time = True next_passage = RealTimePassage(dt, direction, is_real_time) next_passages.append(next_passage) return next_passages