-
Notifications
You must be signed in to change notification settings - Fork 2
/
calendar_event_feature.py
144 lines (120 loc) · 4.83 KB
/
calendar_event_feature.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from datetime import datetime
import os.path
from colorama import Fore
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
from google.auth.exceptions import RefreshError
SCOPES = ["https://www.googleapis.com/auth/calendar.readonly"]
def authenticate_calendar_api():
creds = None
if os.path.exists("Ctoken.json"):
creds = Credentials.from_authorized_user_file("Ctoken.json")
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
try:
creds.refresh(Request())
except RefreshError as e:
print(Fore.RED+"Error while refreshing calender token")
else:
flow = InstalledAppFlow.from_client_secrets_file("credentials.json", SCOPES)
creds = flow.run_local_server(port=0)
with open("Ctoken.json", "w") as token:
token.write(creds.to_json())
return creds
def events_calendar(print_event_details: bool):
try:
creds = authenticate_calendar_api()
service = build("calendar", "v3", credentials=creds)
now = datetime.utcnow().isoformat() + "Z" # 'Z' indicates UTC time
# *print("Getting the upcoming 10 events")
events_result = (
service.events()
.list(
calendarId="primary",
timeMin=now,
maxResults=10,
singleEvents=True,
orderBy="startTime",
)
.execute()
)
events = events_result.get("items", [])
c = 0
if not events:
print("No upcoming events found.")
return c
# Prints the start and name of the next 10 events
for event in events:
location = event.get("location", "not given")
start = event["start"].get("dateTime", event["start"].get("date"))
start_datetime = datetime.fromisoformat(start)
e_time = start_datetime.strftime("%d/%b/%Y %I:%M %p")
if print_event_details:
print(f"Event Name: {event['summary']}")
print(f"Description: {event.get('description', 'No description')}")
print(f"Start Time: {e_time}")
print(f"Event Location: {location}")
print(f"Event Link: {event.get('htmlLink', 'No link available')}\n\n")
c += 1
except HttpError as error:
print(f"An error occurred: {error}")
return c
def get_events_for_current_date(print_event_details: bool):
creds = authenticate_calendar_api()
service = build("calendar", "v3", credentials=creds)
now = datetime.utcnow().isoformat() + "Z" # 'Z' indicates UTC time
# *print("Getting today's upcoming events")
events_result = (
service.events()
.list(
calendarId="primary",
timeMin=now,
maxResults=10,
singleEvents=True,
orderBy="startTime",
)
.execute()
)
events = events_result.get("items", [])
c = 0
# Prints the start and name of the next 10 events
for event in events:
# Get the current date in the format YYYY-MM-DD
current_date = datetime.now().date()
start = event["start"].get("dateTime", event["start"].get("date"))
start_datetime = datetime.fromisoformat(start)
e_time1 = start_datetime.strftime("%d/%b/%Y %I:%M %p")
e_time = start_datetime.strftime("%Y-%m-%d")
if str(current_date) == e_time:
if print_event_details:
location = event.get("location", "not given")
print(f"Event Name: {event['summary']}")
print(f"Description: {event.get('description', 'No description')}")
print(f"Start Time: {e_time1}")
print(f"Event Location: {location}")
print(f"Event Link: {event.get('htmlLink', 'No link available')}\n\n")
c += 1
else:
print("No upcoming event's for Today.")
return c
def eventpop_date():
creds = authenticate_calendar_api()
service = build("calendar", "v3", credentials=creds)
now = datetime.utcnow().isoformat() + "Z" # 'Z' indicates UTC time
# *print("Getting today's upcoming events")
events_result = (
service.events()
.list(
calendarId="primary",
timeMin=now,
maxResults=10,
singleEvents=True,
orderBy="startTime",
)
.execute()
)
events = events_result.get("items", [])
return events