Added deadlines command
This commit is contained in:
parent
adbbc6352b
commit
70867e0d7e
2 changed files with 44 additions and 0 deletions
13
cogs/ksp.py
13
cogs/ksp.py
|
@ -14,6 +14,19 @@ class Ksp(commands.Cog):
|
||||||
f'Solution: {task_link(task_code, solution=True)}',
|
f'Solution: {task_link(task_code, solution=True)}',
|
||||||
ephemeral=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):
|
def setup(bot):
|
||||||
bot.add_cog(Ksp(bot))
|
bot.add_cog(Ksp(bot))
|
||||||
|
|
|
@ -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"
|
KSP_URL = "https://ksp.mff.cuni.cz"
|
||||||
|
|
||||||
def task_link(task_code: str, solution : bool = False):
|
def task_link(task_code: str, solution : bool = False):
|
||||||
return f"{KSP_URL}/viz/{task_code}/{'reseni' if solution else ''}"
|
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
|
||||||
|
|
Loading…
Reference in a new issue