1
1
from googleapiclient .discovery import build
2
- from uuid import uuid4
3
2
from google .auth .transport .requests import Request
4
- from pathlib import Path
3
+ from google . oauth2 . credentials import Credentials
5
4
from google_auth_oauthlib .flow import InstalledAppFlow
5
+
6
+ from uuid import uuid4
6
7
from typing import Dict , List
7
- from pickle import load , dump
8
+ import os
9
+
10
+ SCOPES = ["https://www.googleapis.com/auth/calendar" ]
8
11
9
12
10
13
class CreateMeet :
11
- def __init__ (self , attendees : Dict [str , str ], event_time : Dict [str , str ], topic ):
14
+ def __init__ (self , attendees : Dict [str , str ],
15
+ event_time : Dict [str , str ], Topic ):
12
16
authe = self ._auth ()
13
- attendees = [{"email" : e } for e in attendees .values ()]
17
+ attendees_list = [{"email" : e } for e in attendees .values ()]
14
18
self .event_states = self ._create_event (
15
- attendees , event_time , authe , topic )
19
+ attendees_list , event_time , authe , Topic )
16
20
17
21
@staticmethod
18
- def _create_event (attendees : List [Dict [str , str ]], event_time , authe : build , topic ):
22
+ def _create_event (
23
+ attendees : List [Dict [str , str ]], event_time , authe : build , TopiC ):
19
24
event = {"conferenceData" : {"createRequest" : {"requestId" : f"{ uuid4 ().hex } " , "conferenceSolutionKey" : {"type" : "hangoutsMeet" }}},
20
25
"attendees" : attendees ,
21
26
"start" : {"dateTime" : event_time ["start" ], 'timeZone' : 'Asia/Kolkata' },
22
27
"end" : {"dateTime" : event_time ["end" ], 'timeZone' : 'Asia/Kolkata' },
23
- "summary" : topic ,
28
+ "summary" : TopiC ,
24
29
"reminders" : {"useDefault" : True }
25
30
}
26
31
event = authe .events ().insert (calendarId = "primary" , sendNotifications = True ,
@@ -29,23 +34,24 @@ def _create_event(attendees: List[Dict[str, str]], event_time, authe: build, top
29
34
30
35
@staticmethod
31
36
def _auth ():
32
- token_file , scopes = Path (
33
- "./token.pickle" ), ["https://www.googleapis.com/auth/calendar" ]
34
- credentials = None
35
- if token_file .exists ():
36
- with open (token_file , "rb" ) as token :
37
- credentials = load (token )
38
- if not credentials or not credentials .valid :
39
- if credentials and credentials .expired and credentials .refresh_token :
40
- credentials .refresh (Request ())
37
+ creds = None
38
+ if os .path .exists ("token.json" ):
39
+ creds = Credentials .from_authorized_user_file ("token.json" , SCOPES )
40
+ # If there are no (valid) credentials available, let the user log in.
41
+ if not creds or not creds .valid :
42
+ if creds and creds .expired and creds .refresh_token :
43
+ creds .refresh (Request ())
41
44
else :
42
45
flow = InstalledAppFlow .from_client_secrets_file (
43
- 'credentials.json' , scopes )
44
- credentials = flow .run_local_server (port = 0 )
45
- with open (token_file , "wb" ) as token :
46
- dump (credentials , token )
47
- calendar_service = build ("calendar" , "v3" , credentials = credentials )
48
- return calendar_service
46
+ "credentials.json" , SCOPES
47
+ )
48
+ creds = flow .run_local_server (port = 0 )
49
+ # Save the credentials for the next run
50
+ with open ("token.json" , "w" ) as token :
51
+ token .write (creds .to_json ())
52
+
53
+ service = build ("calendar" , "v3" , credentials = creds )
54
+ return service
49
55
50
56
51
57
print ('------------------------------' )
@@ -60,9 +66,10 @@ def _auth():
60
66
emails = list (
61
67
input ('Enter the emails of guests separated by 1 space each : ' ).strip ().split ())
62
68
topic = input ('Enter the topic of the meeting : ' )
69
+
63
70
time = {
64
- 'start' : date + 'T' + start + ':00.000000' ,
65
- 'end' : date + 'T' + end + ':00.000000'
71
+ 'start' : date + 'T' + start + ':00.000000' ,
72
+ 'end' : date + 'T' + end + ':00.000000'
66
73
}
67
74
guests = {email : email for email in emails }
68
75
meet = CreateMeet (guests , time , topic )
@@ -72,4 +79,4 @@ def _auth():
72
79
print ('-- Meeting Details --' )
73
80
print ('---------------------' )
74
81
for key in keys :
75
- print (key + ' : ' , details [key ])
82
+ print (key + ' : ' , details [key ])
0 commit comments