
s = {

    # This represents the most that would be in this playlist. Some products
    # that do not have current data may cause this playlist to implode.
    # Beware!
    'Local': [
        ("CurrentConditions",       [ 5*30]),
        ("RegionalObservationMap",  [ 5*30]),
        ("MetroObservationMap",     [ 5*30]),
        ("RegionalForecastMap",     [ 5*30]),
        ("MetroForecastMap",        [ 5*30]),
        ("MetroDopplerRadar",       [ 5*30]),
        ("RegionalDopplerRadar",    [ 5*30]),
        ("RadarSatelliteComposite", [ 5*30]),
        ("Satellite",               [ 5*30]),
        ("TextForecast",            [20*30]),
        ("AirQualityForecast",      [ 5*30]),
        ("Almanac",                 [ 5*30]),
        ("Climatology",             [ 5*30]),
        ("DaypartForecast",         [ 5*30]),
        ("ExtendedForecast",        [ 5*30]),
        ("GetawayForecast",         [ 5*30]),
        ("HeatSafetyTips",          [ 5*30]),
        ("MarineForecast",          [ 5*30]),
        ("NWSHeadlines",            [ 5*30, 5*30]),
        ("OutdoorActivityForecast", [ 5*30]),
        ("SchoolDayWeather",        [ 5*30]),
        ("RecordHighLow",           [ 5*30]),
        ("LocalObservations",       [ 5*30]),
        ("Tides",                   [ 5*30]),
        ("TrafficFlow",             [ 5*30, 5*30]),
        ("TrafficOverview",         [ 5*30, 5*30, 5*30]),
        ("TrafficReport",           [ 5*30, 5*30, 5*30]),
        ("7DayForecast",            [ 5*30]), 
    ],
    'Ldl': [
        ("LASCrawl",                [4*30]),
        ("TornadoWatch",            [4*30]),
        ("StarIDMessage",           [4*30]),
        ("CurrentSkyTemp",          [4*30]),
        ("CurrentWinds",            [4*30]),
        ("CurrentDewpoint",         [4*30]),
        ("CurrentGusts",            [4*30]),
        ("CurrentCeiling",          [4*30]),
        ("CurrentApparentTemp",     [4*30]),
        ("CurrentHumidity",         [4*30]),
        ("CurrentPressure",         [4*30]),
        ("CurrentVisibility",       [4*30]),
        ("CurrentMTDPrecip",        [4*30]),
        ("AirQualityForecast",      [4*30]),
        ("AirportDelayConditions",  [12*30]),
        ("ExtendedForecast",        [8*30]),
        ("HourlyForecast",          [12*30]),
        ("Shortcast",               [4*30]),
        ("SummaryForecast",         [4*30]),
        ("SunriseSunset",           [4*30]),
        ("TrafficTripTimes",        [4*30]),
        ("TravelForecast",          [4*30]),
        ("UVForecast",              [4*30]),
        ("Date",                    [4*30]),
    ],
    'BackgroundMusic': [
        ("Default",      [60*30]),
    ],
    'Tag': [
        ("Null",         [50*30]),
        ("Default",      [10*30]),
    ],

}


def getSchedule(duration):
    return _adjustScheduleLength(s, duration)


def _adjustScheduleLength(s, duration):
    news = {}
    
    for stype, pl in s.items():
        news[stype] = _adjustPlaylistLength(pl, duration)

    return news


def _adjustPlaylistLength(pl, duration):
    tally = 0
    newpl = []
    pllen = len(pl)
    
    # loop through each product to see if it exceeds the duration
    for prod, durlist in pl:
        dur = reduce(lambda a,b: a+b, durlist)
        newtally = tally + dur
        if newtally > duration:
            break
            
        newpl.append((prod, durlist))
        tally = newtally
        
        
    # pad out the last product (last page) to meet the duration
    remainder = duration - tally
    if remainder > 0:
        durlist = durlist[:-1] + [remainder]
        newpl.append((prod, durlist))
        
    return newpl

