Example #1
0
def get(term):
    language = utils.get_language()
    key = 'places_' + language
    places = memcache.get(key) #@UndefinedVariable
    if places == None:
        attractions = [x for x in db.GqlQuery("SELECT * FROM Attraction LIMIT 1000")]
        for x in attractions:
            x.place_type = 'airport' if x.name.find('Airport') > 0 else 'sight'
            if language == 'ru' and x.name_rus != None:
                x.name_local = x.name_rus
            else:
                x.name_local = x.name
        hotels = get_hotels()
        places = attractions + hotels 
        memcache.add(key, places, 24*60*60) #@UndefinedVariable
        
    if term != None and term != '':
        result = [x for x in places if (x.name != None and x.name.lower().startswith(term.lower()))
                                    or (x.name_rus != None and x.name_rus.lower().startswith(term.lower()))]
        if len(result) < 10:
            result.extend([x for x in places if (x.name != None and x.name.lower().find(term.lower()) > 0) 
                                             or (x.name_rus != None and x.name_rus.lower().find(term.lower()) > 0)])
        return result
    else:
        return places
Example #2
0
def load(start_location, end_location):
    language = utils.get_language()
    key = 'rusbus_' + start_location.to_url_param() + '_' + end_location.to_url_param() + '_' + language
    data = memcache.get(key) #@UndefinedVariable
    if data != None:
        return data
    
    existing_cookie_used = True
    cookie_key = 'rusbus_cookie'
    cookie_header = memcache.get(cookie_key) #@UndefinedVariable
    opener = urlopener.URLOpener('spb.rusavtobus.ru', cookie_header)
    if cookie_header is None:
        logging.info('RusAvtobus: starting session')
        start_session(opener)
        existing_cookie_used = False
        
    data = retrieve(opener, start_location, end_location, language, key)
    if data is None and existing_cookie_used:
        # existing cookie might have expired, try with new one
        logging.warn('RusAvtobus: existing session expired, starting new session')
        existing_cookie_used = False
        opener = urlopener.URLOpener('spb.rusavtobus.ru')
        start_session(opener)
        data = retrieve(opener, start_location, end_location, language, key)
        
    if data is not None and not existing_cookie_used:
        memcache.add(cookie_key, opener.cookie_header, 60*60) #@UndefinedVariable
        
    if data is None:
        logging.error('RusAvtobus did not retrieve data')
    else:
        memcache.add(key, data) #@UndefinedVariable
        
    return data
Example #3
0
def get_walking_step(start_location, end_location, end_name = None):
    step = None
    direct_distance = estimate_distance(start_location, end_location)
    if direct_distance > 150:
        language = utils.get_language()
        params = { 
                  'origin': start_location.to_url_param(),
                  'destination': end_location.to_url_param(), 
                  'sensor' : 'false', 
                  'mode': 'walking',
                  'language' : language
                  }
        url = 'http://maps.google.com/maps/api/directions/json?' + urllib.urlencode(params)
        result = urlfetch.fetch(url)
    
        if result.status_code == 200:
            route = json.loads(result.content)
            if route['status'] == 'OVER_QUERY_LIMIT':
                logging.warn('OVER_QUERY_LIMIT while fetching walking directions')
            elif route['status'] == 'ZERO_RESULTS':
                # no route found
                pass
            else:
                instructions = []
                duration = 0
                distance = 0
                for leg in route['routes'][0]['legs']:
                    for leg_step in leg['steps']:
                        instructions.append(clean_walk_direction(utils.remove_html_tags(leg_step['html_instructions'])))
                        duration += int(leg_step['duration']['value'])
                        distance += int(leg_step['distance']['value'])
                step = common.RouteStep()                            
                step.direction = ', '.join(instructions)
                step.duration = int(duration / 60)      
                step.distance = distance                  
        else:
            logging.warn('Walking route failed: ' + str(result.status_code))
        
    if step == None: #google not needed or failed
        step = common.RouteStep()
        step.distance = estimate_distance(start_location, end_location) * 1.4
        step.duration = int(step.distance / 80 + 0.5)
        
    if end_name != None:
        walk_text = _('Walk to') + ' ' + end_name
        if step.direction != None:
            step.direction = walk_text + ': ' + step.direction
        else:
            step.direction = walk_text
    elif step.direction == None:
        step.direction = _('Walk') + ' ' + utils.distance_to_string(step.distance)

    step.transport = None # walk                
    step.start_location = start_location
    step.end_location = end_location
    step.has_map = True
    return step
Example #4
0
def get_hotels():    
    language = utils.get_language()
    key = 'hotels_' + language
    hotels = memcache.get(key) #@UndefinedVariable
    if hotels == None:
        hotels = [x for x in db.GqlQuery("SELECT * FROM Hotel LIMIT 1000")]
        for x in hotels:
            x.place_type = 'hotel'
            if language == 'ru' and x.name_rus != None and x.name_rus != '':
                x.name_local = x.name_rus
            else:
                x.name_local = x.name
        memcache.add(key, hotels, 24*60*60) #@UndefinedVariable
    return hotels
Example #5
0
def get_transit_route(start_location, end_location, engine = 'o', force = ''):
    language = utils.get_language()
    key = 'route_' + start_location.to_url_param() + '_' + end_location.to_url_param() + '_' + engine + '_' + language + '_' + force
    data = memcache.get(key) #@UndefinedVariable
    if data != None:
        return data
    
    distance = estimate_distance(start_location, end_location)
    if distance != None and distance < 2000:
        walk_route = get_walking_route(start_location, end_location)
        if walk_route != None and walk_route.get_duration() < 30:
            memcache.add(key, walk_route, 60*60) #@UndefinedVariable
            return walk_route
    
    subway_route = None
    if engine == 'o' or engine == 's':  
        subway_route = get_subway_route(start_location, end_location)
        if subway_route != None and distance > 5000:
            memcache.add(key, subway_route, 60*60) #@UndefinedVariable
            return subway_route
    
    routes = []
    if engine == 'o' or engine == 'r':
        routes = get_rusavtobus_routes(start_location, end_location)
    if engine == 'g' or routes is None or len(routes) == 0:
        routes = get_google_routes(start_location, end_location)
    if subway_route != None:
        routes.append(subway_route)
    if force.find('train') >= 0:
        routes_with_train = [x for x in routes if x.has_transport('Train')]
        if len(routes_with_train) > 0:
            routes = routes_with_train
    if force.find('nosharetaxi') >= 0:
        routes_without_sharetaxi = [x for x in routes if not x.has_transport('Share taxi')]
        if len(routes_without_sharetaxi) > 0:
            routes = routes_without_sharetaxi
    if len(routes) > 0:
        route_optimal = min(routes, key=lambda x: x.get_cost())
        memcache.add(key, route_optimal, 60*60) #@UndefinedVariable
        return route_optimal 
Example #6
0
def get_subway_route(start_location, end_location):
    language = utils.get_language()
    route = router.get_route(start_location, end_location)
    if route == None:
        return None
    
    steps = []
            
    for leg in route:
        step = common.RouteStep()                                
        step.duration = int((sum([x.distance for x in leg.steps[1:]]) + 30) / 60)
        step.start_location = common.GeoPoint(leg.steps[0].station.lat, leg.steps[0].station.lng)
        step.end_location = common.GeoPoint(leg.steps[-1].station.lat, leg.steps[-1].station.lng)
        step.start_name = leg.steps[0].station.name if language != 'ru' else leg.steps[0].station.name_rus 
        step.end_name = leg.steps[-1].station.name if language != 'ru' else leg.steps[-1].station.name_rus
        step.transport = common.Transport(leg.transport, str(leg.line), stops = len(leg.steps) - 1)
        step.has_map = True
        steps.append(step)

    route = common.Route(steps)
    process_transit_route(start_location, end_location, route)
    if route.get_walk_duration() < 40:
        return route
Example #7
0
def clean_walk_direction(direction):
    language = utils.get_language()
    result = direction
    if language != 'ru':
        result = translit.translify(result) 
    return result.replace(_('/M10'), '').replace(_('/M18'), '')
Example #8
0
def process_transit_route(start_location, end_location, route):    
    language = utils.get_language()
    
    # Add end walk
    end_walk = get_walking_step(route.directions[-1].end_location, end_location)
    if end_walk.duration > 0:
        route.directions.append(end_walk)
    
    first_subway = True
    index = 0
    while index < len(route.directions) and index < 100:
        step = route.directions[index]
        previous_step = route.directions[index - 1] if index > 0 else None
        next_step = route.directions[index + 1] if index < len(route.directions) - 1 else None
        
        if step.transport != None:
            if step.transport.is_train():
                station = router.resolve_name(step.start_name, 'Train', step.start_location)
                if station != None:
                    step.start_name = station.name_rus if language == 'ru' else station.name
                    step.transport.start_code = station.name_rus
                    step.start_location.lat = station.lat
                    step.start_location.lng = station.lng
                station = router.resolve_name(step.end_name, 'Train', step.end_location)
                if station != None:
                    step.end_name = station.name_rus if language == 'ru' else station.name
                    step.transport.end_code = station.name_rus
                    step.end_location.lat = station.lat
                    step.end_location.lng = station.lng

            if not step.transport.is_subway() or first_subway:
                from_location = previous_step.end_location if previous_step != None else start_location 
                tostop_walk = get_walking_step(from_location, step.start_location,
                                               _(step.transport.type).lower() + ' ' + _('stop') + ' ' + step.start_name)
                route.directions.insert(index, tostop_walk)
                index += 1
            
            if step.transport.is_train():
                new_step = common.RouteStep(_('Buy the train tickets to') + ' ' + step.end_name + ', ' + _('wait for the train'),
                                       15, '', common.Transport('Train', price = step.transport.price))                
                route.directions.insert(index, new_step)
                index += 1
                step.transport.price = None
            
            if step.transport.is_subway():
                if first_subway:
                    new_step = common.RouteStep(_('Enter subway station') + ' ' + utils.subway_color(step.start_name, step.transport.line_number) + ' (' + _('buy the tokens if needed') + ')',
                                          5, utils.duration_to_string(5) + ', ' + utils.price_to_string(27), common.Transport('Subway', price = 27))                
                else:
                    to_text = _('Change to') + ' ' + utils.subway_color(_('line') + ' ' + str(step.transport.line_number), step.transport.line_number) + ' ' + _('station') + ' ' + utils.subway_color(step.start_name, step.transport.line_number)
                    new_step = common.RouteStep(to_text, 4, utils.duration_to_string(4))
                route.directions.insert(index, new_step)
                index += 1
                step.transport.price = None
                first_subway = False
                if step.transport.stops == 0:
                    route.directions.remove(step)
                    continue
                                
            # Set price for land transport 
            if step.transport.type in ['Bus', 'Trolleybus', 'Tram']:
                step.transport.price = 23
            elif step.transport.type in ['Share taxi']:
                step.transport.price = 35
                
            # Make trolleybus less optimistic
            if step.transport.type == 'Trolleybus':
                step.duration *= 1.4
                            
            # Add 3/4 of service interval duration
            if not step.transport.is_subway():
                if step.transport.interval != None:
                    step.duration += step.transport.interval * 3 / 4                 
                else:
                    step.duration += get_default_service_interval(step.transport) * 3 / 4
                                        
        # Do not show the map for change walks inside subway
        if step.is_walk() and previous_step != None and next_step != None and \
            previous_step.is_subway() and next_step.is_subway():
            step.duration = 4 # subway change is 4 minutes
        else:
            step.has_map = step.start_location != None and step.end_location != None
            
        if step.transport != None:
            if step.is_subway():
                step.direction = _(step.transport.type) + ': ' + utils.subway_color(_('line') + ' ' + step.transport.line_number, step.transport.line_number) + \
                                 ' ' + _('to') + ' ' + utils.subway_color(step.end_name, step.transport.line_number)
            elif step.transport.is_train():
                step.direction = _(step.transport.type) + ' ' + _('to') + ' ' + step.end_name
            else:
                step.direction = _(step.transport.type) + ' ' + step.transport.line_number + ' ' + _('to') + ' ' + step.end_name 
            
        # Improve walk to direction 
        if step.is_walk() and next_step != None and not next_step.is_walk():
            stop_text = _('station') if next_step.is_subway() else _('stop')
            step.direction = step.direction.replace(_('Walk to') + ' ', _('Walk to') + ' ' + _(next_step.transport.type).lower() + ' ' + stop_text + ' ')
   
        index += 1
         
    # Find all subways
    subways = [step for step in route.directions if step.is_subway() and step.direction != None]
    if len(subways) > 0:
        # Add 4 minutes to exit subway
        subways[-1].direction += ', ' + _('leave the subway')
        subways[-1].duration += 4
Example #9
0
def do_about(request, response):
    view.to_html(None, 'about' if utils.get_language() != 'ru' else 'about_ru', request, response)
Example #10
0
def do_booking(request, response):
    view.to_html(None, 'booking' if utils.get_language() != 'ru' else 'booking_ru', request, response)
Example #11
0
 def place_name_local(self):
     return self.place_name_rus if utils.get_language() == 'ru' else self.place_name
Example #12
0
 def hotel_name_local(self):
     return self.hotel_name_rus if utils.get_language() == 'ru' and self.hotel_name != '' else self.hotel_name
Example #13
0
def get_language_link(path):
    language = utils.get_language()
    name = _('EnglishRu') if language == 'ru' else _('RussianEn')
    code = 'en' if language == 'ru' else 'ru'
    lang_path = path.replace('/'  + language, '/' + code)
    return '<a class="language" href="' + lang_path + '">' + name + '</a>'
Example #14
0
 def address_local(self):
     return self.address_rus if utils.get_language() == 'ru' else self.address