From b939a94be7b02da9abf7a978f2b4587abdca8b8c Mon Sep 17 00:00:00 2001 From: stoneLeaf Date: Thu, 12 Apr 2012 14:02:36 +0200 Subject: [PATCH] Improved the Last.fm plugin The API key is now retrieved from the bot config file and the API response is more thoroughly analyzed. --- plugins/lastfm.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) 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