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).
This commit is contained in:
James Lu 2015-11-01 10:44:50 -08:00
parent 5431f25e0a
commit 99d6557e46
2 changed files with 29 additions and 3 deletions

View File

@ -46,5 +46,10 @@ conf.registerGlobalValue(LastFM, 'apiKey',
conf.registerChannelValue(LastFM, "maxResults", conf.registerChannelValue(LastFM, "maxResults",
registry.NonNegativeInteger(5, """Limits the number of results that will be registry.NonNegativeInteger(5, """Limits the number of results that will be
displayed in the channel.""")) 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: # vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:

View File

@ -165,12 +165,12 @@ class LastFM(callbacks.Plugin):
except IndexError: except IndexError:
irc.error("%s doesn't seem to have listened to anything." % user, Raise=True) irc.error("%s doesn't seem to have listened to anything." % user, Raise=True)
artist = ircutils.bold(trackdata["artist"]["#text"].strip()) # Artist name artist = trackdata["artist"]["#text"].strip() # Artist name
track = ircutils.bold(trackdata["name"].strip()) # Track name track = trackdata["name"].strip() # Track name
# Album name (may or may not be present) # Album name (may or may not be present)
album = trackdata["album"]["#text"].strip() album = trackdata["album"]["#text"].strip()
if album: if album:
album = ircutils.bold("[%s] " % album) album = "[%s] " % album
try: try:
time = int(trackdata["date"]["uts"]) # Time of last listen time = int(trackdata["date"]["uts"]) # Time of last listen
@ -180,6 +180,27 @@ class LastFM(callbacks.Plugin):
except KeyError: # Nothing given by the API? except KeyError: # Nothing given by the API?
time = "some point in time" 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
# '<td valign="top">1.&nbsp;</td>', 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")]) np = wrap(nowPlaying, [optional("something")])