Exemplo n.º 1
0
 def _make_request(self, dt, count, monitoring_ref):
     # we don't want to ask 1000 next departure to SIRI :)
     count = min(count or 5, 5)  # if no value defined we ask for 5 passages
     message_identifier = 'IDontCare'
     request = """<?xml version="1.0" encoding="UTF-8"?>
     <x:Envelope xmlns:x="http://schemas.xmlsoap.org/soap/envelope/"
                 xmlns:wsd="http://wsdl.siri.org.uk" xmlns:siri="http://www.siri.org.uk/siri">
       <x:Header/>
       <x:Body>
         <GetStopMonitoring xmlns="http://wsdl.siri.org.uk" xmlns:siri="http://www.siri.org.uk/siri">
           <ServiceRequestInfo xmlns="">
             <siri:RequestTimestamp>{dt}</siri:RequestTimestamp>
             <siri:RequestorRef>{RequestorRef}</siri:RequestorRef>
             <siri:MessageIdentifier>{MessageIdentifier}</siri:MessageIdentifier>
           </ServiceRequestInfo>
           <Request version="1.3" xmlns="">
             <siri:RequestTimestamp>{dt}</siri:RequestTimestamp>
             <siri:MessageIdentifier>{MessageIdentifier}</siri:MessageIdentifier>
             <siri:MonitoringRef>{MonitoringRef}</siri:MonitoringRef>
             <siri:MinimumStopVisitsPerLine>{count}</siri:MinimumStopVisitsPerLine>
           </Request>
           <RequestExtension xmlns=""/>
         </GetStopMonitoring>
       </x:Body>
     </x:Envelope>
     """.format(
         dt=floor_datetime(datetime.utcfromtimestamp(dt),
                           self.from_datetime_step).isoformat(),
         count=count,
         RequestorRef=self.requestor_ref,
         MessageIdentifier=message_identifier,
         MonitoringRef=monitoring_ref,
     )
     return request
Exemplo n.º 2
0
    def _make_url(self, route_point, count=None, from_dt=None):
        """
        the route point identifier is set with the StopDescription argument
         this argument is split in 3 arguments (given between '?' and ';' symbol....)
         * StopTimeoCode: timeo code for the stop
         * LineTimeoCode: timeo code for the line
         * Way: 'A' if the route is forward, 'R' if it is backward
         2 additionals args are needed in this StopDescription ...:
         * NextStopTimeNumber: the number of next departure we want
         * StopTimeType: if we want base schedule data ('TH') or real time one ('TR')

         Note: since there are some strange symbol ('?' and ';') in the url we can't use param as dict in
         requests
         """

        base_params = '&'.join([k + '=' + v for k, v in self.service_args.items()])

        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)

        if not all((stop, line, route)):
            # one a the id is missing, we'll not find any realtime
            logging.getLogger(__name__).debug('missing realtime id for {obj}: '
                                              'stop code={s}, line code={l}, route code={r}'.
                                              format(obj=route_point, s=stop, l=line, r=route),
                                              extra={'rt_system_id': unicode(self.rt_system_id)})
            self.record_internal_failure('missing id')
            return None

        # timeo can only handle items_per_schedule if it's < 5
        count = min(count or 5, 5)# if no value defined we ask for 5 passages

        # if a custom datetime is provided we give it to timeo but we round it to improve cachability
        dt_param = '&NextStopReferenceTime={dt}'\
            .format(dt=floor_datetime(self._timestamp_to_date(from_dt), self.from_datetime_step).strftime('%Y-%m-%dT%H:%M:%S')) \
            if from_dt else ''

        #We want to have StopTimeType as it make parsing of the request way easier
        #for alternative implementation of timeo since we can ignore this params
        stop_id_url = ("StopDescription=?"
                       "StopTimeType={data_freshness}"
                       "&LineTimeoCode={line}"
                       "&Way={route}"
                       "&NextStopTimeNumber={count}"
                       "&StopTimeoCode={stop}{dt};").format(stop=stop,
                                                                 line=line,
                                                                 route=route,
                                                                 count=count,
                                                                 data_freshness='TR',
                                                                 dt=dt_param)

        url = "{base_url}?{base_params}&{stop_id}".format(base_url=self.service_url,
                                                          base_params=base_params,
                                                          stop_id=stop_id_url)

        return url