Skip to content

Commit

Permalink
Fix some issues
Browse files Browse the repository at this point in the history
  • Loading branch information
joinemm committed Jan 13, 2024
1 parent c603c4f commit 5153444
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 31 deletions.
24 changes: 17 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -15,26 +15,36 @@ 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

# 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"]
41 changes: 22 additions & 19 deletions cogs/lastfm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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(
Expand Down
10 changes: 5 additions & 5 deletions modules/lastfm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 5153444

Please sign in to comment.