Fixed DOS format line endings in lastfm.py

This commit is contained in:
neersighted 2012-02-28 08:01:57 +08:00 committed by lukeroge
parent 1905f68037
commit bcdb686989

View file

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