From 5153444b1de9c12fbea940d4a23b74fefbc99c82 Mon Sep 17 00:00:00 2001 From: Joinemm Date: Sat, 13 Jan 2024 21:21:33 +0200 Subject: [PATCH] Fix some issues --- Dockerfile | 24 +++++++++++++++++------- cogs/lastfm.py | 41 ++++++++++++++++++++++------------------- modules/lastfm.py | 10 +++++----- 3 files changed, 44 insertions(+), 31 deletions(-) diff --git a/Dockerfile b/Dockerfile index 5e05e8b..c97f186 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,7 +5,7 @@ # The builder image, used to build the virtual environment FROM python:3.11-buster as builder -RUN pip install poetry==1.4.2 +RUN pip install --no-cache-dir poetry==1.4.2 ENV POETRY_NO_INTERACTION=1 \ POETRY_VIRTUALENVS_IN_PROJECT=1 \ @@ -15,6 +15,8 @@ ENV POETRY_NO_INTERACTION=1 \ WORKDIR /app COPY pyproject.toml poetry.lock ./ + +# poetry complains if there is no readme for some reason RUN touch README.md RUN poetry install --without dev --no-root && rm -rf $POETRY_CACHE_DIR @@ -22,19 +24,27 @@ RUN poetry install --without dev --no-root && rm -rf $POETRY_CACHE_DIR # The runtime image, used to just run the code provided its virtual environment FROM python:3.11-slim-buster as runtime -RUN apt-get update -y -RUN apt-get install --no-install-recommends -y ffmpeg wget -RUN wget --progress=dot:giga https://github.com/isis-project/isis-fonts/blob/master/NanumGothic.ttf?raw=true -O NanumGothic.ttf +WORKDIR /app -RUN apt-get clean \ +# install ffmpeg that is used in some commands +RUN apt-get update -y \ + && apt-get install --no-install-recommends -y ffmpeg wget \ + && apt-get clean \ && rm -rf /var/lib/apt/lists/* +# get font used for memes +RUN wget --progress=dot:giga https://github.com/isis-project/isis-fonts/blob/master/NanumGothic.ttf?raw=true -O NanumGothic.ttf + +# copy over just the virtualenv from our builder image ENV VIRTUAL_ENV=/app/.venv \ - PATH="/app/.venv/bin:$PATH" \ - PYTHONUNBUFFERED=1 + PATH="/app/.venv/bin:$PATH" COPY --from=builder ${VIRTUAL_ENV} ${VIRTUAL_ENV} +# copy over the source code COPY . . +# don't buffer stdout, just show it normally +ENV PYTHONUNBUFFERED=1 + CMD ["python", "-O", "main.py"] diff --git a/cogs/lastfm.py b/cogs/lastfm.py index 187103e..b9eb585 100644 --- a/cogs/lastfm.py +++ b/cogs/lastfm.py @@ -119,12 +119,13 @@ def __str__(self): class ChartSizeArgument: - async def convert(self, ctx: MisoContext, argument: str): + @staticmethod + async def convert(ctx: MisoContext, argument: str): try: size = ChartSize(int(argument), int(argument)) except ValueError: try: - size = ChartSize(*map(lambda n: int(n), argument.split("x"))) + size = ChartSize(*map(int, argument.split("x"))) except ValueError: raise commands.BadArgument( f"Cannot convert `{argument}` into a chart size." @@ -159,26 +160,27 @@ class StrOrNp: def extract(self, data: dict): raise NotImplementedError - def parse(self, argument: str): + @staticmethod + def parse(argument: str): return argument async def convert(self, ctx: MisoContext, argument: str): stripped_argument = remove_mentions(argument) - if stripped_argument.lower() == "np": - assert isinstance(ctx.cog, LastFm) - if hasattr(ctx, "lastfmcontext"): - username = ctx.lfm.username - else: - ctxdata = await get_lastfm_username(ctx) - username = ctxdata[2] - - data = await ctx.cog.api.user_get_now_playing(username) - if data is None: - raise exceptions.CommandWarning("You have not listened to anything!") + if stripped_argument.lower() != "np": + return self.parse(stripped_argument) - return self.extract(data) + assert isinstance(ctx.cog, LastFm) + if hasattr(ctx, "lastfmcontext"): + username = ctx.lfm.username else: - return self.parse(stripped_argument) + ctxdata = await get_lastfm_username(ctx) + username = ctxdata[2] + + data = await ctx.cog.api.user_get_now_playing(username) + if data is None: + raise exceptions.CommandWarning("You have not listened to anything!") + + return self.extract(data) class ArtistArgument(StrOrNp): @@ -1286,7 +1288,7 @@ async def chart_factory( size: ChartSize, hide_labels=True, use_padding=False, - topster_labels: list[str] = list(), + topster_labels: list[str] | None = None, ): resolution = 1080 font_size = 2.5 @@ -1321,7 +1323,7 @@ async def chart_factory( "COLUMNS": size.width, "ALBUMS": albums, "USE_TOPSTER": bool(topster_labels), - "TOPSTER_LABELS": topster_labels, + "TOPSTER_LABELS": topster_labels or [], "TOPSTER_FONT_SIZE": f"{font_size}em", "LABEL_FONT_SIZE": f"{font_size/2}em", "RESOLUTION_WIDTH": f"{image_width}px", @@ -2212,7 +2214,8 @@ async def paginated_user_stat_embed( await RowPaginator(content, rows, **kwargs).run(ctx) - def ranked_list(self, data: list[tuple[int, str]]): + @staticmethod + def ranked_list(data: list[tuple[int, str]]): rows = [] for i, (playcount, name) in enumerate(data, start=1): rows.append( diff --git a/modules/lastfm.py b/modules/lastfm.py index a077fb2..3c2d6db 100644 --- a/modules/lastfm.py +++ b/modules/lastfm.py @@ -24,18 +24,18 @@ def int_bool(value: bool | None) -> int | None: def non_empty(data: dict): - if data: - return data - else: + if not data: raise exceptions.LastFMError(error_code=0, message="Last.fm returned no data") + return data + class LastFmImage: MISSING_IMAGE_HASH = "2a96cbd8b46e442fc41c2b86b821562f" CDN_BASE_URL = "https://lastfm.freetls.fastly.net/i/u/" - def __init__(self, hash: str): - self.hash = hash + def __init__(self, image_hash: str): + self.hash = image_hash @classmethod def from_url(cls, url: str):