def parseSchedule(schedule, channels):
    '''Parse the schedule and return a list of timings to check against.'''

    recordings = {}
    schedules = len(schedule)

    for x in xrange(0, schedules):
        entry = schedule[x] # should be a JSON object

        # Recording start time
        start = entry['start']
        dt = datetime.datetime.strptime(start, '%Y-%m-%d %H:%M:%S')

        channel = entry['channel'] # Extract the channel name

        # Check for an endtime or a duration
        endtime = None
        offset = None

        if 'end' in entry:
            endtime = datetime.datetime.strptime(entry['end'], '%Y-%m-%d %H:%M:%S')

        if 'duration' in entry:
            duration = entry['duration'] # Get the timer duration (minutes)
            offset = dt + datetime.timedelta(minutes=duration)

        # Check to see which gives the longer recording - the duration or end timestamp
        if offset is not None and endtime is not None:
            if offset > endtime:
                endtime = offset

        elif offset is not None:
            endtime = offset

        elif endtime is None and offset is None:
            # No valid duration/end time
            writePrint('End or duration missing for scheduled recording %s (%s).' % (dt, channel))
            continue

        elif endtime is not None and endtime < dt:
            # End is earlier than the start!
            writePrint('End timestamp earlier than start! Cannot record %s (%s).' % (dt, channel))

        programme = None

        if 'programme' in entry:
            programme = entry['programme']

        addr = channels[channel] # Get the channel URL
        pid = '%s %s' % (start, channel)

        recordings[pid] = {
            'url': addr,
            'channel': channel,
            'start':dt,
            'end': endtime,
            'programme': programme,
            'sid': x # Basic schedule id - this can be improved upon later to make it a unique identifier that is read/written by the schedule editor (or a database primary key)
        }

    return recordings
Parse the schedule and return a list of timings to check against.
recipes/Python/579096_vlcpy_stream_capture_scheduler/recipe-579096.py