-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSchedulebot.py
136 lines (119 loc) · 5.36 KB
/
Schedulebot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from sys import exit
import datetime
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
import csv_reader
from itertools import cycle, islice
from pprint import pprint
# to grant read/write access to Calendars
scopes = ['https://www.googleapis.com/auth/calendar']
credentials = None
possibleSchedTypes = csv_reader.possibleSchedTypes
extractedSched = csv_reader.extractedData
# set default timezone
# eg, my classes are at CST, while I am at PDT
defaultTimeZone = "America/Chicago"
try:
with open('token.pickle', 'rb') as token:
credentials = pickle.load(token)
except:
if not credentials or not credentials.valid:
if credentials and credentials.expired and credentials.refresh_token:
credentials.refresh(Request())
else:
print("""Something wrong with token. Run the load.py file first
and verify pickle file exists and credentials are valid.""")
raise
service = build('calendar', 'v3', credentials=credentials)
# Call the Calendar API
now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
print('Getting the upcoming 10 events in primary calendar')
events_result = service.events().list(calendarId='primary', timeMin=now,
maxResults=10, singleEvents=True,
orderBy='startTime').execute()
events = events_result.get('items', [])
if not events:
print('No upcoming events found.')
for event in events:
start = event['start'].get('dateTime', event['start'].get('date'))
print(start, event['summary'])
calendarList = service.calendarList().list().execute()['items']
calendar_id = calendarList[0]['id']
def createEvent(summary, location, description, startTime, endTime, timezone, givenCalendarID):
event = {
'summary': summary,
'location': location,
'description': description,
'start': {
'dateTime': startTime,
'timeZone': timezone,
},
'end': {
'dateTime': endTime,
'timeZone': timezone,
},
'reminders': {
'useDefault': True,
},
}
service.events().insert(calendarId=givenCalendarID, body=event,sendNotifications=True).execute()
if __name__ == '__main__':
print("Attempting to create events.")
try:
dateOnLoop = datetime.datetime.strptime(input("What is the starting date in m/d/yyyy?\n"), '%m/%d/%Y').date()
print("What is the starting schedule type?\nPossible schedules: ", end="")
print(*possibleSchedTypes, sep = ", ")
strSchedStart = input()
if strSchedStart not in possibleSchedTypes:
raise Exception("Schedule type not recognized")
duration = int(input("How long will this schedule last in days (as a number, ie 5)?\n"))
if input("Does the schedule skip weekends?\ny/n ") in ["Y", "y", "Yes", "yes"]:
skipWeekends = True
else:
skipWeekends = False
if input("Should the events be added to the calendar of the same name? If no, events will be added to main calendar. \ny/n ") in ["Y", "y", "Yes", "yes"]:
separateEventsByCalendar = True
else:
separateEventsByCalendar = False
except:
print("Cannot understand format")
raise
schedKey = possibleSchedTypes.index(strSchedStart)
desiredSched = list(islice(cycle(possibleSchedTypes), schedKey, schedKey+duration))
eventsCreated = []
for schedOnLoop in desiredSched:
for period in extractedSched[schedOnLoop]:
print(period)
# For each period in each schedule type
timeStart = extractedSched[schedOnLoop][period]["Start Time"]
timeEnd = extractedSched[schedOnLoop][period]["End Time"]
summary = extractedSched[schedOnLoop][period]["Summary"]
location = extractedSched[schedOnLoop][period]["Location"]
description = extractedSched[schedOnLoop][period]["Description"]
strStartDate = str(dateOnLoop) + timeStart
strEndDate = str(dateOnLoop) + timeEnd
startDate = datetime.datetime.strptime(strStartDate, '%Y-%m-%d%I:%M %p').isoformat()
endDate = datetime.datetime.strptime(strEndDate, '%Y-%m-%d%I:%M %p').isoformat()
print(summary)
print(location)
print(startDate)
print(endDate)
eventsCreated.append(summary)
foundCalendarID = 'primary'
if separateEventsByCalendar:
for calendarListEntry in calendarList:
if calendarListEntry['summary'] == summary:
foundCalendarID = calendarListEntry['id']
break
createEvent(summary, location, description, startDate, endDate, defaultTimeZone, foundCalendarID)
if skipWeekends and dateOnLoop.weekday() == 4:
dateOnLoop = dateOnLoop + datetime.timedelta(days=3)
else:
dateOnLoop = dateOnLoop + datetime.timedelta(days=1)
print("In total, I created", len(eventsCreated), "events over", duration, "days.")
if input("Do you want to see a complete list of the events?\ny/n ") in ["Y", "y", "Yes", "yes"]:
print("Here is a list of the events I created:")
pprint(eventsCreated)