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:
parent
25116e20f9
commit
b939a94be7
|
@ -1,13 +1,19 @@
|
||||||
|
'''
|
||||||
|
The Last.fm API key is retrieved from the bot config file.
|
||||||
|
'''
|
||||||
|
|
||||||
from util import hook, http
|
from util import hook, http
|
||||||
|
|
||||||
|
|
||||||
api_key = ""
|
|
||||||
|
|
||||||
api_url = "http://ws.audioscrobbler.com/2.0/?format=json"
|
api_url = "http://ws.audioscrobbler.com/2.0/?format=json"
|
||||||
|
|
||||||
|
|
||||||
@hook.command
|
@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:
|
if inp:
|
||||||
user = inp
|
user = inp
|
||||||
else:
|
else:
|
||||||
|
@ -20,13 +26,13 @@ def lastfm(inp, nick='', say=None):
|
||||||
if inp: # specified a user name
|
if inp: # specified a user name
|
||||||
return "error: %s" % response["message"]
|
return "error: %s" % response["message"]
|
||||||
else:
|
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"]
|
tracks = response["recenttracks"]["track"]
|
||||||
|
|
||||||
if len(tracks) == 0:
|
|
||||||
return "no recent tracks for user %r found" % user
|
|
||||||
|
|
||||||
if type(tracks) == list:
|
if type(tracks) == list:
|
||||||
# if the user is listening to something, the tracks entry is a list
|
# if the user is listening to something, the tracks entry is a list
|
||||||
# the first item is the current track
|
# the first item is the current track
|
||||||
|
|
Loading…
Reference in New Issue