Merge pull request #117 from pmrowla/lastfm-save

[lastfm] make .lastfm behave the same as .weather
This commit is contained in:
Ryan Hitchman 2014-11-07 22:26:57 -08:00
commit fa904d2ded
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.api_key('lastfm')
@hook.command(autohelp=False) @hook.command(autohelp=False)
def lastfm(inp, nick='', say=None, api_key=None): def lastfm(inp, chan='', nick='', reply=None, api_key=None, db=None):
if inp: ".lastfm <username> [dontsave] | @<nick> -- gets current or last played " \
user = inp "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: 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", response = http.get_json(api_url, method="user.getrecenttracks",
api_key=api_key, user=user, limit=1) api_key=api_key, user=user, limit=1)
if 'error' in response: if 'error' in response:
if inp: # specified a user name return "error: %s" % response["message"]
return "error: %s" % response["message"]
else:
return "your nick is not a Last.fm account. try '.lastfm username'."
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 return "no recent tracks for user \x02%s\x0F found" % user
tracks = response["recenttracks"]["track"] tracks = response["recenttracks"]["track"]
@ -53,4 +73,10 @@ def lastfm(inp, nick='', say=None, api_key=None):
if album: if album:
ret += " on \x02%s\x0f" % 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()