From 70867e0d7e7ec24b01bf7a434c102bb64cd43b44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Sk=C3=BDpala?= Date: Thu, 30 Mar 2023 13:30:16 +0200 Subject: [PATCH] Added deadlines command --- cogs/ksp.py | 13 +++++++++++++ utils/ksp_utils.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/cogs/ksp.py b/cogs/ksp.py index 7177428..6ca3dd6 100644 --- a/cogs/ksp.py +++ b/cogs/ksp.py @@ -14,6 +14,19 @@ class Ksp(commands.Cog): f'Solution: {task_link(task_code, solution=True)}', ephemeral=True ) + + @discord.slash_command() + async def deadlines(self, ctx): + a_deadlines = active_deadlines() + if len(a_deadlines) == 0: + return await ctx.respond("No running series.", ephemeral=True) + + msg = "" + for series_id, deadline in a_deadlines: + msg += f"Sérii **{series_id}** odevzdávejte do " + \ + deadline.strftime('%-d. %-m. %Y %-H:%M') + '\n' + + return await ctx.respond(msg, ephemeral=True) def setup(bot): bot.add_cog(Ksp(bot)) diff --git a/utils/ksp_utils.py b/utils/ksp_utils.py index e256957..624feca 100644 --- a/utils/ksp_utils.py +++ b/utils/ksp_utils.py @@ -1,4 +1,35 @@ +from typing import List, Tuple, Optional +from datetime import datetime +import requests +import json + KSP_URL = "https://ksp.mff.cuni.cz" def task_link(task_code: str, solution : bool = False): return f"{KSP_URL}/viz/{task_code}/{'reseni' if solution else ''}" + +def catalog(year : Optional[int] = None, tasks : bool = False): + year_str = "" + if year is not None: + year_str = f"year={year}" + tasks_str = f"tasks={str(tasks).lower()}" + + page = requests.get(f"{KSP_URL}/api/tasks/catalog?{year_str}&{tasks_str}") + return json.loads(page.text) + +def active_deadlines() -> List[Tuple[str, datetime]]: + deadlines = [] + now = datetime.now().astimezone() + cat = catalog() + for series in cat: + series_deadlines = [series["deadline"]] + if "deadline2" in series: + series_deadlines.append(series["deadline2"]) + series_deadlines = list(map(datetime.fromisoformat, series_deadlines)) + + for series_deadline in series_deadlines: + if now < series_deadline: + deadlines.append((series["id"], series_deadline)) + break + + return deadlines