def __init__(self, origin, destination, date=None, transit_mode_type=None, itinerary_index=0): Itinerary.__init__(self, origin, destination, date, transit_mode_type, itinerary_index) self.transport_mode = "walking" url_request = Walking.__URL_API_DIRECTION url_request += "&origin=" + str(self.origin.lat) + "," + str(self.origin.lng) url_request += "&destination=" + str(self.destination.lat) + "," + str(self.destination.lng) r = requests.get(url_request) if r.status_code != 200: raise BadRequest(r.status_code) else: raw_data = r.json() if raw_data['status'] == "OVER_QUERY_LIMIT": raise QueryLimit("Can't retieve any data from API (Walking)") else: # on récupère les informations concernant les différentes étapes steps = raw_data['routes'][self.itinerary_index]['legs'][0]['steps'] self.total_duration = raw_data['routes'][self.itinerary_index]['legs'][0]['duration']['value'] self.total_polyline = [raw_data['routes'][self.itinerary_index]['overview_polyline']['points']] self.walking_distance = 0 self.walking_duration = 0 self.information_legs = [] # Notre information_legs liste stockant nos étapes de trajet # Parcours des étapes trouvées de notre trajet pour remplir # notre liste de stockage self.information_legs for step_number, step in enumerate(steps): self.information_legs.append({}) self.information_legs[step_number]['transport_mode'] = step['travel_mode'] self.information_legs[step_number]['interim_start'] = Place(lat=step['start_location']['lat'], lng=step['start_location']['lng']) self.information_legs[step_number]['interim_destination'] = Place(lat=step['end_location']['lat'], lng=step['end_location']['lng']) self.information_legs[step_number]['distance'] = step['distance']['value'] self.information_legs[step_number]['duration'] = step['duration']['value'] self.information_legs[step_number]['instructions'] = step['html_instructions'] self.walking_distance += step['distance']['value'] self.walking_duration += step['duration']['value']
def __init__(self, origin, destination, date=None, transit_mode_type=None, itinerary_index=0): Itinerary.__init__(self, origin, destination, date, transit_mode_type, itinerary_index) self.transport_mode = "velib" #prix velib de base - beaucoup de segmentation clients pour les tarifs... self.price = "For 1,70€, the 1 day ticket gives you access to Velib' for 24h with the first 30min free of charge. Additional fares apply afterwards." # Station velib d'origine stop = True search_size = 1 while stop: parameters = Velib.__PARAMETERS + "&geofilter.distance=" + "%2C".join( [ str(self.origin.lat), str(self.origin.lng), str(search_size * 100) ]) r = requests.get(Velib.__URL_VELIB, parameters) if r.status_code != 200: raise BadRequest(r.status_code) else: raw_data = r.json() search_size += 1 stations_origin = [] possible_stations = raw_data['records'] for possible_station in possible_stations: available_bike = possible_station['fields'][ 'available_bikes'] if available_bike == 0: pass else: stop = False stations_origin.append({}) stations_origin[-1]['station_address'] = Place( address=possible_station['fields']['address'], lat=possible_station['fields']['position'][0], lng=possible_station['fields']['position'][1]) stations_origin[-1]['nb_bike'] = available_bike fastest_path_origin = Walking(origin, stations_origin[0]['station_address'], date=self.date) for station in stations_origin: walk = Walking(origin, station['station_address'], date=self.date) if walk.total_duration < fastest_path_origin.total_duration: fastest_path_origin = walk # station velib à l'arrivée stop = True search_size = 1 while stop: parameters = Velib.__PARAMETERS + "&geofilter.distance=" + "%2C".join( [ str(self.destination.lat), str(self.destination.lng), str(search_size * 1000) ]) r = requests.get(Velib.__URL_VELIB, parameters) if r.status_code != 200: raise BadRequest(r.status_code) else: raw_data = r.json() search_size += 1 stations_destination = [] possible_stations = raw_data['records'] for possible_station in possible_stations: empty_slots = possible_station['fields'][ 'available_bike_stands'] if empty_slots == 0: pass else: stop = False stations_destination.append({}) stations_destination[-1]['station_address'] = Place( address=possible_station['fields']['address'], lat=possible_station['fields']['position'][0], lng=possible_station['fields']['position'][1]) stations_destination[-1]['empty_slots'] = empty_slots fastest_path_destination = Walking( stations_destination[0]['station_address'], destination) for station in stations_destination: walk = Walking(station['station_address'], destination) if walk.total_duration < fastest_path_destination.total_duration: fastest_path_destination = walk # trajet en velib start_date_velib = self.date + timedelta( 0, fastest_path_origin.total_duration + Velib.__COMMUTING_DURATION) velib = Bicycling(fastest_path_origin.destination, fastest_path_destination.origin, date=start_date_velib) # Prise en compte du temps pour la dernière étape (station d'arrivée velib à destination) start_date_last_leg = start_date_velib + timedelta( 0, velib.total_duration + Velib.__COMMUTING_DURATION) fastest_path_destination = Walking(fastest_path_destination.origin, self.destination, date=start_date_last_leg) # Itineraire total = fastest_path_origin + velib + fastest_path_destination self.total_duration = fastest_path_origin.total_duration \ + velib.total_duration \ + fastest_path_destination.total_duration self.walking_duration = fastest_path_origin.walking_duration \ + velib.walking_duration \ + fastest_path_destination.walking_duration self.walking_distance = fastest_path_origin.walking_distance \ + velib.walking_distance \ + fastest_path_destination.walking_distance self.transit_duration = fastest_path_origin.transit_duration \ + velib.transit_duration \ + fastest_path_destination.transit_duration self.velib_duration = velib.bicycling_duration self.velib_distance = velib.bicycling_distance self.total_polyline = fastest_path_origin.total_polyline + velib.total_polyline \ + fastest_path_destination.total_polyline self.information_legs = fastest_path_origin.information_legs \ + velib.information_legs \ + fastest_path_destination.information_legs
def __init__(self, origin, destination, date=None, transit_mode_type=None, itinerary_index=0): Itinerary.__init__(self, origin, destination, date, transit_mode_type, itinerary_index) self.transport_mode = "autolib" #Prix indiqué sur le site autolib, indicatif self.price = "For 10€/month you have access to a car 24/7, and the rent available at the best price. +0,23€/ minute. Head to the Autolib website for more information." # Station autolib d'origine stop = True search_size = 1 while stop: parameters = Autolib.__PARAMETERS + "&geofilter.distance=" + "%2C".join( [str(self.origin.lat), str(self.origin.lng), str(search_size * 100)]) r = requests.get(Autolib.__URL_AUTOLIB, parameters) if r.status_code != 200: raise BadRequest(r.status_code) else: raw_data = r.json() search_size += 1 stations_origin = [] possible_stations = raw_data['records'] for possible_station in possible_stations: available_car = possible_station['fields']['cars'] if available_car == 0: pass else: stop = False stations_origin.append({}) stations_origin[-1]['station_address'] = Place(address=(possible_station['fields']['address'] + " " + possible_station['fields'][ 'postal_code'] + " Paris"), lat=possible_station['fields']['geo_point'][0], lng=possible_station['fields']['geo_point'][1]) stations_origin[-1]['nb_auto'] = available_car fastest_path_origin = Walking(self.origin, stations_origin[0]['station_address'], date=self.date) for station in stations_origin: walk = Walking(self.origin, station['station_address'], date=self.date) if walk.total_duration < fastest_path_origin.total_duration: fastest_path_origin = walk # station autolib à l'arrivée stop = True search_size = 1 while stop: parameters = Autolib.__PARAMETERS + "&geofilter.distance=" + "%2C".join( [str(self.destination.lat), str(self.destination.lng), str(search_size * 100)]) r = requests.get(Autolib.__URL_AUTOLIB, parameters) if r.status_code != 200: raise BadRequest(r.status_code) else: raw_data = r.json() search_size += 1 stations_destination = [] possible_stations = raw_data['records'] for possible_station in possible_stations: empty_slots = possible_station['fields']['slots'] - possible_station['fields']['cars'] if empty_slots == 0: pass else: stop = False stations_destination.append({}) stations_destination[-1]['station_address'] = Place( address=(possible_station['fields']['address'] + " " + possible_station['fields'][ 'postal_code'] + " Paris"), lat=possible_station['fields']['geo_point'][0], lng=possible_station['fields']['geo_point'][1]) stations_destination[-1]['empty_slots'] = empty_slots fastest_path_destination = Walking(stations_destination[0]['station_address'], self.destination) for station in stations_destination: walk = Walking(station['station_address'], self.destination) if walk.total_duration < fastest_path_destination.total_duration: fastest_path_destination = walk # trajet en autolib start_date_autolib = self.date + timedelta(0, fastest_path_origin.total_duration + Autolib.__COMMUTING_DURATION) autolib = Driving(fastest_path_origin.destination, fastest_path_destination.origin, date=start_date_autolib) # Prise en compte du temps pour la dernière étape (station d'arrivée Autolib à destination) start_date_last_leg = start_date_autolib + timedelta(0, autolib.total_duration + Autolib.__COMMUTING_DURATION) fastest_path_destination = Walking(fastest_path_destination.origin, self.destination, date=start_date_last_leg) # Itineraire total = fastest_path_origin + autolib + fastest_path_destination self.total_duration = fastest_path_origin.total_duration \ + autolib.total_duration \ + fastest_path_destination.total_duration self.walking_duration = fastest_path_origin.walking_duration \ + autolib.walking_duration \ + fastest_path_destination.walking_duration self.walking_distance = fastest_path_origin.walking_distance \ + autolib.walking_distance \ + fastest_path_destination.walking_distance self.transit_duration = fastest_path_origin.transit_duration \ + autolib.transit_duration \ + fastest_path_destination.transit_duration self.driving_duration = autolib.driving_duration self.driving_distance = autolib.driving_distance self.total_polyline = fastest_path_origin.total_polyline \ + autolib.total_polyline \ + fastest_path_destination.total_polyline self.information_legs = fastest_path_origin.information_legs \ + autolib.information_legs \ + fastest_path_destination.information_legs
def __init__(self, origin, destination, date=None, transit_mode_type=None, itinerary_index=0): Itinerary.__init__(self, origin, destination, date, transit_mode_type, itinerary_index) self.transport_mode = "transit" if self.transit_mode_type is None: self.transit_mode_type = "bus|rail" url_request = Transit.__URL_API_DIRECTION_TRANSIT url_request += "&origin=" + str(self.origin.lat) + "," + str( self.origin.lng) url_request += "&destination=" + str(self.destination.lat) + "," + str( self.destination.lng) url_request += "&transit_mode=" + str(self.transit_mode_type) r = requests.get(url_request) if r.status_code != 200: raise BadRequest(r.status_code) else: raw_data = r.json() if raw_data['status'] == "OVER_QUERY_LIMIT": raise QueryLimit("Can't retieve any data from API (Transit)") else: steps = raw_data['routes'][ self.itinerary_index]['legs'][0]['steps'] self.total_duration = raw_data['routes'][ self.itinerary_index]['legs'][0]['duration']['value'] self.total_polyline = [ raw_data['routes'][self.itinerary_index] ['overview_polyline']['points'] ] self.walking_distance = 0 self.walking_duration = 0 self.transit_duration = 0 self.information_legs = [] # Parcours des étapes trouvées de notre trajet # pour remplir notre liste de stockage self.information_legs for step_number, step in enumerate(steps): self.information_legs.append({}) self.information_legs[step_number][ 'transport_mode'] = step['travel_mode'] self.information_legs[step_number]['distance'] = step[ 'distance']['value'] self.information_legs[step_number][ 'interim_destination'] = Place( lat=step['end_location']['lat'], lng=step['end_location']['lng']) self.information_legs[step_number]['duration'] = step[ 'duration']['value'] self.information_legs[step_number][ 'interim_start'] = Place( lat=step['start_location']['lat'], lng=step['start_location']['lng']) self.information_legs[step_number]['instructions'] = step[ 'html_instructions'] if self.information_legs[step_number][ 'transport_mode'] == "TRANSIT": self.information_legs[step_number][ 'arrival_stop'] = step['transit_details'][ 'arrival_stop']['name'] self.information_legs[step_number]['departure_stop'] = \ step['transit_details']['departure_stop']['name'] self.information_legs[step_number][ 'transit_mode'] = step['transit_details']['line'][ 'vehicle']['type'] if 'short_name' in step['transit_details'][ 'line'].keys(): self.information_legs[step_number]['line'] = step[ 'transit_details']['line']['short_name'] else: self.information_legs[step_number]['line'] = step[ 'transit_details']['line']['name'] self.information_legs[step_number][ 'number_stops'] = step['transit_details'][ 'num_stops'] self.information_legs[step_number]['duration'] = step[ 'duration']['value'] self.transit_duration += step['duration']['value'] if self.information_legs[step_number][ 'transport_mode'] == "WALKING": self.walking_distance += step['distance']['value'] self.walking_duration += step['duration']['value']
def __init__(self, origin, destination, date=None, uber_type=None, transit_mode_type=None, itinerary_index=0): Itinerary.__init__(self, origin, destination, date, transit_mode_type, itinerary_index) self.transport_mode = "uber" if uber_type in Uber.__UBER_MODE_TYPES: self._uber_type = uber_type elif isinstance(uber_type, str): raise ValueError( "The uber type must be a part of the possible uber vehicle options" ) elif uber_type is None: self._uber_type = "uberx" else: raise TypeError( "The uber type must be a chain of characters among the possible uber vehicle options available." ) self._options_uber = [] self._uber_wait_duration = 0 self._uber_travel_duration = 0 self._available_options = [] # Code Spécifique à l'API UBER headers = { 'Authorization': 'Token 1QTK0iskAoX7vFZ3Ir1j_NdqnADK7zXAF4GcaRLe', 'Accept-Language': 'en_US', "Content-Type": "application/json" } #PRICE API from Uber to get Price information url_request_price = Uber.__URL_UBER_PRICE url_request_price += 'start_latitude=' + str( self.origin.lat) + '&start_longitude=' + str( self.origin.lng) + '&end_latitude=' + str( self.destination.lat) + '&end_longitude=' + str( self.destination.lng) r_price = requests.get(url=url_request_price, headers=headers) #TIME API from Uber to get Time information url_request_time = Uber.__URL_UBER_TIME url_request_time += 'start_latitude=' + str( self.origin.lat) + '&start_longitude=' + str( self.origin.lng) + '&end_latitude=' + str( self.destination.lat) + '&end_longitude=' + str( self.destination.lng) r_time = requests.get(url=url_request_time, headers=headers) if r_price.status_code != 200 or r_time.status_code != 200: raise BadRequest() else: raw_data_price = r_price.json() raw_data_time = r_time.json() options_price = raw_data_price['prices'] options_time = raw_data_time['times'] # les estimations temps et prix ne sont pas toujours identiques. # On veut prendre l'intersection des deux. # D'où la disjonction de cas ... if len(options_price) <= len(options_time): for uber_option, option in enumerate(options_price): self._options_uber.append({}) self._options_uber[-1]['uber_name'] = str( option['display_name']).lower() self._available_options.append( str(option['display_name']).lower()) self._options_uber[-1]['price'] = option['estimate'] self._options_uber[-1]['distance'] = option['distance'] self._options_uber[-1]['duration'] = option['duration'] if options_time[uber_option]['display_name'] == option[ 'display_name']: self._options_uber[-1]['wait_time'] = options_time[ uber_option]['estimate'] if self._options_uber[-1]['uber_name'] == self._uber_type: self.price = self._options_uber[uber_option]['price'] self._uber_wait_duration = self._options_uber[ uber_option]['wait_time'] self._uber_travel_duration = self._options_uber[ uber_option]['duration'] self.total_duration = self._uber_wait_duration + self._uber_travel_duration self.driving_distance = int( self._options_uber[uber_option]['distance'] * 1610) # l'API renvoie des miles et non des km else: for uber_option, option in enumerate(options_time): self._options_uber.append({}) self._options_uber[-1]['uber_name'] = str( option['display_name']).lower() self._available_options.append( str(option['display_name']).lower()) self._options_uber[-1]['wait_time'] = option['estimate'] if options_price[uber_option]['display_name'] == option[ 'display_name']: self._options_uber[-1]['price'] = options_price[ uber_option]['estimate'] self._options_uber[-1]['distance'] = options_price[ uber_option]['distance'] self._options_uber[-1]['duration'] = options_price[ uber_option]['duration'] if self._options_uber[-1]['uber_name'] == self._uber_type: self.price = self._options_uber[uber_option]['price'] self._uber_wait_duration = self._options_uber[ uber_option]['wait_time'] self._uber_travel_duration = self._options_uber[ uber_option]['duration'] self.total_duration = self._uber_wait_duration + self._uber_travel_duration self.driving_distance = int( float(self._options_uber[uber_option]['distance']) * 1610) # l'API renvoie des miles et non des km self.total_polyline = Driving(origin, destination, date, transit_mode_type, itinerary_index).total_polyline if self._uber_type not in self._available_options: TypeError( "The Uber option you selected isn't available. Please select another option or try again later." )