Skip to content

Commit

Permalink
Bugfixes for lastfm
Browse files Browse the repository at this point in the history
  • Loading branch information
joinemm committed Jan 17, 2024
1 parent 3e8d7bf commit 883f139
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
34 changes: 27 additions & 7 deletions cogs/lastfm.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@
from enum import Enum, auto
from typing import TYPE_CHECKING, Annotated, Any, Callable, Literal, Optional, Union

import aiohttp
import arrow
import discord
import kdtree
import orjson
from discord.ext import commands
from discord.ext import commands, tasks
from discord.utils import escape_markdown
from loguru import logger

from modules import emojis, exceptions, util
from modules.lastfm import LastFmApi, LastFmImage, Period
from modules.misobot import LastFmContext, MisoBot, MisoContext
from modules.ui import RowPaginator

from modules import emojis, exceptions, util


def is_small_server():
async def predicate(ctx: MisoContext):
Expand Down Expand Up @@ -232,10 +234,24 @@ def __init__(self, bot):
self.bot: MisoBot = bot
self.api = LastFmApi(bot)

@tasks.loop(minutes=1)
async def lastfm_login_task(self):
logger.info("Running lastfm login task...")
try:
await self.api.login(
self.bot.keychain.LASTFM_USERNAME, self.bot.keychain.LASTFM_PASSWORD
)
except aiohttp.ClientError:
pass
else:
self.lastfm_login_task.cancel()
logger.info("Lastfm login successfull, canceling task")

async def cog_load(self):
await self.api.login(
self.bot.keychain.LASTFM_USERNAME, self.bot.keychain.LASTFM_PASSWORD
)
self.lastfm_login_task.start()

async def cog_unload(self):
self.lastfm_login_task.cancel()

@commands.group(aliases=["lastfm", "lfm", "lf"])
async def fm(self, ctx: MisoContext):
Expand Down Expand Up @@ -923,7 +939,11 @@ async def album(self, ctx: MisoContext, *, album: Annotated[tuple, AlbumArgument

tags_list = None
if tags := albuminfo["tags"]:
tags_list = ", ".join(t["name"] for t in tags["tag"])
# sometimes it's a dict, maybe when there's only one tag(?)
if isinstance(tags["tag"], dict):
tags_list = ", ".join(tags["tag"]["name"])
else:
tags_list = ", ".join(t["name"] for t in tags["tag"])

tracklist = []
for track in tracks:
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ exclude = '''
)/
'''

[tool.isort]
profile = "black"

[tool.ruff]
target-version = "py311"

Expand Down

0 comments on commit 883f139

Please sign in to comment.