2010-11-18 22:21:44 +00:00
|
|
|
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):
|
|
|
|
if inp:
|
|
|
|
user = inp
|
|
|
|
else:
|
|
|
|
user = nick
|
|
|
|
|
|
|
|
response = http.get_json(api_url, method="user.getrecenttracks",
|
|
|
|
api_key=api_key, user=user, limit=1)
|
|
|
|
|
|
|
|
if 'error' in response:
|
|
|
|
if inp: # specified a user name
|
|
|
|
return "error: %s" % response["message"]
|
|
|
|
else:
|
|
|
|
return "your nick is not a LastFM account. try '.lastfm username'."
|
|
|
|
|
2011-02-03 19:16:28 +00:00
|
|
|
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
|
|
|
|
track = tracks[0]
|
|
|
|
status = 'current track'
|
|
|
|
elif type(tracks) == dict:
|
|
|
|
# otherwise, they aren't listening to anything right now, and
|
|
|
|
# the tracks entry is a dict representing the most recent track
|
|
|
|
track = tracks
|
|
|
|
status = 'last track'
|
|
|
|
else:
|
|
|
|
return "error parsing track listing"
|
|
|
|
|
2010-11-18 22:21:44 +00:00
|
|
|
title = track["name"]
|
|
|
|
album = track["album"]["#text"]
|
|
|
|
artist = track["artist"]["#text"]
|
|
|
|
|
2011-02-03 19:16:28 +00:00
|
|
|
ret = "\x02%s\x0F's %s - \x02%s\x0f" % (user, status, title)
|
2010-11-18 22:21:44 +00:00
|
|
|
if artist:
|
|
|
|
ret += " by \x02%s\x0f" % artist
|
|
|
|
if album:
|
|
|
|
ret += " on \x02%s\x0f" % album
|
|
|
|
|
|
|
|
say(ret)
|