Upgraded lastfm to use SQL

This commit is contained in:
neersighted 2012-02-28 07:58:34 +08:00 committed by lukeroge
parent dad1c0873e
commit 1905f68037

View file

@ -1,55 +1,64 @@
from util import hook, http from util import hook, http
@hook.command
api_url = "http://ws.audioscrobbler.com/2.0/?format=json" def lastfm(inp, nick='', say=None, db=None, bot=None):
db.execute("create table if not exists lastfm(nick primary key, acc)")
sql = db.execute("select acc from lastfm where nick=lower(?)", (nick,)).fetchone();
@hook.command api_url = "http://ws.audioscrobbler.com/2.0/?format=json"
def lastfm(inp, nick='', say=None, bot=None): api_key = bot.config.get("api_keys", {}).get("lastfm")
if inp: if api_key is None:
user = inp return "error: no api key set"
else:
user = nick
if sql:
api_key = bot.config.get("api_keys", {}).get("lastfm", None) if not inp: user = sql[0]
if api_key is None: else:
return "error: no api key set" user = inp
db.execute("insert or replace into lastfm(nick,acc) values(?,?)",(nick.lower(), user))
response = http.get_json(api_url, method="user.getrecenttracks", db.commit()
api_key=api_key, user=user, limit=1) else:
if not inp: user = nick
if 'error' in response: else:
if inp: # specified a user name user = inp
return "error: %s" % response["message"] db.execute("insert or replace into lastfm(nick,acc) values(?,?)",(nick.lower(), user))
else: db.commit()
return "Your nick is not a LastFM account. Try '.lastfm username'."
response = http.get_json(api_url, method="user.getrecenttracks",
tracks = response["recenttracks"]["track"] api_key=api_key, user=user, limit=1)
if len(tracks) == 0: if 'error' in response:
return "No recent tracks for user %r found." % user if inp: # specified a user name
return "error: %s" % response["message"]
if type(tracks) == list: else:
# if the user is listening to something, the tracks entry is a list return "your nick is not a LastFM account. try '.lastfm username'."
# the first item is the current track
track = tracks[0] tracks = response["recenttracks"]["track"]
status = 'current track'
elif type(tracks) == dict: if len(tracks) == 0:
# otherwise, they aren't listening to anything right now, and return "no recent tracks for user %r found" % user
# the tracks entry is a dict representing the most recent track
track = tracks if type(tracks) == list:
status = 'last track' # if the user is listening to something, the tracks entry is a list
else: # the first item is the current track
return "Error parsing track listing" track = tracks[0]
status = 'current track'
title = track["name"] elif type(tracks) == dict:
album = track["album"]["#text"] # otherwise, they aren't listening to anything right now, and
artist = track["artist"]["#text"] # the tracks entry is a dict representing the most recent track
track = tracks
ret = "\x02%s\x0F's %s - \x02%s\x0f" % (user, status, title) status = 'last track'
if artist: else:
ret += " by \x02%s\x0f" % artist return "error parsing track listing"
if album:
ret += " on \x02%s\x0f" % album title = track["name"]
album = track["album"]["#text"]
say(ret) 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)