Improved the Last.fm plugin

The API key is now retrieved from the bot config file and the API response is more thoroughly analyzed.
This commit is contained in:
stoneLeaf 2012-04-12 14:02:36 +02:00
parent 25116e20f9
commit b939a94be7
1 changed files with 13 additions and 7 deletions

View File

@ -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