From 7eb9ff1c7f00cfdebee039f5453a5ed567ffecb2 Mon Sep 17 00:00:00 2001 From: Luke Rogers Date: Thu, 19 Apr 2012 21:25:08 +1200 Subject: [PATCH] Standardised name saving between weather and lastfm --- plugins/lastfm.py | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/plugins/lastfm.py b/plugins/lastfm.py index cc4d854..48987a9 100755 --- a/plugins/lastfm.py +++ b/plugins/lastfm.py @@ -1,7 +1,5 @@ -# Upgraded with tables/caching by ChauffeR of #freebnc on irc.esper.net from util import hook, http - @hook.command('l', autohelp=False) @hook.command(autohelp=False) def lastfm(inp, nick='', say=None, db=None, bot=None): @@ -15,23 +13,22 @@ def lastfm(inp, nick='', say=None, db=None, bot=None): 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" + dontsave = user.endswith(" dontsave") + if dontsave: + user = user[:-9].strip().lower() - if sql: - if not inp: user = sql[0] - else: - user = inp - db.execute("insert or replace into lastfm(nick,acc) values(?,?)", (nick.lower(), user)) - db.commit() - else: - if not inp: user = nick - else: - user = inp - db.execute("insert or replace into lastfm(nick,acc) values(?,?)", (nick.lower(), user)) - db.commit() + 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] response = http.get_json(api_url, method="user.getrecenttracks", api_key=api_key, user=user, limit=1) @@ -69,5 +66,10 @@ def lastfm(inp, nick='', say=None, db=None, bot=None): out += ' by "\x02%s\x0f"' % artist if album: out += ' from the album "\x02%s\x0f"' % album + + if inp and not dontsave: + db.execute("insert or replace into lastfm(nick, acc) values (?,?)", + (nick.lower(), user)) + db.commit() return out