LastFM: optionally show extended info (user play count + tags)

This commit is contained in:
James Lu 2017-04-11 18:11:15 -07:00
parent 4cd8bc9354
commit f88018a76f
2 changed files with 20 additions and 2 deletions

View File

@ -49,5 +49,10 @@ conf.registerChannelValue(LastFM, "fetchYouTubeLink",
fetch a YouTube link for the track given in 'np'. This is an fetch a YouTube link for the track given in 'np'. This is an
experimental feature, and requires the DDG plugin in this repository experimental feature, and requires the DDG plugin in this repository
to be loaded.""")) to be loaded."""))
conf.registerChannelValue(LastFM, "showExtendedInfo",
registry.Boolean(False, """Determines whether the bot will show extended info
for tracks in 'np' (currently the track tags and user play count). Note:
this requires a second API call to be made for each 'np' call, which may
be expensive in high traffic situations."""))
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: # vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:

View File

@ -185,8 +185,21 @@ class LastFM(callbacks.Plugin):
# entire np request to fail. # entire np request to fail.
log.exception("LastFM: failed to get YouTube link for track %s - %s", artist, track) log.exception("LastFM: failed to get YouTube link for track %s - %s", artist, track)
s = '%s listened to %s by %s %s %s. %s' % (ircutils.bold(user), ircutils.bold(track), ext_info = ''
ircutils.bold(artist), album, time, public_url) if self.registryValue("showExtendedInfo", msg.args[0]):
# Get extended info via a separate API call.
ext_info_url = "%sapi_key=%s&method=track.getinfo&user=%s&format=json&mbid=%s" % (self.APIURL, apiKey, user, trackdata['mbid'])
ext_info_f = utils.web.getUrl(ext_info_url).decode("utf-8")
self.log.debug("LastFM.nowPlaying: using url %s for extended info", ext_info_url)
ext_data = json.loads(ext_info_f)['track']
# We currently show play count and tags - more could be added in the future...
userplaycount = ext_data['userplaycount']
tags = [tag['name'] for tag in ext_data['toptags']['tag']]
ext_info = ' (Playcount: %s / Tags: %s)' % (userplaycount, ', '.join(tags))
s = '%s listened to %s by %s %s %s%s. %s' % (ircutils.bold(user), ircutils.bold(track),
ircutils.bold(artist), album, time, ext_info, public_url)
irc.reply(utils.str.normalizeWhitespace(s)) irc.reply(utils.str.normalizeWhitespace(s))
@wrap(["something"]) @wrap(["something"])