我有一个代码,工作'。完美'. 我可以在我的日历中添加一个事件,但在添加之前,它会检查时间段是否被占用。如果时间段没有被占用,它就会添加事件。如果它的采取,它告诉我的事件已经存在的细节。
问题是,有几个人谁可以有预约在同一时间范围内,但我的代码将不会让它发生。我需要我的代码来检查 "摘要",因为它将包含忙碌的人的名字。对不起,我的英语,IM法国,所以这里是一个例子下面。
我需要的例子 : 如果在我的日历中已经有一个詹姆斯(摘要)上午10点至下午1点的登记预约,而我决定为玛丽添加一个上午10点至下午1点的新预约,它应该让我添加。而我决定在上午10点至下午1点为Mary添加一个新的预约,它应该让我添加。因为他们是两个不同的人。但是,如果我想再为James添加一个上午10点的预约,显然它不会让我添加,因为他在这个时间段已经有一个预约了。
这是我目前有效的代码(见第1段阅读它的作用
进口:
from __future__ import print_function
import datetime
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
TOKEN :
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/calendar']
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.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)
如果在要求的时间没有事件,请添加事件。
service = build('calendar', 'v3', credentials=creds)
event_summary = 'Jad'
event_location = 'H7N 5H9'
event_description = 'Test'
event_start = '2020-04-29T14:00:00-00:00'
event_end = '2020-04-29T16:00:00-00:00'
# Call the Calendar API
now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
print('Looking if already created - if not, will create')
events_result = service.events().list(calendarId='primary', timeMin=event_start,
maxResults=1, singleEvents=True,
orderBy='startTime').execute()
events = events_result.get('items',[])
if not events :
event = {
'summary': event_summary,
'location': event_location,
'description': event_description,
'start': {
'dateTime': event_start,
'timeZone': 'America/New_York',
},
'end': {
'dateTime': event_end,
'timeZone': 'America/New_York',
},
'reminders': {
'useDefault': True,
},
}
event = service.events().insert(calendarId='primary', body=event).execute()
print('new event created')
如果事件已经存在,显示事件。
for event in events:
print('Cannot create new event because time slot already taken by : ')
start = event['start'].get('dateTime', event['start'].get('date'))
print(start, event['summary'], event['location'])
if __name__ == '__main__':
main()
所以我认为答案很简单。首先让我们检查一下你的代码。
你已经有了创建和提交事件的代码,以及读取事件的代码。我建议你这样做。
event['summary']
的值,并查看是否与每个事件的 event_summary
,也就是你之前定义的变量。你会希望使用某种布尔标志来指示是否找到了匹配。如果有匹配,设置标志并中断循环。编辑:解决方案是这样的。
service = build('calendar', 'v3', credentials=creds)
event_summary = 'Jad'
event_location = 'H7N 5H9'
event_description = 'Test'
event_start = '2020-04-29T14:00:00-00:00'
event_end = '2020-04-29T16:00:00-00:00'
# Call the Calendar API
now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
print('Looking if already created - if not, will create')
events_result = service.events().list(calendarId='primary', timeMin=event_start,
maxResults=1, singleEvents=True,
orderBy='startTime').execute()
events = events_result.get('items',[])
found_match = False
for event in events:
if event['summary'] == event_summary:
print('Cannot create new event because time slot already taken by : ')
start = event['start'].get('dateTime', event['start'].get('date'))
print(start, event['summary'], event['location'])
found_match = True
break
if not(found_match):
event = {
'summary': event_summary,
'location': event_location,
'description': event_description,
'start': {
'dateTime': event_start,
'timeZone': 'America/New_York',
},
'end': {
'dateTime': event_end,
'timeZone': 'America/New_York',
},
'reminders': {
'useDefault': True,
},
}
event = service.events().insert(calendarId='primary', body=event).execute())
if __name__ == '__main__':
main()