Skip to content

Commit

Permalink
refactor(last_fm): use Pydantic models
Browse files Browse the repository at this point in the history
  • Loading branch information
HitaloM committed Aug 5, 2024
1 parent dbb8814 commit e987750
Show file tree
Hide file tree
Showing 13 changed files with 187 additions and 201 deletions.
34 changes: 17 additions & 17 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: PyKorone 1.0\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2024-08-05 16:45-0300\n"
"POT-Creation-Date: 2024-08-05 19:15-0300\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
Expand Down Expand Up @@ -314,7 +314,7 @@ msgid ""
"username</code>."
msgstr ""

#: src/korone/modules/lastfm/handlers/album.py:46
#: src/korone/modules/lastfm/handlers/album.py:48
#: src/korone/modules/lastfm/handlers/artist.py:45
#: src/korone/modules/lastfm/handlers/collage.py:61
#: src/korone/modules/lastfm/handlers/compat.py:78
Expand All @@ -325,7 +325,7 @@ msgstr ""
msgid "Your LastFM username was not found! Try setting it again."
msgstr ""

#: src/korone/modules/lastfm/handlers/album.py:49
#: src/korone/modules/lastfm/handlers/album.py:51
#: src/korone/modules/lastfm/handlers/artist.py:48
#: src/korone/modules/lastfm/handlers/collage.py:65
#: src/korone/modules/lastfm/handlers/compat.py:81
Expand All @@ -338,27 +338,27 @@ msgid ""
"Error: <i>{error}</i>"
msgstr ""

#: src/korone/modules/lastfm/handlers/album.py:58
#: src/korone/modules/lastfm/handlers/album.py:60
#: src/korone/modules/lastfm/handlers/artist.py:57
#: src/korone/modules/lastfm/handlers/now.py:58
#: src/korone/modules/lastfm/handlers/recent.py:70
msgid "{user}'s is listening to:\n"
msgstr ""

#: src/korone/modules/lastfm/handlers/album.py:60
#: src/korone/modules/lastfm/handlers/album.py:62
#: src/korone/modules/lastfm/handlers/artist.py:59
#: src/korone/modules/lastfm/handlers/now.py:60
#: src/korone/modules/lastfm/handlers/recent.py:75
msgid "{user}'s was listening to:\n"
msgstr ""

#: src/korone/modules/lastfm/handlers/album.py:65
#: src/korone/modules/lastfm/handlers/album.py:67
#: src/korone/modules/lastfm/handlers/artist.py:63
#: src/korone/modules/lastfm/handlers/now.py:65
msgid ", ❤️ loved"
msgstr ""

#: src/korone/modules/lastfm/handlers/album.py:67
#: src/korone/modules/lastfm/handlers/album.py:69
msgid " ∙ <code>{album_playcount} plays</code>"
msgstr ""

Expand Down Expand Up @@ -461,43 +461,43 @@ msgid ""
"Registered: <i>{registered}</i>"
msgstr ""

#: src/korone/modules/lastfm/utils/formatters.py:22
#: src/korone/modules/lastfm/utils/formatters.py:26
msgid ", {days} day(s) ago"
msgstr ""

#: src/korone/modules/lastfm/utils/formatters.py:26
#: src/korone/modules/lastfm/utils/formatters.py:30
msgid ", {hours} hour(s) ago"
msgstr ""

#: src/korone/modules/lastfm/utils/formatters.py:30
#: src/korone/modules/lastfm/utils/formatters.py:34
msgid ", {minutes} minute(s) ago"
msgstr ""

#: src/korone/modules/lastfm/utils/formatters.py:30
#: src/korone/modules/lastfm/utils/formatters.py:34
msgid ", Just now"
msgstr ""

#: src/korone/modules/lastfm/utils/formatters.py:59
#: src/korone/modules/lastfm/utils/formatters.py:63
msgid "1 week"
msgstr ""

#: src/korone/modules/lastfm/utils/formatters.py:61
#: src/korone/modules/lastfm/utils/formatters.py:65
msgid "1 month"
msgstr ""

#: src/korone/modules/lastfm/utils/formatters.py:63
#: src/korone/modules/lastfm/utils/formatters.py:67
msgid "3 months"
msgstr ""

#: src/korone/modules/lastfm/utils/formatters.py:65
#: src/korone/modules/lastfm/utils/formatters.py:69
msgid "6 months"
msgstr ""

#: src/korone/modules/lastfm/utils/formatters.py:67
#: src/korone/modules/lastfm/utils/formatters.py:71
msgid "1 year"
msgstr ""

#: src/korone/modules/lastfm/utils/formatters.py:68
#: src/korone/modules/lastfm/utils/formatters.py:72
msgid "All time"
msgstr ""

Expand Down
6 changes: 4 additions & 2 deletions src/korone/modules/lastfm/handlers/album.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ async def handle(client: Client, message: Message) -> None:
try:
last_played = (await last_fm.get_recent_tracks(last_fm_user, limit=1))[0]
album_info = await last_fm.get_album_info(
last_played.artist, last_played.album, last_fm_user
last_played.artist.name,
last_played.album.name, # type: ignore
last_fm_user,
)
except LastFMError as e:
error_message = str(e)
Expand All @@ -60,7 +62,7 @@ async def handle(client: Client, message: Message) -> None:
text = _("{user}'s was listening to:\n").format(user=user_link)

text += "💽 <i>{album_artist}</i> — <b>{album_name}</b>{loved}{time}{plays}".format(
album_artist=album_info.artist,
album_artist=album_info.artist.name, # type: ignore
album_name=album_info.name,
loved=_(", ❤️ loved") if album_info.loved else "",
time="" if last_played.now_playing else get_time_elapsed_str(last_played),
Expand Down
4 changes: 2 additions & 2 deletions src/korone/modules/lastfm/handlers/artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async def handle(client: Client, message: Message) -> None:

try:
last_played = (await last_fm.get_recent_tracks(last_fm_user, limit=1))[0]
artist_info = await last_fm.get_artist_info(last_played.artist, last_fm_user)
artist_info = await last_fm.get_artist_info(last_played.artist.name, last_fm_user)
except LastFMError as e:
error_message = str(e)
if error_message == "User not found":
Expand All @@ -58,7 +58,7 @@ async def handle(client: Client, message: Message) -> None:
else:
text = _("{user}'s was listening to:\n").format(user=user_link)

text += "<b>{artist_name}</b>{loved}{time}{plays}".format(
text += "👨‍🎤 <b>{artist_name}</b>{loved}{time}{plays}".format(
artist_name=artist_info.name,
loved=_(", ❤️ loved") if artist_info.loved else "",
time="" if last_played.now_playing else get_time_elapsed_str(last_played),
Expand Down
4 changes: 2 additions & 2 deletions src/korone/modules/lastfm/handlers/now.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async def handle(client: Client, message: Message) -> None:
try:
last_played = (await last_fm.get_recent_tracks(last_fm_user, limit=1))[0]
track_info = await last_fm.get_track_info(
last_played.artist, last_played.name, last_fm_user
last_played.artist.name, last_played.name, last_fm_user
)
except LastFMError as e:
error_message = str(e)
Expand All @@ -60,7 +60,7 @@ async def handle(client: Client, message: Message) -> None:
text = _("{user}'s was listening to:\n").format(user=user_link)

text += "🎧 <i>{track_artist}</i> — <b>{track_name}</b>{loved}{time}{plays}".format(
track_artist=track_info.artist,
track_artist=track_info.artist.name,
track_name=track_info.name,
loved=_(", ❤️ loved") if track_info.loved else "",
time="" if last_played.now_playing else get_time_elapsed_str(last_played),
Expand Down
2 changes: 1 addition & 1 deletion src/korone/modules/lastfm/handlers/recent.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@ def format_recent_plays(
def format_track(track: LastFMTrack, now_playing: bool = False) -> str:
time_elapsed_str = "" if now_playing else get_time_elapsed_str(track)

return f"🎧 <i>{track.artist}</i> — <b>{track.name}</b>{time_elapsed_str}"
return f"🎧 <i>{track.artist.name}</i> — <b>{track.name}</b>{time_elapsed_str}"
2 changes: 1 addition & 1 deletion src/korone/modules/lastfm/handlers/top.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ async def handle(self, client: Client, message: Message) -> None:
)
for item in top_items:
if isinstance(item, LastFMTrack):
text += f"{item.artist}{item.name}"
text += f"{item.artist.name}{item.name}"
else:
text += item.name

Expand Down
9 changes: 2 additions & 7 deletions src/korone/modules/lastfm/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2024 Hitalo M. <https://github.com/HitaloM>

from korone.modules.lastfm.utils.api import (
LastFMClient,
LastFMError,
LastFMImage,
LastFMTrack,
TimePeriod,
)
from korone.modules.lastfm.utils.api import LastFMClient, LastFMError, TimePeriod
from korone.modules.lastfm.utils.collage_generator import create_album_collage
from korone.modules.lastfm.utils.deezer_api import DeezerClient, DeezerError
from korone.modules.lastfm.utils.formatters import (
Expand All @@ -18,6 +12,7 @@
)
from korone.modules.lastfm.utils.image_filter import get_biggest_lastfm_image
from korone.modules.lastfm.utils.parse_collage import EntryType, parse_collage_arg
from korone.modules.lastfm.utils.types import LastFMImage, LastFMTrack

__all__ = (
"DeezerClient",
Expand Down
Loading

0 comments on commit e987750

Please sign in to comment.