-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
86 lines (73 loc) · 2.78 KB
/
main.py
File metadata and controls
86 lines (73 loc) · 2.78 KB
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
"""
This program allows the user to upload their class schedule in text format and have it automatically uploaded
to their Google Calendar:
-In weekly intervals
-For the duration of a semester
-Each class has a different colour
This program only works for authorized users (as per google cloud console dashboard):
patrick.bonini13@gmail.com
"""
import datetime as dt
import os.path
import re
#text to google calendar
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
SCOPES = ['https://www.googleapis.com/auth/calendar']
def main ():
"""
If you're a first time user, signs in to your google account
"""
creds = None
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json')
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
with open('token.json', 'w') as token:
token.write(creds.to_json())
#Converts the text file into a list containing the different elements needed
textFromFile = open('schedule.txt', 'r')
text = textFromFile.read()
text = text.split('; ')
schedule = [i.split(', ') for i in text]
#Loops over the list
for i in range(len(schedule)):
#Assigns a different color code to each class
colors = [0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7]
currentColor = colors[i]
#Creates the calendar events
try:
service = build("calendar", "v3", credentials=creds)
event = {
'summary': schedule[i][0],
'Description': 'Event details',
'colorId': currentColor,
'start': {
'dateTime': schedule[i][1],
'timeZone': 'America/Chicago'
},
'end': {
'dateTime': schedule[i][2],
'timeZone': 'America/Chicago'
},
'location': schedule[i][3],
'recurrence': [
'RRULE:FREQ=WEEKLY;COUNT=13'
],
'attendees': [
{'email': 'patrick.bonini13@gmail.com'}
]
}
event = service.events().insert(calendarId='primary', body=event).execute()
print(f"Event created {event.get('htmlLink')}")
except HttpError as error:
print("Error occurred:", error)
if __name__ == '__main__':
main()