First 'working' version
This commit is contained in:
124
gcal.py
Normal file
124
gcal.py
Normal file
@@ -0,0 +1,124 @@
|
||||
import datetime
|
||||
import os.path
|
||||
|
||||
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
|
||||
# The file token.json 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.json"):
|
||||
creds = Credentials.from_authorized_user_file("token.json", SCOPES)
|
||||
# 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.json", "w") as token:
|
||||
token.write(creds.to_json())
|
||||
|
||||
try:
|
||||
service = build("calendar", "v3", credentials=creds)
|
||||
|
||||
|
||||
calendars_to_use = [\
|
||||
# "meneelyl@gmail.com",\
|
||||
"Family",\
|
||||
# "Tim Meneely2",\
|
||||
# "meneelyt@gmail.com",\
|
||||
"1532 Ingomar Heights Road",\
|
||||
"Vivienne",\
|
||||
"Benjamin",\
|
||||
"Madeline"\
|
||||
]
|
||||
|
||||
|
||||
calendarids = []
|
||||
calendarsummaries = []
|
||||
calendar_list = (service.calendarList().list()).execute().get("items", [])
|
||||
for result in calendar_list:
|
||||
if result.get("summary",[]) in calendars_to_use:
|
||||
calendarids.append(result.get("id",[]))
|
||||
calendarsummaries.append(result.get("summary",[]))
|
||||
#print("result",result.get("summary",[]),result.get("id",[]))
|
||||
# print("summaries",calendarsummaries)
|
||||
#print("first result",calendar_list[0])
|
||||
#print("first result",calendar_list[len(calendar_list)-1])
|
||||
#result meneelyl@gmail.com meneelyl@gmail.com
|
||||
#result Family family14345103599446093558@group.calendar.google.com
|
||||
#result Meneely Birthdays fjlvq3dnhfvn76ihljocbrhrdc@group.calendar.google.com
|
||||
#result Tim Meneely2 k7iohvqfspej540e37bqr1hkso@group.calendar.google.com
|
||||
#result meneelyt@gmail.com meneelyt@gmail.com
|
||||
#result 1532 Ingomar Heights Road 3a0104e2f97668033b30227726b79f48b9f341a84cd9fa57aa4e23851aee1750@group.calendar.google.com
|
||||
#result Vivienne fb53f1ba30abe33a395cb3483aaba416675e2ac829e9e74ca25c10d4fae593c5@group.calendar.google.com
|
||||
#result Benjamin ed47cdac4f626432af8c0d1956499d258d0365cf3fe6ee70ce73d7d478b0f1dd@group.calendar.google.com
|
||||
#result Madeline c6eed2e9ec829d835357032a344d2f6894677a6e652d04345c73249cd9bff8fb@group.calendar.google.com
|
||||
|
||||
|
||||
|
||||
# Call the Calendar API
|
||||
now = datetime.datetime.now(tz=datetime.timezone.utc).isoformat()
|
||||
tomorrow = (datetime.datetime.now(tz=datetime.timezone.utc)+datetime.timedelta(days=3)).isoformat()
|
||||
#print("Getting the upcoming 10 events")
|
||||
allevents = []
|
||||
for i in range(0,len(calendarids)):
|
||||
events_result = (
|
||||
service.events()
|
||||
.list(
|
||||
calendarId=calendarids[i],
|
||||
timeMin=now,
|
||||
timeMax=tomorrow,
|
||||
maxResults=10,
|
||||
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:
|
||||
# start = event["start"].get("dateTime", event["start"].get("date"))
|
||||
#print(start, event["summary"],"(from "+calendarsummaries[i]+")")
|
||||
#print("type events",type(events),"type event",type(event))
|
||||
#allevents.append(events)
|
||||
for event in events:
|
||||
event["calendarname"]=calendarsummaries[i]
|
||||
allevents.append(event)
|
||||
#print("allevents",allevents)
|
||||
allevents2 = sorted(allevents, key=lambda event: event["start"].get("dateTime", event["start"].get("date")))
|
||||
#for event in allevents2:
|
||||
# start = event["start"].get("dateTime", event["start"].get("date"))
|
||||
# print(start, event["summary"],"(from "+calendarsummaries[event["mycalendarkey"]]+")")
|
||||
except HttpError as error:
|
||||
print(f"An error occurred: {error}")
|
||||
allevents2 = []
|
||||
return allevents2
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
allevents = (main())
|
||||
for event in allevents:
|
||||
start = event["start"].get("dateTime", event["start"].get("date"))
|
||||
print(start, event["summary"],"(from "+event["calendarname"]+")")
|
||||
Reference in New Issue
Block a user