def get_stations(mc):
    """
    Get the list of all stations, put in cache if not already there
    """
    try:
        stations = mc.get('stations')
    except KeyError:
        stations = []
        try:
            stations = nsapi.get_stations()
        except requests.exceptions.ConnectionError:
            print('Something went wrong connecting to the API')

        stations_json = ns_api.list_to_json(stations)
        # Cache the stations
        mc.set('stations', stations_json)
    return stations
def get_changed_trips(mc, routes, userkey):
    """
    Get the new or changed trips for userkey
    """
    today = datetime.datetime.now().strftime('%d-%m')
    today_date = datetime.datetime.now().strftime('%d-%m-%Y')
    current_time = datetime.datetime.now()

    prev_trips = mc.get(str(userkey) + '_trips')
    if prev_trips == None:
        prev_trips = []
    prev_trips = ns_api.list_from_json(prev_trips)
    #print prev_trips
    trips = []

    for route in routes:
        if len(route['time']) == 5:
            route_time = datetime.datetime.strptime(today_date + " " + route['time'], "%d-%m-%Y %H:%M")
        else:
            route_time = datetime.datetime.strptime(route['time'], "%d-%m-%Y %H:%M")
        delta = current_time - route_time
        if current_time > route_time and delta.total_seconds() > MAX_TIME_PAST:
            # the route was too long ago ago, lets skip it
            continue
        if current_time < route_time and abs(delta.total_seconds()) > MAX_TIME_FUTURE:
            # the route is too much in the future, lets skip it
            continue
        try:
            keyword = route['keyword']
        except KeyError:
            keyword = None
        current_trips = nsapi.get_trips(route['time'], route['departure'], keyword, route['destination'], True)
        optimal_trip = ns_api.Trip.get_optimal(current_trips, route['time'])
        if not optimal_trip:
            print "Optimal not found. Alert?"
            # TODO: Get the trip before and the one after route['time']?
        trips.append(optimal_trip)
        #print(optimal_trip)

    new_or_changed_trips = ns_api.list_diff(prev_trips, trips)
    #prev_trips = new_or_changed_trips + trips
    save_trips = ns_api.list_merge(prev_trips, trips)

    mc.set(str(userkey) + '_trips', ns_api.list_to_json(save_trips), MEMCACHE_TTL)
    return new_or_changed_trips
def get_changed_disruptions(mc, disruptions):
    """
    Get the new or changed disruptions
    """
    #prev_disruptions = None
    prev_disruptions = mc.get('prev_disruptions')
    # TODO: check whether this went ok
    if prev_disruptions == None or prev_disruptions == []:
        prev_disruptions = {'unplanned': [], 'planned': []}

    #print prev_disruptions['unplanned']
    #prev_disruptions['unplanned'] = ns_api.list_from_json(prev_disruptions['unplanned'])
    prev_disruptions_unplanned = ns_api.list_from_json(prev_disruptions['unplanned'])
    #prev_disruptions['planned'] = ns_api.list_from_json(prev_disruptions['planned'])

    #new_or_changed_unplanned = ns_api.list_diff(prev_disruptions['unplanned'], disruptions['unplanned'])
    new_or_changed_unplanned = ns_api.list_diff(prev_disruptions_unplanned, disruptions['unplanned'])
    #print('New or changed unplanned disruptions:')
    #print(new_or_changed_unplanned)

    #unchanged_unplanned = ns_api.list_same(prev_disruptions['unplanned'], disruptions['unplanned'])

    #prev_unplanned = new_or_changed_unplanned + unchanged_unplanned
    #prev_unplanned = new_or_changed_unplanned + prev_disruptions_unplanned
    save_unplanned = ns_api.list_merge(prev_disruptions_unplanned, new_or_changed_unplanned)

    # Planned disruptions don't have machine-readable date/time and route information, so
    # we skip planned disruptions for this moment
    #new_or_changed_planned = ns_api.list_diff(prev_disruptions['planned'], disruptions['planned'])
    #print(new_or_changed_planned)
    #for plan in new_or_changed_planned:
    #    print plan.key
    #    print plan.message
    #    print "------"

    #unchanged_planned = ns_api.list_same(prev_disruptions['planned'], disruptions['planned'])
    #prev_planned = new_or_changed_planned + unchanged_planned

    # Update the cached list with the current information
    #mc.set('prev_disruptions', {'unplanned': ns_api.list_to_json(prev_unplanned), 'planned': []})
    #mc.set('prev_disruptions', {'unplanned': ns_api.list_to_json(disruptions['unplanned']), 'planned': []}, MEMCACHE_TTL)
    mc.set('prev_disruptions', {'unplanned': ns_api.list_to_json(save_unplanned), 'planned': []}, MEMCACHE_TTL)
    return new_or_changed_unplanned