-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalendrier.py
More file actions
333 lines (296 loc) · 11.8 KB
/
calendrier.py
File metadata and controls
333 lines (296 loc) · 11.8 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/python3.10
# -*- coding: utf-8 -*-
from __future__ import print_function
from datetime import datetime
from sys import argv
from googleapiclient.errors import HttpError
from googleApi import setService
import textFct
from eventCls import DatePerso, Event
from fileList import FileList
import loggerFct as log
"""
mise en place
https://developers.google.com/calendar/api/quickstart/python
projet calendrier deborah
"""
help =""" récupérer des évenements
utilisation
python3 %s tag
les valeurs de tag
regles: récupérer les dates de mes dernières règles
reves: " la liste de mes rêves
journal: " mon journal
poids: " " poids
depenses: " " dépenses
repas: " " repas
""" % __file__
dateStart = DatePerso (2026, 2, 21)
dateEnd = DatePerso.today()
dateEndStr = dateEnd.toStrDay()
"""
calNameList = ('deborah.powers89@gmail.com', 'sante', 'anniversaires et appels', 'quotidien', 'journal', 'boulot')
'anniversaires et appels': 'v6apvjh1jvlcf9m127b2d15ngg@group.calendar.google.com'
"""
calNameList = ('deborah.powers89@gmail.com', 'quotidien', 'journal')
calDict = {
'deborah.powers89@gmail.com': 'deborah.powers89@gmail.com',
'journal': '33akjnknr3mp1oletdkm8tdjog@group.calendar.google.com',
'quotidien': 'nb71ojvfs60b1lmm2re27kj8h4@group.calendar.google.com',
}
evtDict = {
'repas': ('quotidien', '2'),
'poids': ('quotidien', '2'),
'regles': ('quotidien', '7'),
'douleurs': ('quotidien', '2'),
'depenses': ('quotidien', '2'),
'journal': ('journal', '2'),
'reves': ('journal', '7'),
}
def keyByValue (dico, value):
keys = dico.keys()
res = None
for key in keys:
if dico[key] == value or value in dico[key]: res = key
return res
def getCalDict (service):
calDict = {}
calTmp = service.calendarList().list().execute()
calList = calTmp.get ('items', [])
for cal in calList:
calName = cal.get ('summary')
if calName in calNameList:
calDict [calName] = cal.get ('id')
return calDict
""" ____________________________________ classes personnelles simplifiant la manipulation des objets récupérés ____________________________________ """
class calendarGoogle():
def __init__ (self):
self.id =""
self.title =""
self.infos =""
self.color =""
def fromGoogleObj (self, tmpCal):
self.id = tmpCal.get ('id')
self.title = tmpCal.get ('summary')
self.infos = tmpCal.get ('description')
self.color = tmpCal.get ('colorId')
def fromId (self, service, calId):
tmpCal = service.calendarList().get (calendarId=calId).execute()
self.fromGoogleObj (tmpCal)
def fromName (self, service, calName):
if calName in calDict.keys(): self.fromId (service, calDict [calName])
def __str__ (self):
strCal = "id: %s\tnom: %s\tcouleur: %s\tinfos: %s" % (self.id, self.title, self.color, self.infos)
return strCal
class eventGoogle (calendarGoogle, Event):
def __init__ (self):
calendarGoogle.__init__ (self)
Event.__init__ (self)
self.idRec =""
def fromGoogleObj (self, tmpEvt):
calendarGoogle.fromGoogleObj (self, tmpEvt)
self.location = tmpEvt.get ('location')
dateKey = 'date'
if 'dateTime' in tmpEvt.get ('start').keys(): dateKey = 'dateTime'
self.date = DatePerso.fromGoogle (tmpEvt.get ('start').get (dateKey))
if dateKey != 'date':
newDate = DatePerso.fromGoogle (tmpEvt.get ('end').get (dateKey))
self.setDuration (newDate)
self.idRec = tmpEvt.get ('recurringEventId')
def equals (self, newEvt):
if Event.equals (self, newEvt) and self.idRec == newEvt.idRec: return True
else: return False
def __lt__ (self, newEvt):
return self.__str__() < newEvt.__str__()
def toLowercase (self):
self.category = self.category.lower()
self.title = self.title.lower()
self.infos = self.infos.lower()
self.location = self.location.lower()
self.location = self.location.replace (' paris', "")
self.location = self.location.replace (', france', "")
self.location = self.location.replace ('-le-grand, 93160 noisy-le-grand', "")
self.location = self.location.replace ('-malmaison, 92500 rueil-malmaison', "")
self.location = self.location.replace ('-les-moulineaux, 92130 issy-les-moulineaux', "")
""" __________________ récupérer un type d'évenement __________________ """
def getOneMeal (self):
if self.color == evtDict ['repas'][1] and self.title == 'repas' and self.date <= dateEnd:
dateMeal = self.date.toStrDay()
if dateMeal > dateEndStr: return None
if not self.infos: self.infos = 'non noté'
else: self.infos = self.infos.replace ('\n', '\t')
if self.date.hour >16: self.title = 'soir'
elif self.date.hour >11: self.title = 'midi'
else: self.title = 'matin'
evtStr = '%s\t%s\t%s' % (dateMeal, self.title, self.infos)
return evtStr
else: return None
def getOneWeight (self):
# log (self.title +'\t'+ self.color +'\t'+ evtDict ['poids'][1])
if self.color == evtDict ['poids'][1] and self.title.lower() == 'poids':
dateStr = self.date.toStrDay()
if dateStr > dateEndStr: return None
else:
self.infos = self.infos.replace ('\n', '\t')
evtStr = '%s\t%s' % (dateStr, self.infos)
return evtStr
else: return None
def getOnePeriod (self):
if self.color == evtDict ['regles'][1] and self.title.lower() in ('règles', 'regles'):
self.infos = self.infos.replace ('\n', '\t')
evtStr = '%s\t%s' % (self.date.toStrDay(), self.infos)
return evtStr
else: return None
def getOneWalk (self):
if self.title == 'Balade':
self.infos = self.infos.replace ('\r', "")
evtStr = '%s\n%s' % (self.date.toStrDay(), self.infos)
evtStr = evtStr.replace ('\n', '\n\t')
evtStr = evtStr.replace ('\r',"")
return evtStr
else: return None
def getOnePurchase (self):
if self.color == evtDict ['depenses'][1] and self.infos and self.title.lower() not in 'regles règles poids douleurs repas balade':
self.infos = self.infos.replace ('\r', "")
self.infos = self.infos.replace ('\n', " ")
details = 'inconnu'
if " " in self.infos:
d= self.infos.index (" ")
details = self.infos [d+1:]
self.infos = self.infos[:d]
# tmpList = self.infos.split (" "); cost = tmpList.pop (-1)
if self.infos =='0' or self.infos == '0.0': return None
# elif details: self.infos = self.infos +'\t'+ details
evtStr = '%s\t%s\t%s\t%s\t%s' % (self.date.toStrDay(), self.infos, self.title, self.location, details)
return evtStr
else: return None
def getOneDolor (self):
if self.title[0] in 'mM': log.message (self.title)
if self.title == 'douleur' or self.title == 'maladie':
self.infos = self.infos.replace ('.\n', '. ')
self.infos = self.infos.replace ('\n', '. ')
# strDate = self.date.toStrHour() +'\t'+ self.duration.toStrHour() +'\t'+ self.infos.replace ('. ', '\t')
strDate = self.date.toStrHour() +'\t%02d\t'+ self.infos
strDate = strDate % self.duration
# strDate = strDate.replace ('0/00/00 ',"")
return strDate
else: return None
def getOneDiary (self):
if self.color == evtDict ['journal'][1] and self.infos:
self.infos = textFct.shape (self.infos, 'reset upper')
# evtStr = '======================== %s ========================\n\ndate: %s\nlieu: %s\ntags: o\npersonnes: o\n\n%s\n\n'
evtStr = '== %s\n\ndate: %s\nlieu: %s\ntags: o\npersonnes: o\n\n%s\n\n'
evtStr = evtStr %(self.title, self.date.toStrDay(), self.location, self.infos)
evtStr = evtStr.replace ('None', "")
return evtStr
else: return None
def getOneDream (self):
if self.color == evtDict ['reves'][1]:
self.infos = textFct.shape (self.infos, 'reset upper')
evtStr = '== %s\n\ndate: %s\nlieu: %s\ntags: o\npersonnes: o\n\n%s\n\n'
evtStr = evtStr %(self.title, self.date.toStrDay(), self.location, self.infos)
evtStr = evtStr.replace ('None', "")
return evtStr
else: return None
class eventList (FileList):
def __init__ (self):
FileList.__init__ (self)
self.path = 'b/calendrier.txt'
self.fromPath()
def fromCalendar (self, service, calendar, dateMin, duplicate='False'):
evtListTmp = []
dateStrMin = dateMin.toUtz()
evtListTmp = service.events().list (calendarId=calendar.id, singleEvents=duplicate, orderBy='startTime', timeMin=dateStrMin).execute()
"""
if dateMax: ancien paramètre
dateStrMax = dateMax.toUtz()
evtListTmp = service.events().list (calendarId=calendar.id, singleEvents=duplicate, orderBy='startTime', timeMax=dateStrMax).execute()
"""
evtList = evtListTmp.get ('items', [])
for evt in evtList:
evtTmp = eventGoogle()
evtTmp.fromGoogleObj (evt)
if not evtTmp.color: evtTmp.color = calendar.color
if (calendar.title, evtTmp.color) in evtDict.values(): evtTmp.category = keyByValue (evtDict, (calendar.title, evtTmp.color))
else: evtTmp.category = 'divers'
evtTmp.title = evtTmp.title.strip()
evtTmp.title = evtTmp.title.lower()
if evtTmp.infos:
evtTmp.infos = evtTmp.infos.strip()
evtTmp.infos = evtTmp.infos.lower()
evtTmp.category = evtTmp.category.strip()
evtTmp.category = evtTmp.category.lower()
if evtTmp.location:
evtTmp.location = evtTmp.location.strip()
evtTmp.location = evtTmp.location.lower()
self.append (evtTmp)
def selectByCalendar (self, calId):
newList = eventList()
for evt in self.list:
if evt.calId ==calId: newList.append (evt)
return newList
def selectByColor (self, colId):
newList = eventList()
for evt in self.list:
if evt.color ==colId: newList.append (evt)
return newList
def select (self, calId, colId):
tmpList = self.selectByCalendar (calId)
newList = tmpList.selectByColor (colId)
return newList
def getAll (self):
# récupérer tous les calendriers
for calName in calDict.keys():
loggerFct.log (calName)
cal = calendarGoogle()
cal.fromName (service, calName)
# extraire tous leurs évênements
tmpList = eventList()
# tmpList.fromCalendar (service, cal, dateMin=dateStart, dateMax=dateEnd)
tmpList.fromCalendar (service, cal, dateStart)
self.extend (tmpList.list)
self.list.sort()
self.list.reverse()
evtList = FileList ('b/calendrier.tsv', '\n')
for evt in self.list: evtList.append (evt.date.__str__() +'\t'+ evt.category +'\t'+ evt.title)
evtList.write()
def getPastEvents (self, calName, func):
cal = calendarGoogle()
cal.fromName (service, calName)
# extraire tous leurs évênemnts
self.fromCalendar (service, cal, dateStart, True)
evtList = FileList ('b/calendrier.tsv', '\n')
evtList.fromPath()
for evt in self.list:
if not evt.infos: evt.infos =""
if not evt.location: evt.location =""
if not evt.category: evt.category =""
evt.toLowercase()
evtStr = func (evt)
if evtStr: evtList.append (evtStr)
evtList.reverse()
evtList.write (True)
def showColors():
colors = service.colors().get().execute()
# Print available calendarListEntry colors.
keys = colors['calendar'].keys()
for key in keys: print (key, colors['calendar'][key]['background'])
# Print available event colors.
keys = colors['event'].keys()
for key in keys: print (key, colors['event'][key]['background'], colors['event'][key]['foreground'])
# on appele ce script dans un autre script
evtList = eventList()
scopes =[ 'https://www.googleapis.com/auth/calendar.readonly' ]
service = setService ('calendar', scopes)
if __name__ != '__main__': pass
elif len (argv) <2: print (help)
elif argv[1] == 'journal': evtList.getPastEvents (evtDict ['journal'] [0], eventGoogle.getOneDiary)
elif argv[1] == 'reves': evtList.getPastEvents (evtDict ['reves'] [0], eventGoogle.getOneDream)
elif argv[1] == 'repas': evtList.getPastEvents (evtDict ['repas'] [0], eventGoogle.getOneMeal)
elif argv[1] == 'regles': evtList.getPastEvents (evtDict ['regles'] [0], eventGoogle.getOnePeriod)
elif argv[1] == 'poids': evtList.getPastEvents (evtDict ['poids'] [0], eventGoogle.getOneWeight)
elif argv[1] == 'depenses': evtList.getPastEvents (evtDict ['depenses'] [0], eventGoogle.getOnePurchase)
elif argv[1] == 'douleurs': evtList.getPastEvents (evtDict ['douleurs'] [0], eventGoogle.getOneDolor)
elif argv[1] == 'balade': evtList.getPastEvents (evtDict ['balade'] [0], eventGoogle.getOneWalk)
elif argv[1] == 'all': evtList.getAll()
else: print (help)