从 google 日历 API 中检索摘要信息
Retrieve summary information from google calendar API
我有一个“完美”的代码。我可以在我的日历中添加一个事件,但在添加它之前,它会检查时间段是否被占用。如果未占用时隙,则添加事件。如果它被占用,它会告诉我已经存在的事件的详细信息。
问题是有几个人可以在同一个时间范围内有约会,但我的代码不会让它发生。我需要我的代码来检查 'summary',因为它将包含忙碌人员的姓名。抱歉我的英语不好,我是法语所以下面是一个例子。
我需要的示例:如果我的日历中已经有上午 10 点到下午 1 点为 James(摘要)注册的约会。我决定在上午 10 点到下午 1 点为玛丽添加一个新约会,它应该让我添加它。因为他们是两个不同的人。但是,如果我想再次为 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
令牌:
# 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()
我有一个“完美”的代码。我可以在我的日历中添加一个事件,但在添加它之前,它会检查时间段是否被占用。如果未占用时隙,则添加事件。如果它被占用,它会告诉我已经存在的事件的详细信息。
问题是有几个人可以在同一个时间范围内有约会,但我的代码不会让它发生。我需要我的代码来检查 'summary',因为它将包含忙碌人员的姓名。抱歉我的英语不好,我是法语所以下面是一个例子。
我需要的示例:如果我的日历中已经有上午 10 点到下午 1 点为 James(摘要)注册的约会。我决定在上午 10 点到下午 1 点为玛丽添加一个新约会,它应该让我添加它。因为他们是两个不同的人。但是,如果我想再次为 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
令牌:
# 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()