From f88018a76f7e1bb030aee5c0a97dbd63934cdc6e Mon Sep 17 00:00:00 2001 From: James Lu Date: Tue, 11 Apr 2017 18:11:15 -0700 Subject: [PATCH] LastFM: optionally show extended info (user play count + tags) --- LastFM/config.py | 5 +++++ LastFM/plugin.py | 17 +++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/LastFM/config.py b/LastFM/config.py index 3b262de..85f0708 100644 --- a/LastFM/config.py +++ b/LastFM/config.py @@ -49,5 +49,10 @@ conf.registerChannelValue(LastFM, "fetchYouTubeLink", fetch a YouTube link for the track given in 'np'. This is an experimental feature, and requires the DDG plugin in this repository 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: diff --git a/LastFM/plugin.py b/LastFM/plugin.py index 398af4f..16c1cbc 100644 --- a/LastFM/plugin.py +++ b/LastFM/plugin.py @@ -185,8 +185,21 @@ class LastFM(callbacks.Plugin): # entire np request to fail. 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), - ircutils.bold(artist), album, time, public_url) + ext_info = '' + 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)) @wrap(["something"])