From abb355a6c57c7cbf1a675bd68ac50d4b1808814b Mon Sep 17 00:00:00 2001 From: WiredMind2 Date: Tue, 15 Oct 2024 22:25:22 +0200 Subject: [PATCH] Updated command !lesson --- .gitignore | 2 +- cogs/admin.py | 18 ++++++++++-- utils/github.py | 77 ++++++++++++++++++++++++++++++++++--------------- 3 files changed, 71 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index 12ade61..8dac351 100644 --- a/.gitignore +++ b/.gitignore @@ -20,4 +20,4 @@ token.* .env # NixOs related -.venv/ \ No newline at end of file +venv/ \ No newline at end of file diff --git a/cogs/admin.py b/cogs/admin.py index 7afda47..96533ee 100644 --- a/cogs/admin.py +++ b/cogs/admin.py @@ -1,5 +1,6 @@ from logging import getLogger from pathlib import Path +from urllib.parse import quote import discord import discord.ext.commands as commands @@ -38,17 +39,30 @@ async def lesson(self, ctx: commands.Context, repo: str = "", lesson: str = ""): Get an embed from the README of a given lesson in a given repo. If no repo is given, takes the repo named after the current schoolyear : `INSAlgo-{year1}-{year2}`. If no lesson is given, takes the lesson with the highest number. + You can pass a lesson number for the current year, or the year of the repo followed by the lesson number, or the name of either or both. """ - err_code, res = github_client.get_lesson_ressource(repo, lesson) + if lesson == "" : + lesson = repo + repo = "" + + err_code, repo, lesson = github_client.find_lesson_ressource(repo, lesson) + err_code, res = github_client.get_repo_readme(repo, lesson) + if err_code == 0: emb = embed_lesson(res).set_thumbnail(url="attachment://INSAlgo.png") logo = discord.File("data/INSAlgo.png", filename="INSAlgo.png") channel = self.bot.get_channel(RESSOURCES) + + if not emb.url.startswith('http'): + file = emb.url + url = f"https://github.com/INSAlgo/{repo}/blob/main/{lesson}/{file}" + emb.url = quote(url, safe=':/') + assert isinstance(channel, discord.TextChannel) await channel.send(file=logo, embed=emb) - + else : if err_code == 5 : res += "\nYou can also pass the exact repo name as an argument of this function." diff --git a/utils/github.py b/utils/github.py index 23ff88f..ef0abae 100644 --- a/utils/github.py +++ b/utils/github.py @@ -246,36 +246,67 @@ def get_INSAlgo_lessons(self, repo: str) -> tuple[int, str | list[str]] : return 0, [dir_["name"] for dir_ in self.lr_response(json=True)] - def get_lesson_ressource(self, repo: str = "", lesson: str = "") -> tuple[int, str] : + def find_lesson_ressource(self, repo: str = "", lesson: str = "") -> tuple[int, str] : + err_code, res = self.get_INSAlgo_repos() + + if err_code > 0 : + return 5, f"Could not get INSAlgo's repos :\n{res}" + if repo == "" : - err_code, res = self.get_INSAlgo_repos() - - if err_code > 0 : - return 5, f"Could not get INSAlgo's repos :\n{res}" - cur_year = datetime.today().year - cur_name = f"INSAlgo-{cur_year-1}-{cur_year}" + cur_name = f"INSAlgo-{cur_year}-{cur_year+1}" if cur_name not in res : - cur_name = f"INSAlgo-{cur_year}-{cur_year+1}" + cur_name = f"INSAlgo-{cur_year-1}-{cur_year}" if cur_name not in res : return 5, "Could not find an appropriate repo for the current year. Name should be `INSAlgo-{year1}-{year2}`." repo = cur_name - - if lesson == "" : - - err_code, res = self.get_INSAlgo_lessons(repo) - - if err_code > 0 : - return 6, f"Could not get lessons from `{repo}` :\n{res}" - - try : - lesson = max(res, key=lambda s: int(s.split(' ')[0])) - - except ValueError : - return 6, f"No valid lesson name found in `{repo}`. Check `https://github.com/INSAlgo/INSAlgo-2022-2023` for reference." - - return self.get_repo_readme(repo, lesson) + else: + found = False + if repo not in res and repo.isdigit() : + for repo_name in res: + if not repo_name.startswith("INSAlgo-") or len(repo_name.split('-')) != 3: continue + + _, year_a, year_b = repo_name.split('-') + if int(year_a) == int(repo): + repo = repo_name + found = True + break + + if not found: + return 5, "Could not find an appropriate repo for the current year. Name should be `INSAlgo-{year1}-{year2}`." + + # if lesson == "" : + + err_code, res = self.get_INSAlgo_lessons(repo) + + if err_code > 0 : + return 6, f"Could not get lessons from `{repo}` :\n{res}" + + if lesson == "": + best = 0 + for l in res: + num = l.split(' ')[0] + if num.isdigit() and int(num) > best: + best = int(num) + lesson = l + else: + found = False + for l in res: + if l == lesson: + found = True + break + elif lesson.isdigit(): + num = l.split(' ')[0] + if num.isdigit() and int(num) == int(lesson) : + lesson = l + found = True + break + + if not found: + raise Exception(f"No valid lesson name found in `{repo}`. Check `https://github.com/INSAlgo/INSAlgo-2022-2023` for reference.") + + return 0, repo, lesson github_client = GithubClient() \ No newline at end of file