Skip to content

Commit

Permalink
Updated command !lesson
Browse files Browse the repository at this point in the history
  • Loading branch information
WiredMind2 committed Oct 15, 2024
1 parent 6baea0f commit abb355a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ token.*
.env

# NixOs related
.venv/
venv/
18 changes: 16 additions & 2 deletions cogs/admin.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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."
Expand Down
77 changes: 54 additions & 23 deletions utils/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit abb355a

Please sign in to comment.