def schedule(allocated_time, tasks): output = [] counter = 1 time_a = allocated_time[0].end time_b = allocated_time[1].start thirty = timedelta(seconds = 1800) onefifty = timedelta(seconds = 9000) while(True): remove = [] for i in range(0,len(tasks)): if(tasks[i][1].minutes <= 2): remove.append(tasks[i]) tasks[i][1].update(time_b) for r in remove: tasks.remove(r) tasks.sort() if(len(tasks) <= 0 or counter >= (len(allocated_time)-2)): break delta = time_b - time_a delta = delta.days * 1440 + delta.seconds / 60 if(delta >= 180 and tasks[0][1].minutes >= 120): new = timeBlock(time_a + thirty, time_a + onefifty, tasks[0][1].name) time_a = new.end tasks[0][1].dec(2) continue elif(delta >= 150 and tasks[0][1].minutes >= 90): new = timeBlock(time_a + thirty, time_b - thirty, tasks[0][1].name) tasks[0][1].dec(1.5) elif(delta >= 120 and tasks[0][1].minutes >= 60): new = timeBlock(time_a + thirty, time_b - thirty, tasks[0][1].name) tasks[0][1].dec(1) elif(delta >= 90 and tasks[0][1].minutes >= 30): new = timeBlock(time_a + thirty, time_b - thirty, tasks[0][1].name) tasks[0][1].dec(.5) output.append(new) counter += 1 time_a = allocated_time[counter].end time_b = allocated_time[counter+1].start return output
def main(): """Shows basic usage of the Google Calendar API. Creates a Google Calendar API service object and outputs a list of the next 10 events on the user's calendar. """ now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time print('Getting the upcoming 10 events') eventsResult = service.events().list( calendarId='primary', timeMin=now, maxResults=20, singleEvents=True, orderBy='startTime').execute() events = eventsResult.get('items', []) allocated_time = [] if not events: print('No upcoming events found.') for event in events: name = str(event['summary']) start = (event['start'].get('dateTime')) end = (event['end'].get('dateTime')) start = datetime.datetime.strptime(start[:19], '%Y-%m-%dT%H:%M:%S') end = datetime.datetime.strptime(end[:19], '%Y-%m-%dT%H:%M:%S') block = timeBlock(start, end, name) allocated_time.append(block) allocated_time.sort(key=lambda x: x.start, reverse=False) tasks = [] f = open('input', 'r') g = f.read() text = g.split('\n') for a in text: if len(a) == 0: break temp = Task(a) heappush(tasks, (temp.prio, temp)) f.close() tasks.sort() output = schedule(allocated_time, tasks) credentials.revoke(http) get_credentials(); for o in output: name = o.name s = datetime.datetime.strftime(o.start, '%Y-%m-%dT%H:%M:%S') e = datetime.datetime.strftime(o.end, '%Y-%m-%dT%H:%M:%S') event = { 'summary': name, 'start': { 'dateTime': s, 'timeZone': 'America/Los_Angeles', },'end': { 'dateTime': e, 'timeZone': 'America/Los_Angeles', } } event = service.events().insert(calendarId='primary', body=event).execute()