-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalendar_today.py
More file actions
67 lines (51 loc) · 2.05 KB
/
Copy pathcalendar_today.py
File metadata and controls
67 lines (51 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Install dependencies
#pip install google-api-python-client google-auth google-auth-oauthlib google-auth-httplib2
from datetime import datetime, timedelta
import os.path
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from google.auth.transport.requests import Request
SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']
def get_calendar_service():
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())
service = build('calendar', 'v3', credentials=creds)
return service
def get_today_events(service):
now = datetime.utcnow()
start_of_day = now.replace(hour=0, minute=0, second=0)
end_of_day = start_of_day + timedelta(days=1)
events_result = service.events().list(
calendarId='primary',
timeMin=start_of_day.isoformat() + 'Z',
timeMax=end_of_day.isoformat() + 'Z',
singleEvents=True,
orderBy='startTime'
).execute()
return events_result.get('items', [])
def display_events(events):
if not events:
print("No events today 🎉")
return
print("\nToday's Events:\n")
for event in events:
start = event['start'].get('dateTime', event['start'].get('date'))
start_time = datetime.fromisoformat(start.replace('Z', ''))
time_str = start_time.strftime('%H:%M')
title = event.get('summary', '(No title)')
print(f"{time_str} – {title}")
if __name__ == "__main__":
service = get_calendar_service()
events = get_today_events(service)
display_events(events)