-
Notifications
You must be signed in to change notification settings - Fork 1
/
api-google-calendar.py
188 lines (161 loc) · 7.17 KB
/
api-google-calendar.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
from __future__ import print_function
import datetime
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
import SGBD
# Alcove Europe - Toulouse - 1 - Grise
id_objet_reservable = 1
def get_id_utilisateur(mail):
# à coder (voir si on garde le mail)
# SGBD.createConnection()
id_utilisateur = 666
# SGBD.closeConnection()
return id_utilisateur
# id_utilisateur = get_id_utilisateur()
""""gerer les ID des events + gerer le fait de devoir se co à chaque fois + faire un if pour ne pas ajouter un event
s'il y en a deja un de prevu à cette heure + ne pas pouvoir reserver hors des horaires d'ouverture + creer une fonction duree
+ faire une fonction qui trouve l'id de l'utilisateur en cours"""
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/calendar']
def se_connecter():
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# 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:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server()
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('calendar', 'v3', credentials=creds)
def creer_event(date, heure_debut, heure_fin, promo, nb_pers):
# format 'AAAA-MM-JJ' date
# format 'HH-MM-SS' heure_d
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# 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:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server()
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('calendar', 'v3', credentials=creds)
event = {
'summary': 'Occupé',
'location': '',
'description': promo + str(nb_pers),
'start': {
'dateTime': date + 'T' + heure_debut + ':00+02:00',
'timeZone': 'Europe/Paris',
},
'end': {
'dateTime': date + 'T' + heure_fin + ':00+02:00',
'timeZone': 'Europe/Paris',
},
}
event = service.events().insert(calendarId='primary', body=event).execute()
print('Event created: %s' % (event.get('htmlLink')))
### voir si on garde le mail
mail = 'test@h.fr'
id_utilisateur = get_id_utilisateur(mail)
###partie base de donnée
cnx = SGBD.createConnection()
msg = SGBD.ajout_reserver(cnx, 2, date, 120, heure_debut, id_objet_reservable, id_utilisateur, nb_pers)
SGBD.closeConnection(cnx, cnx.cursor()) # a rendre plus joli
return msg
def supprimer_event(date, heure_debut):
# format 'AAAA-MM-JJ' date
# format 'HH-MM-SS' heure_d
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# 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:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server()
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('calendar', 'v3', credentials=creds)
event_id = 0
list_event_id,list_date,list_heure_debut = prochains_event(date,heure_debut)
for i in range(len(list_event_id)):
if date==list_date[i] :
if heure_debut+':00'==list_heure_debut[i] :
event_id = list_event_id[i]
print(event_id)
service.events().delete(calendarId='primary', eventId=event_id).execute()
def prochains_event(date,heure_debut):
datetime_event_debut=date+'T'+heure_debut+':00+02:00'
datetime_event_fin=date+'T'+'19:30'+':00+02:00'
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# 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:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server()
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('calendar', 'v3', credentials=creds)
# Call the Calendar API
# now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
# print('Getting the upcoming 20 events')
events_result = service.events().list(calendarId='primary', timeMin=datetime_event_debut,timeMax=datetime_event_fin,
maxResults=20, singleEvents=True,
orderBy='startTime').execute()
events = events_result.get('items', [])
if not events:
print('No upcoming events found.')
date = []
eventId = []
heure_debut = []
for event in events:
start = event['start'].get('dateTime', event['start'].get('date'))
eventId.append(event.get('id'))
l = start.split("T")
date.append(l[0])
heure_debut.append( l[1].split('+')[0])
# print(start, event['summary'], event.get('id'))
return eventId,date,heure_debut
if __name__ == '__main__':
supprimer_event('2019-05-22','09:00')