LastFM: migrate 'profile' to LastFM API v2

This commit is contained in:
GLolol 2014-11-10 18:15:20 -08:00
parent 8c00e3240a
commit 2f70d9bd71
2 changed files with 19 additions and 11 deletions

View File

@ -189,22 +189,30 @@ class LastFM(callbacks.Plugin):
Set your LastFM ID with the set method (default is your current nick) Set your LastFM ID with the set method (default is your current nick)
or specify <id> to switch for one call. or specify <id> to switch for one call.
""" """
if not self.apiKey:
irc.error("The API Key is not set for this plugin. Please set it via"
"config plugins.lastfm.apikey and reload the plugin. "
"You can sign up for an API Key using "
"http://www.last.fm/api/account/create", Raise=True)
id = (optionalId or self.db.getId(msg.nick) or msg.nick) id = (optionalId or self.db.getId(msg.nick) or msg.nick)
url = "%s/%s/profile.xml" % (self.APIURL_1_0, id) url = "%sapi_key=%s&method=user.getInfo&user=%s" % (self.APIURL_2_0, self.apiKey, id)
try: try:
f = utils.web.getUrlFd(url) f = utils.web.getUrlFd(url)
except utils.web.Error: except utils.web.Error:
irc.error("Unknown user (%s)" % id) irc.error("Unknown user (%s)" % id, Raise=True)
return
xml = minidom.parse(f).getElementsByTagName("profile")[0] xml = minidom.parse(f).getElementsByTagName("user")[0]
keys = "realname registered age gender country playcount".split() keys = ("realname", "registered", "age", "gender", "country", "playcount")
profile = tuple([self._parse(xml, node) for node in keys]) profile = {"id": id}
for tag in keys:
irc.reply("%s (realname: %s) registered on %s; age: %s / %s; " try:
"Country: %s; Tracks played: %s" % ((id,) + profile)) profile[tag] = xml.getElementsByTagName(tag)[0].firstChild.data.strip()
except AttributeError: # empty field
profile[tag] = 'unknown'
# node.getElementsByTagName(tagName)[0].firstChild.data
irc.reply(("%(id)s (realname: %(realname)s) registered on %(registered)s; age: %(age)s / %(gender)s; "
"Country: %(country)s; Tracks played: %(playcount)s") % profile)
profile = wrap(profile, [optional("something")]) profile = wrap(profile, [optional("something")])

View File

@ -29,7 +29,7 @@
### ###
from supybot.test import * from supybot.test import *
from plugin import LastFMParser from .plugin import LastFMParser
try: try:
from StringIO import StringIO from StringIO import StringIO