-
Notifications
You must be signed in to change notification settings - Fork 0
/
rooms.py
130 lines (106 loc) · 4.06 KB
/
rooms.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
"""
This script checks the availability of the meeting rooms in the Ostrava office.
It uses the Google Calendar API to query the availability of the meeting rooms
and prints the results to the console.
The script requires a credentials.json file in the same directory.
The credentials.json file can be obtained by following the instructions at
https://developers.google.com/calendar/quickstart/python
"""
from __future__ import print_function
import datetime
import os.path
import pickle
import colorama
from google.auth.transport.requests import Request
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
# If modifying these scopes, delete the file token.pickle.
SCOPES = [
"https://www.googleapis.com/auth/calendar.readonly",
"https://www.googleapis.com/auth/calendar.events",
]
colorama.init()
def authenticate():
"""
Authenticate the user and return the credentials.
Returns:
creds (google.oauth2.credentials.Credentials): The credentials of the user.
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists("token.pickle"):
with open("token.pickle", "rb") as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
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)
# Save the credentials for the next run
with open("token.pickle", "wb") as token:
pickle.dump(creds, token)
return creds
def get_calendar_list(service):
"""
Get the list of calendars from the Google Calendar API.
Args:
service (googleapiclient.discovery.Resource): The Google Calendar API service.
Returns:
calendar_list (list): A list of dictionaries containing the calendar information.
"""
page_token = None
calendar_list = []
while True:
calendar_list_response = (
service.calendarList().list(pageToken=page_token).execute()
)
for calendar_list_entry in calendar_list_response["items"]:
if "resource" in calendar_list_entry["id"]:
calendar_list.append(calendar_list_entry)
page_token = calendar_list_response.get("nextPageToken")
if not page_token:
break
return calendar_list
def check_availability(service, calendar_list_entry):
"""
Check the availability of a meeting room.
Args:
service (googleapiclient.discovery.Resource): The Google Calendar API service.
calendar_list_entry (dict): A dictionary containing the calendar information.
"""
print(
colorama.Style.RESET_ALL
+ calendar_list_entry["summary"].replace("Ostrava Office-3-", ""),
end=" is ",
)
free_busy_query = {
"timeMin": datetime.datetime.utcnow().isoformat() + "Z",
"timeMax": (
datetime.datetime.utcnow() + datetime.timedelta(minutes=30)
).isoformat()
+ "Z",
"timeZone": "Europe/London",
"items": [{"id": calendar_list_entry["id"]}],
}
free_busy_response = service.freebusy().query(body=free_busy_query).execute()
if free_busy_response["calendars"][calendar_list_entry["id"]]["busy"]:
print(
colorama.Fore.RED + "BUSY until:",
free_busy_response["calendars"][calendar_list_entry["id"]]["busy"][0][
"end"
][11:16],
)
else:
print(colorama.Fore.GREEN + "FREE")
def main():
creds = authenticate()
service = build("calendar", "v3", credentials=creds)
calendar_list = get_calendar_list(service)
for calendar_list_entry in calendar_list:
check_availability(service, calendar_list_entry)
if __name__ == "__main__":
main()