Skip to content

Commit bd9f310

Browse files
committed
Adding support to Distance Learning
1 parent f26d8cf commit bd9f310

File tree

1 file changed

+175
-45
lines changed

1 file changed

+175
-45
lines changed

EDT.py

+175-45
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
# EDT.py
2-
31
import discord
42
from discord.ext import commands
53

64
import requests, json, html, re
75
from datetime import datetime, timedelta
86

7+
# Remote course: Displaying Zoom links with the calendar
8+
zoom_links = {
9+
"IN603": "https://uvsq-fr.zoom.us/j/XXXXXXXXX?pwd=XXXXXXXXXXXXXXXXX",
10+
"IN606_TD1": "https://uvsq-fr.zoom.us/j/XXXXXXXXX?pwd=XXXXXXXXXXXXXXXXX"
11+
}
12+
913
# Just a check to limit these commands to the authorized guilds
1014
def usage_check(ctx):
1115
authorized_guilds = [688084158026743816, 705373935243362347, 710518367253168199]
@@ -14,10 +18,16 @@ def usage_check(ctx):
1418
# Main request function, return a formatted array of modules (dicts)
1519
# @start_date @end_date : First and last day of the requested calendar
1620
# @TD : Group class
17-
def request_td_edt(start_date, end_date, TD):
21+
def request_td_edt(start_date, end_date, TD, LIC):
1822
url = 'https://edt.uvsq.fr/Home/GetCalendarData'
19-
TDList = ["S5 INFO TD 1","S5 INFO TD 2","S5 INFO TD 3","S5 INFO TD 4"]
20-
global_edt = []
23+
if LIC == 3:
24+
# TODO: Automate even/odd semester's deduction
25+
TDList = ["S6 INFO TD01","S6 INFO TD02","S6 INFO TD03","S6 INFO TD04"]
26+
elif LIC == 2:
27+
TDList = ["S4 INFO TD01","S4 INFO TD02","S4 INFO TD03","S4 INFO TD04"]
28+
else: # M1
29+
TDList = ["M1 SECRETS", "M1 AMIS", "M1 SECRETS", "M1 SECRETS"]
30+
2131

2232
data = {'start':start_date,'end':end_date,'resType':'103','calView':'agendaDay','federationIds[]':TDList[TD]}
2333
response = requests.post(url,data=data)
@@ -43,9 +53,30 @@ def request_td_edt(start_date, end_date, TD):
4353
"module":description.split("¨")[2],
4454
"groupes":description.split("¨")[3]
4555
}
46-
modules.append(sub_data)
56+
57+
# Remote course: We don't append the on-moodle-TD to avoid the double displaying
58+
if "TD" in sub_data["type"] and "MOODLE" in sub_data["salle"]:
59+
continue
60+
else:
61+
modules.append(sub_data)
62+
4763
# Sort the modules array by in ascending order of schedules
4864
modules = sorted(modules, key=lambda sub_data: sub_data["horaire"])
65+
66+
# Remote course: Adding zoom CM & TD links next to Module's names
67+
for module in modules:
68+
module["module"] = module["module"][:len(module["module"])-11]
69+
# if 'CM' in module['type']:
70+
# for zm in zoom_links:
71+
# if zm in module['module']:
72+
# module['module'] += f"\n[Lien Zoom disponible!]({zoom_links[zm]})"
73+
74+
# elif 'TD' in module['type']:
75+
# formatted = f"{module['module'][:5]}_TD{TD+1}"
76+
# if formatted in zoom_links:
77+
# module['module'] += f"\n[Lien Zoom disponible!]({zoom_links[formatted]})"
78+
79+
4980
return modules
5081

5182
# Return a specific thumbnail for each @weekday
@@ -72,15 +103,20 @@ def jour_de_la_semaine(start_date):
72103

73104
return "{} {:0>2d}/{:0>2d}".format(day, start_date.day, start_date.month)
74105

106+
def calculate_duration(modules):
107+
# TODO: Calculate total courses duration for a given day
108+
return 'X'
109+
75110
# Interpret the @daydate from the user's command and return two datetime objects @start_date & @end_date
76111
def date_formatting(daydate):
77112
# start_date = str(datetime.now())[:10] if daydate == None else '2020-{}-{}'.format(daydate.split("/")[1], daydate.split("/")[0])
78113
if daydate == None:
79114
start_date = str(datetime.now())[:10]
80-
elif int(daydate.split("/")[1]) > 9:
81-
start_date = '2020-{}-{}'.format(daydate.split("/")[1], daydate.split("/")[0])
82-
else:
115+
elif int(daydate.split("/")[1]) > 8:
116+
# TODO: Automate the current year's deduction
83117
start_date = '2021-{}-{}'.format(daydate.split("/")[1], daydate.split("/")[0])
118+
else:
119+
start_date = '2022-{}-{}'.format(daydate.split("/")[1], daydate.split("/")[0])
84120

85121
try:
86122
start_date = datetime.strptime(start_date, "%Y-%m-%d")
@@ -89,71 +125,165 @@ def date_formatting(daydate):
89125
except Exception as e:
90126
return -1
91127

128+
# Remote course: checks and returns the actual week
129+
# def get_semaine(modules):
130+
# for module in modules:
131+
# if "TD" in module["type"]:
132+
# if "A" in module["groupes"]:
133+
# return "Semaine A"
134+
# else:
135+
# return "Semaine B"
136+
# return ""
137+
138+
def get_m1_group(module):
139+
if 'gr 1' in module["groupes"]:
140+
return "TD1"
141+
elif 'gr 2' in module["groupes"]:
142+
return "TD2"
143+
elif 'gr 3' in module["groupes"]:
144+
return "TD3"
145+
else:
146+
return "None"
147+
148+
149+
async def send_day_edt(self, ctx, TDGR, daydate, LIC):
150+
TD = int(re.sub(r'[^1-4]', '', TDGR))
151+
152+
if isinstance(daydate, str):
153+
if isinstance(date_formatting(daydate), int):
154+
await ctx.send("La date {} n'est pas valide !".format(daydate))
155+
return
156+
else:
157+
start_date, end_date = date_formatting(daydate)
158+
else:
159+
start_date = daydate
160+
end_date = start_date + timedelta(days=0)
161+
162+
163+
modules = request_td_edt(start_date, end_date, TD-1, LIC)
164+
165+
# semaine = get_semaine(modules)
166+
# desc = "**{}** — {} créneaux ce jour".format(semaine, len(modules)) if len(modules) > 0 else 'Journée libre !'
167+
# if len(modules) == 1 : desc = "**{}** — {} créneau ce jour".format(semaine, len(modules))
168+
169+
if LIC == 1:
170+
lic_txt = 'M1 Info'
171+
elif LIC == 2:
172+
lic_txt = 'L2 Info'
173+
elif LIC == 3:
174+
lic_txt = 'L3 Info'
175+
176+
duration = calculate_duration(modules)
177+
178+
desc = f"{len(modules)} créneaux ce jour — {duration} heures au total" if len(modules) > 0 else 'Journée libre !'
179+
if len(modules) == 1 : desc = f"{len(modules)} créneau ce jour"
180+
181+
embed = discord.Embed(title="<:week:887402631482474506> {} TD{} — {}".format(lic_txt, TD, jour_de_la_semaine(start_date)), description=desc, color=0x3b8ea7, timestamp=datetime.utcnow())
182+
embed.set_thumbnail(url=url_jour(start_date.weekday()))
183+
embed.set_footer(text="Dernière actualisation", icon_url=self.bot.user.avatar_url)
184+
185+
if LIC == 1: # M1
186+
for module in modules:
187+
if "TD" in module["type"]:
188+
tdgrp = get_m1_group(module)
189+
embed.add_field(name=module["horaire"], value=f"{module['module']}\n<:L3:881594439552860281> **{tdgrp}** | {module['salle']}\n", inline=False)
190+
else:
191+
embed.add_field(name=module["horaire"], value="{}\n<:M1:881594437585752085> **{}** | {}\n".format(module["module"], module["type"], module["salle"]) + u"\u2063", inline=False)
192+
193+
else: # TODO: L3 & L2 daily and weekly calendar
194+
for module in modules:
195+
if "TD" in module["type"]:
196+
if "A" in module["groupes"]:
197+
embed.add_field(name=module["horaire"], value="{}\n{} | {}\n❯ Sous-groupe B en distanciel\n".format(module["module"], module["type"], module["salle"]) + u"\u2063", inline=False)
198+
else:
199+
embed.add_field(name=module["horaire"], value="{}\n{} | {}\n❯ Sous-groupe A en distanciel\n".format(module["module"], module["type"], module["salle"]) + u"\u2063", inline=False)
200+
else:
201+
embed.add_field(name=module["horaire"], value="{}\n{} | {}\n".format(module["module"], module["type"], module["salle"]) + u"\u2063", inline=False)
202+
203+
await ctx.channel.send(embed=embed)
204+
205+
92206
# EDT Cog Class
93207
class EDT(commands.Cog):
94208
def __init__(self, bot):
95209
self.bot = bot
96210

97211
# Return the calendar for a @TD Group and a given @daydate (default: today date)
98-
@commands.command(name="day")
99-
async def day(self, ctx, TDGR : str, daydate : str = None):
212+
@commands.command(name="dayl3")
213+
async def dayl3(self, ctx, TDGR : str, daydate : str = None):
100214
if not usage_check(ctx):
101215
print("EDT usage on the wrong guild.")
102216
return
103217

104-
TD = int(re.sub(r'[^1-4]', '', TDGR))
105-
106-
if isinstance(date_formatting(daydate), int):
107-
await ctx.send("La date {} n'est pas valide !".format(daydate))
218+
await send_day_edt(self, ctx, TDGR, daydate, 3)
219+
220+
@commands.command(name="dayl2")
221+
async def dayl2(self, ctx, TDGR : str, daydate : str = None):
222+
if not usage_check(ctx):
223+
print("EDT usage on the wrong guild.")
108224
return
109-
else:
110-
start_date, end_date = date_formatting(daydate)
111-
112-
modules = request_td_edt(start_date, end_date, TD-1)
113225

114-
desc = "{} créneaux ce jour".format(len(modules)) if len(modules) > 0 else 'Journée libre !'
115-
if len(modules) == 1 : desc = "{} créneau ce jour".format(len(modules))
226+
await send_day_edt(self, ctx, TDGR, daydate, 2)
116227

117-
embed = discord.Embed(title="<:week:755154675149439088> TD{} — {}".format(TD, jour_de_la_semaine(start_date)), description=desc, color=0x3b8ea7)
118-
for module in modules:
119-
embed.add_field(name=module["horaire"], value="{}\n{} | {}\n".format(module["module"][2:len(module["module"])-11], module["type"], module["salle"]) + u"\u2063", inline=False)
120-
embed.set_thumbnail(url=url_jour(start_date.weekday()))
121-
embed.set_footer(text="Dernière actualisation : {}".format(str(datetime.now())[:16]), icon_url=self.bot.user.avatar_url)
122-
await ctx.channel.send(embed=embed)
228+
@commands.command(name="daym1")
229+
async def daym1(self, ctx, daydate : str = None):
230+
if not usage_check(ctx):
231+
print("EDT usage on the wrong guild.")
232+
return
123233

234+
await send_day_edt(self, ctx, '1', daydate, 1)
235+
236+
124237
# Return the week calendar for a @TD Group and a given @daydate (default: today date)
125-
@commands.command(name="week")
126-
async def week(self, ctx, TDGR : str, daydate : str = None):
238+
@commands.command(name="weekm1")
239+
async def weekm1(self, ctx, daydate : str = None):
127240
if not usage_check(ctx):
128241
print("EDT usage on the wrong guild.")
129242
return
130243

131-
TD = int(re.sub(r'[^1-4]', '', TDGR))
244+
# TD = int(re.sub(r'[^1-4]', '', TDGR))
132245

133246
if isinstance(date_formatting(daydate), int):
134247
await ctx.send("La date {} n'est pas valide !".format(daydate))
135248
return
136249
else:
137250
start_date, end_date = date_formatting(daydate)
138-
251+
139252
while start_date.weekday() < 5:
140-
end_date = start_date + timedelta(days=0)
141-
modules = request_td_edt(start_date, end_date, TD-1)
142-
143-
desc = "{} créneaux ce jour".format(len(modules)) if len(modules) > 0 else 'Journée libre !'
144-
if len(modules) == 1 : desc = "{} créneau ce jour".format(len(modules))
145-
146-
embed = discord.Embed(title="<:week:755154675149439088> TD{} — {}".format(TD, jour_de_la_semaine(start_date)), description=desc, color=0x3b8ea7)
147-
for module in modules:
148-
embed.add_field(name=module["horaire"], value="{}\n{} | {}\n".format(module["module"][2:len(module["module"])-11], module["type"], module["salle"]) + u"\u2063", inline=False)
149-
embed.set_thumbnail(url=url_jour(start_date.weekday()))
150-
embed.set_footer(text="Dernière actualisation : {}".format(str(datetime.now())[:16]), icon_url=self.bot.user.avatar_url)
151-
await ctx.channel.send(embed=embed)
253+
# end_date = start_date + timedelta(days=0)
254+
255+
await send_day_edt(self, ctx, '1', start_date, 1)
152256
start_date = start_date + timedelta(days=1)
153257

258+
# modules = request_td_edt(start_date, end_date, TD-1, LIC)
259+
260+
# semaine = get_semaine(modules)
261+
# desc = "**{}** — {} créneaux ce jour".format(semaine, len(modules)) if len(modules) > 0 else 'Journée libre !'
262+
# if len(modules) == 1 : desc = "**{}** — {} créneau ce jour".format(semaine, len(modules))
263+
264+
265+
# embed = discord.Embed(title="<:week:887402631482474506> TD{} — {}".format(TD, jour_de_la_semaine(start_date)), description=desc, color=0x3b8ea7)
266+
267+
# for module in modules:
268+
# if "TD" in module["type"]:
269+
# if "A" in module["groupes"]:
270+
# embed.add_field(name=module["horaire"], value="{}\n{} | {}\n❯ Sous-groupe B en distanciel\n".format(module["module"], module["type"], module["salle"]) + u"\u2063", inline=False)
271+
# else:
272+
# embed.add_field(name=module["horaire"], value="{}\n{} | {}\n❯ Sous-groupe A en distanciel\n".format(module["module"], module["type"], module["salle"]) + u"\u2063", inline=False)
273+
# else:
274+
# embed.add_field(name=module["horaire"], value="{}\n{} | {}\n".format(module["module"], module["type"], module["salle"]) + u"\u2063", inline=False)
275+
# embed.set_thumbnail(url=url_jour(start_date.weekday()))
276+
# embed.set_footer(text="Dernière actualisation {}".format(str(datetime.now())[:16]), icon_url=self.bot.user.avatar_url)
277+
# await ctx.channel.send(embed=embed)
278+
279+
# start_date = start_date + timedelta(days=1)
280+
281+
154282
# Just some error sended warnings
155-
@week.error
156-
@day.error
283+
@weekm1.error
284+
@dayl2.error
285+
@dayl3.error
286+
@daym1.error
157287
async def test_on_error(self, ctx, error):
158288
await ctx.send("*{}*\n`{}{} [TD] [day/month]`".format(error, ctx.prefix, ctx.command))
159289

0 commit comments

Comments
 (0)