2012-02-28 01:01:57 +01:00
|
|
|
from util import hook, http
|
|
|
|
|
2012-03-12 23:35:36 +01:00
|
|
|
@hook.command('l', autohelp=False)
|
|
|
|
@hook.command(autohelp=False)
|
2012-02-28 01:01:57 +01:00
|
|
|
def lastfm(inp, nick='', say=None, db=None, bot=None):
|
2012-04-19 11:30:07 +02:00
|
|
|
".lastfm [user] [dontsave] -- Displays the now playing (or last played)" \
|
|
|
|
" track of LastFM user [user]."
|
2012-04-19 10:32:31 +02:00
|
|
|
api_key = bot.config.get("api_keys", {}).get("lastfm")
|
2012-04-19 03:29:53 +02:00
|
|
|
if not api_key:
|
|
|
|
return "error: no api key set"
|
2012-04-19 10:32:31 +02:00
|
|
|
|
2012-04-19 11:27:52 +02:00
|
|
|
user = inp
|
2012-04-19 11:25:08 +02:00
|
|
|
|
2012-02-28 01:01:57 +01:00
|
|
|
api_url = "http://ws.audioscrobbler.com/2.0/?format=json"
|
2012-04-19 03:29:53 +02:00
|
|
|
|
2012-04-19 11:25:08 +02:00
|
|
|
dontsave = user.endswith(" dontsave")
|
|
|
|
if dontsave:
|
|
|
|
user = user[:-9].strip().lower()
|
2012-02-28 01:01:57 +01:00
|
|
|
|
2012-04-19 11:25:08 +02:00
|
|
|
db.execute("create table if not exists lastfm(nick primary key, acc)")
|
|
|
|
|
|
|
|
if not user:
|
|
|
|
user = db.execute("select acc from lastfm where nick=lower(?)",
|
|
|
|
(nick,)).fetchone()
|
|
|
|
if not user:
|
|
|
|
notice(lastfm.__doc__)
|
|
|
|
return
|
|
|
|
user = user[0]
|
2012-02-28 01:01:57 +01:00
|
|
|
|
|
|
|
response = http.get_json(api_url, method="user.getrecenttracks",
|
|
|
|
api_key=api_key, user=user, limit=1)
|
|
|
|
|
|
|
|
if 'error' in response:
|
2012-04-19 11:27:52 +02:00
|
|
|
return "Error: %s." % response["message"]
|
2012-02-28 01:01:57 +01:00
|
|
|
|
2012-04-19 03:31:47 +02:00
|
|
|
if not "track" in response["recenttracks"] or len(response["recenttracks"]["track"]) == 0:
|
2012-04-19 10:50:08 +02:00
|
|
|
return 'No recent tracks for user "%s" found.' % user
|
2012-04-19 03:29:53 +02:00
|
|
|
|
2012-04-19 03:31:47 +02:00
|
|
|
tracks = response["recenttracks"]["track"]
|
2012-02-28 01:01:57 +01:00
|
|
|
|
|
|
|
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]
|
2012-04-19 10:45:37 +02:00
|
|
|
status = 'is listening to'
|
2012-02-28 01:01:57 +01:00
|
|
|
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
|
2012-04-19 10:45:37 +02:00
|
|
|
status = 'last listened to'
|
2012-02-28 01:01:57 +01:00
|
|
|
else:
|
2012-02-28 01:05:41 +01:00
|
|
|
return "error: could not parse track listing"
|
2012-02-28 01:01:57 +01:00
|
|
|
|
|
|
|
title = track["name"]
|
|
|
|
album = track["album"]["#text"]
|
|
|
|
artist = track["artist"]["#text"]
|
|
|
|
|
2012-04-19 10:45:37 +02:00
|
|
|
out = "%s %s \x02%s\x0f" % (user, status, title)
|
2012-02-28 01:01:57 +01:00
|
|
|
if artist:
|
2012-04-19 03:29:53 +02:00
|
|
|
out += ' by "\x02%s\x0f"' % artist
|
2012-02-28 01:01:57 +01:00
|
|
|
if album:
|
2012-04-19 03:29:53 +02:00
|
|
|
out += ' from the album "\x02%s\x0f"' % album
|
2012-04-19 11:25:08 +02:00
|
|
|
|
|
|
|
if inp and not dontsave:
|
|
|
|
db.execute("insert or replace into lastfm(nick, acc) values (?,?)",
|
|
|
|
(nick.lower(), user))
|
|
|
|
db.commit()
|
2012-02-28 01:01:57 +01:00
|
|
|
|
2012-04-19 03:29:53 +02:00
|
|
|
return out
|