-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
117 lines (97 loc) · 3.56 KB
/
main.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
import datetime
import requests
import caldav_manager
import os
ERR_WEBHOOK_URL = os.getenv("ERR_WEBHOOK_URL", "")
INTRA_TOKEN = os.getenv("INTRA_TOKEN", "")
SYNC_START = os.getenv("SYNC_START", "2024-01-01")
SYNC_END = os.getenv("SYNC_END", "2025-12-31")
def send_error_webhook(error):
requests.post(ERR_WEBHOOK_URL, json={
'content': f"```{error}```"
})
def get_activities(start, end):
res = requests.get("https://intra.epitech.eu/module/board/?format=json&start=" + start + "&end=" + end,
cookies={
"user": INTRA_TOKEN
})
return res.json()
def get_planning(start, end):
r = requests.get('https://intra.epitech.eu/planning/load', params={
'start': start,
'end': end,
'format': 'json',
}, cookies={
'user': INTRA_TOKEN
})
if r.status_code != 200:
send_error_webhook(f"Error while fetching planning: {r.status_code} {r.text}")
return []
return r.json()
def get_next_date(current, days):
return (current + datetime.timedelta(days=days)).strftime("%Y-%m-%d")
def run():
print("Starting synchronization")
print("Fetching planning from intra")
planning = get_planning(SYNC_START, SYNC_END)
activities = get_activities(SYNC_START, SYNC_END)
minified = []
minified_activities = []
print("Formatting data")
print(planning)
for event in planning:
if "calendar_type" in event and event["calendar_type"] == "perso":
continue
if event['rdv_indiv_registered'] is None and event['rdv_group_registered'] is None:
if event['semester'] not in [0, 1, 2]:
continue
if not event['module_registered']:
continue
if not event['register_student']:
continue
if not event['event_registered'] in ['present', 'registered']:
continue
if event['type_code'] == 'rdv':
jkey = 'rdv_indiv_registered' if event['rdv_indiv_registered'] is not None else 'rdv_group_registered'
if event[jkey] is None:
start = event['start']
end = event['end']
else:
start = event[jkey].split('|')[0]
end = event[jkey].split('|')[1]
else:
start = event['start']
end = event['end']
minified.append({
'id': event['codeevent'],
'title': event['acti_title'],
'start': start,
'location': event['room']['code'].split('/')[-1] if event['room'] is not None and "code" in event[
"room"] else "Pas de salle",
'end': end,
'type': event['type_code'],
})
if "message" in activities and activities['message'] == "Veuillez vous connecter":
send_error_webhook("Veuillez vous connecter à l'intra pour obtenir un cookie valide.")
return
for act in activities:
if not act['registered']:
continue
if not act['type_acti_code'] == 'proj':
continue
beg_date = act["begin_acti"]
end_date = act["end_acti"]
minified_activities.append({
'id': act['codeacti'],
'title': act['acti_title'],
'start': beg_date,
'end': end_date,
})
print("Sorting data")
minified.sort(key=lambda x: x['start'])
print("Synchronizing data")
caldav_manager.synchronize(minified, minified_activities)
print("All events synchronized !")
if __name__ == '__main__':
run()
exit(0)