diff --git a/plugins/lastfm.py b/plugins/lastfm.py index 89f590d..4e77f65 100644 --- a/plugins/lastfm.py +++ b/plugins/lastfm.py @@ -1,13 +1,19 @@ +''' +The Last.fm API key is retrieved from the bot config file. +''' + from util import hook, http -api_key = "" - api_url = "http://ws.audioscrobbler.com/2.0/?format=json" @hook.command -def lastfm(inp, nick='', say=None): +def lastfm(inp, nick='', say=None, bot=None): + api_key = bot.config.get("api_keys", {}).get("lastfm", None) + if not api_key: + return None + if inp: user = inp else: @@ -20,13 +26,13 @@ def lastfm(inp, nick='', say=None): if inp: # specified a user name return "error: %s" % response["message"] else: - return "your nick is not a LastFM account. try '.lastfm username'." + return "your nick is not a Last.fm account. try '.lastfm username'." + + if not "track" in response["recenttracks"] or len(response["recenttracks"]["track"]) == 0: + return "no recent tracks for user \x02%s\x0F found" % user tracks = response["recenttracks"]["track"] - if len(tracks) == 0: - return "no recent tracks for user %r found" % user - if type(tracks) == list: # if the user is listening to something, the tracks entry is a list # the first item is the current track