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': []}

    prev_disruptions_unplanned = ns_api.list_from_json(prev_disruptions['unplanned'])
    new_or_changed_unplanned = ns_api.list_diff(prev_disruptions_unplanned, disruptions['unplanned'])
    save_unplanned = ns_api.list_merge(prev_disruptions_unplanned, new_or_changed_unplanned)

    try:
        keywordfilter = settings.keywordfilter
    except AttributeError:
        keywordfilter = []
    # filter away on keyword
    save_unplanned_filtered = []
    for item in save_unplanned:
        for keyword in keywordfilter:
            if keyword not in item:
                save_unplanned_filtered.append(item)

    # Update the cached list with the current information
    mc.set('prev_disruptions', {'unplanned': ns_api.list_to_json(save_unplanned_filtered), 'planned': []}, MEMCACHE_TTL)
    return new_or_changed_unplanned
Ejemplo n.º 2
0
def get_changed_trips(mc, nsapi, 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)
    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_actual(current_trips, route['time'])
        #optimal_trip = ns_api.Trip.get_optimal(current_trips)
        if not optimal_trip:
            #print("Optimal not found. Alert?")
            # TODO: Get the trip before and the one after route['time']?
            pass
        else:
            try:
                # User set a minimum treshold for departure, skip if within this limit
                minimal_delay = int(route['minimum'])
                trip_delay = optimal_trip.delay
                if (not optimal_trip.has_delay) or (optimal_trip.has_delay and trip_delay['departure_delay'] != None and trip_delay['departure_delay'].seconds//60 < minimal_delay and optimal_trip.going):
                    # Trip is going, has no delay or one that is below threshold, ignore
                    optimal_trip = None
            except KeyError:
                # No 'minimum' setting found, just continue
                pass
        if optimal_trip:
            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
Ejemplo n.º 3
0
def get_stations(mc, nsapi):
    """
    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
Ejemplo n.º 4
0
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': []}

    prev_disruptions_unplanned = ns_api.list_from_json(prev_disruptions['unplanned'])
    new_or_changed_unplanned = ns_api.list_diff(prev_disruptions_unplanned, disruptions['unplanned'])
    save_unplanned = ns_api.list_merge(prev_disruptions_unplanned, new_or_changed_unplanned)

    try:
        keywordfilter = settings.keywordfilter
    except AttributeError:
        keywordfilter = []
    # filter away on keyword
    save_unplanned_filtered = []
    for item in save_unplanned:
        for keyword in keywordfilter:
            if keyword not in item:
                save_unplanned_filtered.append(item)

    # 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(save_unplanned_filtered), 'planned': []}, MEMCACHE_TTL)
    return new_or_changed_unplanned