-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush_deadlines.py
More file actions
90 lines (65 loc) · 2.88 KB
/
Copy pathpush_deadlines.py
File metadata and controls
90 lines (65 loc) · 2.88 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from datetime import datetime,timedelta
import os.path
import pytz
from retrieval import fetch_deadlines
from email_parser import fetch_today_email_deadlines
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
SCOPES = [
"https://www.googleapis.com/auth/gmail.modify",
"https://www.googleapis.com/auth/calendar",
] # union of scopes used across the app
# authenticating
def get_calendar_service():
"""Authenticate and return Google Calendar API 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(
"client_secret.json", SCOPES
)
creds = flow.run_local_server(port=0)
with open("token.json", "w") as token:
token.write(creds.to_json())
return build("calendar", "v3", credentials=creds)
def push_event(service, deadlines):
"""Push deadlines dict to Google Calendar."""
try:
# Example from deadliness dict: "Sunday, August 03, 2025 12:00 AM"
for deadline in deadlines :
due_str = deadline["Due_date"]
due_dt = datetime.strptime(due_str, "%A, %B %d, %Y %I:%M %p")
tz = pytz.timezone("Asia/Kolkata") # changing to my timezone
due_dt = tz.localize(due_dt)
due_dt -= timedelta(days=1) # adding the event to the calendar on prev day itself so that i dont have to worry abt it on the last minute🥸
due_iso = due_dt.isoformat() # formatting it into RFC3339 (ISO8601) string (the format Google Calendar expects).
event = {
"summary": deadline["Name"],
"description": "Deadlines extracted from LMS/Email",
"start": {"dateTime": due_iso, "timeZone": "Asia/Kolkata"},
"end": {"dateTime": due_iso, "timeZone": "Asia/Kolkata"},
}
created = service.events().insert(calendarId="primary", body=event).execute()
print("Event added:", created.get("htmlLink"))
except HttpError as error:
print(f"An error occurred: {error}")
def main():
service = get_calendar_service()
# Fetch LMS deadlines for the month
lms_deadlines = fetch_deadlines()
# Fetch today's email-derived deadlines
email_deadlines = fetch_today_email_deadlines()
combined = list(lms_deadlines) + list(email_deadlines)
if not combined:
print("No deadlines to push today.")
return
push_event(service, combined)
if __name__ == "__main__":
main()