88 lines
2.9 KiB
Python
88 lines
2.9 KiB
Python
|
|
import datetime
|
||
|
|
import subprocess
|
||
|
|
import os
|
||
|
|
|
||
|
|
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
|
||
|
|
|
||
|
|
# If modifying these scopes, delete the file token.json.
|
||
|
|
SCOPES = ["https://www.googleapis.com/auth/calendar.readonly"]
|
||
|
|
|
||
|
|
|
||
|
|
def main():
|
||
|
|
"""Shows basic usage of the Google Calendar API.
|
||
|
|
Prints the start and name of the next 10 events on the user's calendar.
|
||
|
|
"""
|
||
|
|
creds = None
|
||
|
|
if os.path.exists("token.json"):
|
||
|
|
creds = Credentials.from_authorized_user_file("token.json", SCOPES)
|
||
|
|
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())
|
||
|
|
|
||
|
|
try:
|
||
|
|
service = build("calendar", "v3", credentials=creds)
|
||
|
|
|
||
|
|
# Call the Calendar API
|
||
|
|
now = datetime.datetime.utcnow().isoformat() + "Z"
|
||
|
|
# time_change = datetime.timedelta(minutes=5)
|
||
|
|
# print(now)
|
||
|
|
# now2 = now + time_change
|
||
|
|
# now2 = now2.isoformat() + "Z"
|
||
|
|
# print(now2)
|
||
|
|
events_result = (
|
||
|
|
service.events()
|
||
|
|
.list(
|
||
|
|
calendarId="primary",
|
||
|
|
timeMin=now,
|
||
|
|
maxResults=4,
|
||
|
|
singleEvents=True,
|
||
|
|
orderBy="startTime",
|
||
|
|
)
|
||
|
|
.execute()
|
||
|
|
)
|
||
|
|
events = events_result.get("items", [])
|
||
|
|
|
||
|
|
if not events:
|
||
|
|
print("No upcoming events found.")
|
||
|
|
return
|
||
|
|
|
||
|
|
# Prints the start and name of the next 10 events
|
||
|
|
for event in events:
|
||
|
|
# Parse the datetime first
|
||
|
|
start = event["start"].get("dateTime", event["start"].get("date"))[:-6]
|
||
|
|
# start = start[:-6]
|
||
|
|
start_time = datetime.datetime.strptime(start, "%Y-%m-%dT%H:%M:%S")
|
||
|
|
now = datetime.datetime.now()
|
||
|
|
|
||
|
|
difference = start_time - now
|
||
|
|
seconds_in_day = 24 * 60 * 60
|
||
|
|
time_left_in_min_sec = divmod(difference.days * seconds_in_day + difference.seconds, 60)
|
||
|
|
|
||
|
|
# Then parse the name to attribute it to a client.
|
||
|
|
client_name = event["summary"]
|
||
|
|
# if time_left_in_min_sec[0] < 1:
|
||
|
|
# print("Upcoming Meeting")
|
||
|
|
# else:
|
||
|
|
# print("Nothing new")
|
||
|
|
# print(start, event["organizer"]["email"])
|
||
|
|
if event["eventType"] != "outOfOffice":
|
||
|
|
# print(event["organizer"]["email"])
|
||
|
|
for items in event["attendees"]:
|
||
|
|
print(items["email"])
|
||
|
|
|
||
|
|
except HttpError as error:
|
||
|
|
print(f"An error occurred: {error}")
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
main()
|