2024-04-17 19:05:44 +05:30
|
|
|
import os.path
|
2024-04-21 21:09:22 +05:30
|
|
|
|
2024-04-17 19:05:44 +05:30
|
|
|
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
|
|
|
|
|
|
|
|
SCOPES = ['https://www.googleapis.com/auth/calendar']
|
|
|
|
|
2024-04-22 22:59:01 +05:30
|
|
|
def create_google_calendar_event(calendar_id, name, description, location, date, start_time, end_time):
|
2024-04-17 19:05:44 +05:30
|
|
|
|
|
|
|
creds = None
|
|
|
|
|
|
|
|
# The file token.json stores the user's access and refresh tokens
|
|
|
|
if os.path.exists('token.json'):
|
|
|
|
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
|
|
|
|
|
|
|
|
# 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:
|
|
|
|
print("Login to your Google account. [THIS IS A ONE TIME PROCESS]")
|
|
|
|
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.json', 'w') as token:
|
|
|
|
token.write(creds.to_json())
|
|
|
|
|
|
|
|
try :
|
|
|
|
service = build('calendar', 'v3', credentials=creds)
|
|
|
|
start = f"{date}T{start_time}:00"
|
|
|
|
end = f"{date}T{end_time}:00"
|
|
|
|
event = {
|
|
|
|
'summary': name,
|
|
|
|
'location': location,
|
|
|
|
'description': description,
|
|
|
|
'start': {
|
|
|
|
'dateTime': start,
|
|
|
|
'timeZone': 'Asia/Kolkata',
|
|
|
|
},
|
|
|
|
'end': {
|
|
|
|
'dateTime': end,
|
|
|
|
'timeZone': 'Asia/Kolkata',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-04-21 21:09:22 +05:30
|
|
|
event = service.events().insert(calendarId=calendar_id, body=event).execute()
|
2024-04-17 19:05:44 +05:30
|
|
|
|
2024-04-22 22:59:01 +05:30
|
|
|
print('Google calendar: %s' % (event.get('htmlLink')))
|
2024-04-17 19:05:44 +05:30
|
|
|
return event
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
return None
|
|
|
|
|
|
|
|
# END
|