Simplified some more logic in les.py

pull/53/head
Jef Roosens 2021-04-25 12:48:21 +02:00
parent 08fe799a30
commit 2988aca22b
Signed by: Jef Roosens
GPG Key ID: B580B976584B5F30
1 changed files with 71 additions and 45 deletions

View File

@ -10,12 +10,14 @@ import json
def createCourseString(courses): def createCourseString(courses):
courseString = "" courseString = ""
for course in sorted(courses, key=lambda item: item["slot"]["time"][1]): for course in sorted(courses, key=lambda item: item["slot"]["time"][1]):
# Add a ":" to the hour + add a leading "0" if needed # Add a ":" to the hour + add a leading "0" if needed
start = timeFormatters.timeFromInt(course["slot"]["time"][1]) start = timeFormatters.timeFromInt(course["slot"]["time"][1])
end = timeFormatters.timeFromInt(course["slot"]["time"][2]) end = timeFormatters.timeFromInt(course["slot"]["time"][2])
courseString += "{} - {}: {} {}\n".format(start, end, courseString += "{} - {}: {} {}\n".format(start, end,
str(course["course"]), getLocation(course["slot"])) str(course["course"]), getLocation(course["slot"]))
return courseString return courseString
@ -41,27 +43,38 @@ def createEmbed(day, dayDatetime, semester, year, schedule):
embed = discord.Embed(colour=discord.Colour.blue(), title=title) embed = discord.Embed(colour=discord.Colour.blue(), title=title)
embed.set_author(name="Lessenrooster voor {}{} Bachelor".format(year, "ste" if year == 1 else "de")) embed.set_author(name="Lessenrooster voor {}{} Bachelor".format(year, "ste" if year == 1 else "de"))
# The only if statement we'd wish to be True
if len(courses) == 0: if len(courses) == 0:
embed.add_field(name="Geen Les", value="Geen Les", inline=False) embed.add_field(name="Geen Les", value="Geen Les", inline=False)
else:
courseString = createCourseString(courses)
# TODO uncomment this when covid rules slow down
# courseString += "\nGroep {} heeft vandaag online les.".format(1 if week % 2 == 0 else 2)
embed.description = courseString
if prev: return embed
embed.add_field(name="Vakken uit vorige jaren", value=createCourseString(prev), inline=False)
if extras: courseString = createCourseString(courses)
embed.add_field(name="Extra", value="\n".join(getExtras(extra) for extra in extras), inline=False) # TODO uncomment this when covid rules slow down
# courseString += "\nGroep {} heeft vandaag online les.".format(1 if week % 2 == 0 else 2)
embed.description = courseString
# Add online links - temporarily removed because everything is online right now if prev:
if online: embed.add_field(name="Vakken uit vorige jaren", value=createCourseString(prev), inline=False)
uniqueLinks: dict = getUniqueLinks(online)
embed.add_field(name="Online Links", value="\n".join( if extras:
sorted(getLinks(onlineClass, links) for onlineClass, links in uniqueLinks.items()))) embed.add_field(name="Extra", value="\n".join(getExtras(extra) for extra in extras), inline=False)
# Add online links - temporarily removed because everything is online right now
if online:
uniqueLinks: dict = getUniqueLinks(online)
embed.add_field(
name="Online Links",
value="\n".join(
sorted(
getLinks(onlineClass, links)
for onlineClass, links in uniqueLinks.items()
)
)
)
embed.set_footer(text="Semester {} | Lesweek {}".format(semester, round(week)))
embed.set_footer(text="Semester {} | Lesweek {}".format(semester, round(week)))
return embed return embed
@ -74,6 +87,7 @@ def findDate(targetWeekday):
now = timeFormatters.dateTimeNow() now = timeFormatters.dateTimeNow()
while now.weekday() != targetWeekday: while now.weekday() != targetWeekday:
now = now + datetime.timedelta(days=1) now = now + datetime.timedelta(days=1)
return now return now
@ -144,13 +158,17 @@ def getCourses(schedule, day, week):
if week in slot["weeks"]: if week in slot["weeks"]:
if "custom" not in course: if "custom" not in course:
courses.append(classDic) courses.append(classDic)
extras.append(classDic) extras.append(classDic)
elif "weeks" in slot and "online" in slot and "group" not in slot: elif "weeks" in slot and "online" in slot and "group" not in slot:
# This class is only online for this week # This class is only online for this week
if week in slot["weeks"]: if week in slot["weeks"]:
if "custom" not in course: if "custom" not in course:
courses.append(classDic) courses.append(classDic)
extras.append(classDic) extras.append(classDic)
else: else:
# Nothing special happening, just add it to the list of courses # Nothing special happening, just add it to the list of courses
# in case this is a course for everyone in this year # in case this is a course for everyone in this year
@ -195,11 +213,13 @@ def getExtras(extra):
extra["slot"]["group"], extra["course"], location, extra["slot"]["group"], extra["course"], location,
start, end start, end
) )
elif "online" in extra["slot"]: elif "online" in extra["slot"]:
return "**{}** gaat vandaag uitzonderlijk **online** door {} van **{} tot {}**.".format( return "**{}** gaat vandaag uitzonderlijk **online** door {} van **{} tot {}**.".format(
extra["course"], location[7:], extra["course"], location[7:],
start, end start, end
) )
else: else:
return "**{}** vindt vandaag uitzonderlijk plaats **{}** van **{} tot {}**.".format( return "**{}** vindt vandaag uitzonderlijk plaats **{}** van **{} tot {}**.".format(
extra["course"], location, extra["course"], location,
@ -232,11 +252,15 @@ def getLinks(onlineClass, links):
Function that returns a formatted string giving a hyperlink Function that returns a formatted string giving a hyperlink
to every online link for this class today. to every online link for this class today.
""" """
return "{}: {}".format(onlineClass, return "{}: {}".format(
" | ".join( onlineClass,
["**[{}]({})**".format(platform, url) for platform, url in " | ".join(
links.items()]) [
) f"**[{platform}]({url})**"
for platform, url in links.items()
]
)
)
def getLocation(slot): def getLocation(slot):
@ -302,12 +326,15 @@ def getWeekDay(day=None):
if day is not None: if day is not None:
if day[0] == "morgen": if day[0] == "morgen":
dayNumber += 1 dayNumber += 1
elif day[0] == "overmorgen": elif day[0] == "overmorgen":
dayNumber += 2 dayNumber += 2
else: else:
for i in range(5): for i in range(5):
if weekDays[i].startswith(day): if weekDays[i].startswith(day):
dayNumber = i dayNumber = i
# Weekends should be skipped # Weekends should be skipped
dayNumber = dayNumber % 7 dayNumber = dayNumber % 7
if dayNumber > 4: if dayNumber > 4:
@ -321,32 +348,31 @@ def parseArgs(day):
semester = int(config.get("semester")) semester = int(config.get("semester"))
year = int(config.get("year")) year = int(config.get("year"))
years_counter = int(config.get("years")) years_counter = int(config.get("years"))
# Check if a schedule or a day was called # Check if a schedule or a day was called
if len(day) == 0: if len(day) == 1:
day = [] # Called a schedule
else: if day[0].isdigit():
# Only either of them was passed if not (0 < int(day[0]) < years_counter + 1):
if len(day) == 1: return [False, "Dit is geen geldige jaargang."]
# Called a schedule
if day[0].isdigit(): year = int(day[0])
if 0 < int(day[0]) < years_counter + 1: day = []
year = int(day[0])
day = [] # elif: calling a weekday is automatically handled below,
else: # so checking is obsolete
return [False, "Dit is geen geldige jaargang."] elif len(day) > 1:
# elif: calling a weekday is automatically handled below, # TODO check other direction (di 1) in else
# so checking is obsolete # Both were passed
else: if day[0].isdigit():
# TODO check other direction (di 1) in else if not (0 < int(day[0]) < years_counter + 1):
# Both were passed return [False, "Dit is geen geldige jaargang."]
if day[0].isdigit():
if 0 < int(day[0]) < years_counter + 1: year = int(day[0])
year = int(day[0])
# day = [] # Cut the schedule from the string
else: day = day[1:]
return [False, "Dit is geen geldige jaargang."]
# Cut the schedule from the string
day = day[1:]
day = getWeekDay(None if len(day) == 0 else day)[1] day = getWeekDay(None if len(day) == 0 else day)[1]
dayDatetime = findDate(timeFormatters.weekdayToInt(day)) dayDatetime = findDate(timeFormatters.weekdayToInt(day))