Skip to content

Commit

Permalink
fix(lastfm): handle missing album covers in /lfmcollage command
Browse files Browse the repository at this point in the history
Fixed a bug where the bot did not respond to the /lfmcollage command if one of the albums in the collage lacked a cover.
  • Loading branch information
HitaloM committed Sep 13, 2024
1 parent d9c0ac7 commit 3f2b5a7
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 18 deletions.
24 changes: 15 additions & 9 deletions locales/bot.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2024-09-12 16:23-0300\n"
"POT-Creation-Date: 2024-09-13 14:02-0300\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -801,52 +801,58 @@ msgstr ""
msgid "<b>Supported languages</b>:\n"
msgstr ""

#: src/korone/modules/piston/handlers/run.py:23
#: src/korone/modules/piston/handlers/run.py:24
msgid ""
"You need to provide a command to run. Example: <code>/piston python "
"print('Hello, World!')</code>"
msgstr ""

#: src/korone/modules/piston/handlers/run.py:33
#: src/korone/modules/piston/handlers/run.py:34
msgid "Sorry, I couldn't fetch the available languages. Please try again later."
msgstr ""

#: src/korone/modules/piston/handlers/run.py:40
#: src/korone/modules/piston/handlers/run.py:41
msgid ""
"Invalid language. Use <code>/pistonlangs</code> to see the available "
"languages. Then use it like this: <code>/piston python print('Hello, "
"World!')</code>"
msgstr ""

#: src/korone/modules/piston/handlers/run.py:52
#: src/korone/modules/piston/handlers/run.py:53
msgid ""
"You need to provide a valid language and code. Example: <code>/piston "
"python print('Hello, World!')</code>"
msgstr ""

#: src/korone/modules/piston/handlers/run.py:62
#: src/korone/modules/piston/handlers/run.py:63
msgid "An error occurred while running the code."
msgstr ""

#: src/korone/modules/piston/handlers/run.py:65
#: src/korone/modules/piston/handlers/run.py:66
msgid ""
"<b>Code</b>:\n"
"<pre language='{lang}'>{code}</pre>\n"
"\n"
msgstr ""

#: src/korone/modules/piston/handlers/run.py:70
#: src/korone/modules/piston/handlers/run.py:71
msgid ""
"<b>Output</b>:\n"
"<pre language='bash'>{output}</pre>\n"
msgstr ""

#: src/korone/modules/piston/handlers/run.py:75
#: src/korone/modules/piston/handlers/run.py:76
msgid ""
"<b>Compiler Output</b>:\n"
"<pre language='bash'>{output}</pre>"
msgstr ""

#: src/korone/modules/piston/handlers/run.py:84
msgid ""
"The result exceeds the 4096 character limit of Telegram. Please refine "
"your code."
msgstr ""

#: src/korone/modules/pm_menu/handlers/about.py:28
msgid ""
"Korone is a comprehensive and cutting-edge Telegram bot that offers a "
Expand Down
1 change: 1 addition & 0 deletions news/+lfmcollage.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed a bug where the bot did not respond to the `/lfmcollage` command if one of the albums in the collage lacked a cover.
1 change: 1 addition & 0 deletions news/+piston.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed an error that occurred when the result of `/piston` evaluation exceeded the 4096-character limit for Telegram messages.
File renamed without changes.
Binary file added resources/lastfm/dummy_image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
12 changes: 4 additions & 8 deletions src/korone/modules/lastfm/utils/collage_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,9 @@ def blocking_add_text_to_image():


async def fetch_album_arts(albums: list[LastFMAlbum]) -> list[Image.Image]:
async def fetch_art(album: LastFMAlbum) -> Image.Image:
image_url = await get_biggest_lastfm_image(album)
return (
Image.open(image_url)
if image_url
else Image.open("https://telegra.ph/file/d0244cd9b8bc7d0dd370d.png")
)
async def fetch_art(album: LastFMAlbum) -> Image.Image | None:
image = await get_biggest_lastfm_image(album)
return Image.open(image) if image else Image.open("resources/lastfm/dummy_image.png")

tasks = [asyncio.create_task(fetch_art(album)) for album in albums]
return [img for img in await asyncio.gather(*tasks) if img is not None]
Expand All @@ -60,7 +56,7 @@ async def create_album_collage(
thumb_size = 300
collage = Image.new("RGB", (thumb_size * cols, thumb_size * rows))

font = ImageFont.truetype("resources/fonts/DejaVuSans-Bold.ttf", 24) if show_text else None
font = ImageFont.truetype("resources/lastfm/DejaVuSans-Bold.ttf", 24) if show_text else None

album_images = await fetch_album_arts(albums[: rows * cols])

Expand Down
2 changes: 1 addition & 1 deletion src/korone/modules/lastfm/utils/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from .types import LastFMAlbum, LastFMArtist, LastFMTrack

with Path(
constants.BOT_ROOT_PATH / "resources/misc/everynoise_genres.txt",
constants.BOT_ROOT_PATH / "resources/lastfm/everynoise_genres.txt",
).open(encoding="utf-8") as file:
ACCEPTABLE_TAGS = {line.strip().lower() for line in file}

Expand Down

0 comments on commit 3f2b5a7

Please sign in to comment.