From 99d6557e462a71e7c20c4bcd447e0758323f6301 Mon Sep 17 00:00:00 2001 From: James Lu Date: Sun, 1 Nov 2015 10:44:50 -0800 Subject: [PATCH] LastFM: add experimental YouTube link fetching for 'np' Essentially, my reimplementation of krf/supybot-lastfm#8. Instead of using YouTube's API (which requires API keys and all that), this performs web search on site:youtube.com using the DDG (DuckDuckGo) plugin in this repository. The advantage to this is that it's simpler to set up, but results may not always be the most accurate / relevant. This behavior can be turned on by setting supybot.plugins.LastFM.fetchYouTubeLinks to True - it defaults to off since this is experimental anyways. The DDG plugin needs to be loaded too for this to work. Also, only bold the album/artist/track data during output formatting, NOT before (this screws up searches with all the \x02's). --- LastFM/config.py | 5 +++++ LastFM/plugin.py | 27 ++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/LastFM/config.py b/LastFM/config.py index 0ec93e6..c38ff5b 100644 --- a/LastFM/config.py +++ b/LastFM/config.py @@ -46,5 +46,10 @@ conf.registerGlobalValue(LastFM, 'apiKey', conf.registerChannelValue(LastFM, "maxResults", registry.NonNegativeInteger(5, """Limits the number of results that will be displayed in the channel.""")) +conf.registerChannelValue(LastFM, "fetchYouTubeLink", + registry.Boolean(False, """Determines whether the bot will try to + 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.""")) # vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: diff --git a/LastFM/plugin.py b/LastFM/plugin.py index 1913944..0f052ab 100644 --- a/LastFM/plugin.py +++ b/LastFM/plugin.py @@ -165,12 +165,12 @@ class LastFM(callbacks.Plugin): except IndexError: irc.error("%s doesn't seem to have listened to anything." % user, Raise=True) - artist = ircutils.bold(trackdata["artist"]["#text"].strip()) # Artist name - track = ircutils.bold(trackdata["name"].strip()) # Track name + artist = trackdata["artist"]["#text"].strip() # Artist name + track = trackdata["name"].strip() # Track name # Album name (may or may not be present) album = trackdata["album"]["#text"].strip() if album: - album = ircutils.bold("[%s] " % album) + album = "[%s] " % album try: time = int(trackdata["date"]["uts"]) # Time of last listen @@ -180,6 +180,27 @@ class LastFM(callbacks.Plugin): except KeyError: # Nothing given by the API? time = "some point in time" + public_url = '' + # If the DDG plugin from this repository is loaded, we can integrate + # that by finding a YouTube link for the track. + if self.registryValue("fetchYouTubeLink"): + ddg = irc.getCallback("DDG") + if ddg: + # Each valid result has a preceding heading in the format + # '1. ', etc. + try: + search = [td for td in ddg._ddgurl('site:youtube.com "%s - %s"' % (artist, track)) + if "1." in td.text] + res = search[0].next_sibling.next_sibling + public_url = format(' - %u', res.a.get('href')) + except: + # If something breaks, log the error but don't cause the + # entire np request to fail. + log.exception("LastFM: failed to get YouTube link for track %s - %s", artist, track) + + irc.reply('%s listened to %s by %s %sat %s%s' % + (ircutils.bold(user), ircutils.bold(track), + ircutils.bold(artist), ircutils.bold(album), time, public_url)) np = wrap(nowPlaying, [optional("something")])