[lastfm] make .lastfm behave the same as .weather

- syntax is now .lastfm <username> [dontsave] | @<nick>
This commit is contained in:
Peter Rowlands 2014-08-27 14:51:39 -05:00
parent f4accb15e5
commit 22f2e28e97
1 changed files with 36 additions and 10 deletions

View File

@ -10,22 +10,42 @@ api_url = "http://ws.audioscrobbler.com/2.0/?format=json"
@hook.api_key('lastfm')
@hook.command(autohelp=False)
def lastfm(inp, nick='', say=None, api_key=None):
if inp:
user = inp
def lastfm(inp, chan='', nick='', reply=None, api_key=None, db=None):
".lastfm <username> [dontsave] | @<nick> -- gets current or last played " \
"track from lastfm"
db.execute(
"create table if not exists "
"lastfm(chan, nick, user, primary key(chan, nick))"
)
if inp[0:1] == '@':
nick = inp[1:].strip()
user = None
dontsave = True
else:
user = nick
user = inp
dontsave = user.endswith(" dontsave")
if dontsave:
user = user[:-9].strip().lower()
if not user:
user = db.execute(
"select user from lastfm where chan=? and nick=lower(?)",
(chan, nick)).fetchone()
if not user:
return lastfm.__doc__
user = user[0]
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 Last.fm account. try '.lastfm username'."
return "error: %s" % response["message"]
if not "track" in response["recenttracks"] or len(response["recenttracks"]["track"]) == 0:
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"]
@ -53,4 +73,10 @@ def lastfm(inp, nick='', say=None, api_key=None):
if album:
ret += " on \x02%s\x0f" % album
say(ret)
reply(ret)
if inp and not dontsave:
db.execute(
"insert or replace into lastfm(chan, nick, user) "
"values (?, ?, ?)", (chan, nick.lower(), inp))
db.commit()