[docs]defget_google_credentials(token_file:Optional[str]=None,client_secrets_file:Optional[str]=None,scopes:Optional[List[str]]=None,)->Credentials:"""Get credentials."""# From https://developers.google.com/calendar/api/quickstart/pythonRequest,Credentials=import_google()InstalledAppFlow=import_installed_app_flow()creds=Nonescopes=scopesorDEFAULT_SCOPEStoken_file=token_fileorDEFAULT_CREDS_TOKEN_FILEclient_secrets_file=client_secrets_fileorDEFAULT_CLIENT_SECRETS_FILE# 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.ifos.path.exists(token_file):creds=Credentials.from_authorized_user_file(token_file,scopes)# If there are no (valid) credentials available, let the user log in.ifnotcredsornotcreds.valid:ifcredsandcreds.expiredandcreds.refresh_token:creds.refresh(Request())# type: ignore[call-arg]else:# https://developers.google.com/calendar/api/quickstart/python#authorize_credentials_for_a_desktop_application # noqaflow=InstalledAppFlow.from_client_secrets_file(client_secrets_file,scopes)creds=flow.run_local_server(port=0)# Save the credentials for the next runwithopen(token_file,"w")astoken:token.write(creds.to_json())returncreds
[docs]defbuild_resource_service(credentials:Optional[Credentials]=None,service_name:str="calendar",service_version:str="v3",)->Resource:"""Build a Google Calendar service."""credentials=credentialsorget_google_credentials()builder=import_googleapiclient_resource_builder()returnbuilder(service_name,service_version,credentials=credentials)
[docs]defis_all_day_event(start_datetime:str,end_datetime:str)->bool:"""Check if the event is all day."""try:datetime.strptime(start_datetime,"%Y-%m-%d")datetime.strptime(end_datetime,"%Y-%m-%d")returnTrueexceptValueError:returnFalse