-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcalendar_craller.py
109 lines (94 loc) · 3.92 KB
/
calendar_craller.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
#!/bin/python3
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
import time
SCOPES = 'https://www.googleapis.com/auth/calendar'
BASE_URL = 'https://intra.epitech.eu'
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
store = file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('calendar', 'v3', http=creds.authorize(Http()))
def format_code_epitech(description):
desc = description.split()[0]
if desc[0] != '#':
return
if desc[1:11] != "codeevent=":
return
return desc[11:]
def get_google_event_list(CALENDAR_ID):
event_list = []
page_token = None
while True:
events = service.events().list(calendarId=CALENDAR_ID,
pageToken=page_token).execute()
for event in events['items']:
if 'description' in event:
desc = format_code_epitech(event['description'])
if desc is not None:
event_list.append(desc)
page_token = events.get('nextPageToken')
if not page_token:
break
return event_list
def format_time(h):
t = time.strptime(h, '%Y-%m-%d %H:%M:%S')
return time.strftime('%Y-%m-%dT%H:%M:%S', t)
def format_time_all_day(h):
t = time.strptime(h, '%Y-%m-%d %H:%M:%S')
return time.strftime('%Y-%m-%d', t)
def create_event_project(event_param, CALENDAR_ID):
event = {
"end": {
"date": format_time_all_day(event_param['end']),
"timeZone": "Europe/Paris"
},
"start": {
"date": format_time_all_day(event_param['start']),
"timeZone": "Europe/Paris"
},
"summary": event_param['title'] + ' | ' + event_param['module_title'],
"description": f'#codeevent={event_param["codeevent"]}\n{BASE_URL}/module/{event_param["scolaryear"]}/{event_param["codemodule"]}/{event_param["codeinstance"]}/{event_param["codeacti"]}',
'transparency': 'transparent',
}
print(event)
print(service.events().insert(calendarId=CALENDAR_ID,
body=event).execute())
def create_event(event_param, CALENDAR_ID):
if 'is_projet' in event_param:
create_event_project(event_param, CALENDAR_ID)
return
if 'rdv_group_registered' not in event_param or event_param['rdv_group_registered'] is None:
en = format_time(event_param['end'])
st = format_time(event_param['start'])
else:
a = event_param['rdv_group_registered'].split('|')
en = format_time(a[1])
st = format_time(a[0])
summary = event_param['acti_title'] if ('acti_title' in event_param) and \
(event_param['acti_title'] is not None) else 'No title'
location = event_param['room']['code'] if ('room' in event_param) and \
(event_param['room'] is not None) and \
('code' in event_param['room']) and \
(event_param['room']['code'] is not None) else ''
event = {
"end": {
"dateTime": en,
"timeZone": "Europe/Paris"
},
"start": {
"dateTime": st,
"timeZone": "Europe/Paris"
},
"summary": summary,
"location": location,
"description": f'#codeevent={event_param["codeevent"]}\n{BASE_URL}/module/{event_param["scolaryear"]}/{event_param["codemodule"]}/{event_param["codeinstance"]}/{event_param["codeacti"]}',
}
print(event)
print(service.events().insert(calendarId=CALENDAR_ID,
body=event).execute())