This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/plugins/lastfm.py

72 lines
2.4 KiB
Python
Raw Normal View History

2012-03-12 23:36:27 +01:00
# Upgraded with tables/caching by ChauffeR of #freebnc on irc.esper.net
from util import hook, http
2012-04-02 18:17:55 +02:00
2012-03-12 23:35:36 +01:00
@hook.command('l', autohelp=False)
@hook.command(autohelp=False)
def lastfm(inp, nick='', say=None, db=None, bot=None):
2012-04-02 18:17:55 +02:00
".lastfm [user] -- Displays the now playing (or last played) "\
"track of LastFM user [user]."
2012-02-28 01:17:18 +01:00
if inp:
user = inp
else:
user = nick
db.execute("create table if not exists lastfm(nick primary key, acc)")
sql = db.execute("select acc from lastfm where nick=lower(?)", (nick,)).fetchone();
api_url = "http://ws.audioscrobbler.com/2.0/?format=json"
api_key = bot.config.get("api_keys", {}).get("lastfm")
if api_key is None:
return "error: no api key set"
if sql:
if not inp: user = sql[0]
else:
user = inp
2012-02-29 09:29:53 +01:00
db.execute("insert or replace into lastfm(nick,acc) values(?,?)", (nick.lower(), user))
db.commit()
else:
if not inp: user = nick
else:
user = inp
2012-02-29 09:29:53 +01:00
db.execute("insert or replace into lastfm(nick,acc) values(?,?)", (nick.lower(), user))
db.commit()
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:
2012-03-13 19:55:58 +01:00
return "your nick is not a LastFM account. try '.lastfm [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
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:
2012-02-28 01:05:41 +01:00
return "error: could not parse track listing"
title = track["name"]
album = track["album"]["#text"]
artist = track["artist"]["#text"]
ret = "\x02%s\x0F's %s - \x02%s\x0f" % (user, status, title)
if artist:
ret += " by \x02%s\x0f" % artist
if album:
ret += " on \x02%s\x0f" % album
say(ret)