Lyrics: match break, debug url, add retries

This commit is contained in:
oddluck 2020-07-06 18:08:18 +00:00
parent d2118e773f
commit 454fac3ca9

View File

@ -54,7 +54,10 @@ class Lyrics(callbacks.Plugin):
threaded = True threaded = True
def dosearch(self, irc, channel, text): def dosearch(self, irc, channel, text):
google = ddg = title = None google = None
ddg = None
title = None
match = None
if self.registryValue("google", channel) > 0: if self.registryValue("google", channel) > 0:
google = irc.getCallback("google") google = irc.getCallback("google")
if not google: if not google:
@ -69,6 +72,8 @@ class Lyrics(callbacks.Plugin):
query = "site:lyrics.fandom.com/wiki/ %s" % text query = "site:lyrics.fandom.com/wiki/ %s" % text
pattern = re.compile(r"https?://lyrics.fandom.com/wiki/.*") pattern = re.compile(r"https?://lyrics.fandom.com/wiki/.*")
for i in range(1, 3): for i in range(1, 3):
if match:
break
if google and self.registryValue("google", channel) == i: if google and self.registryValue("google", channel) == i:
try: try:
results = google.decode(google.search(query, irc.network, channel)) results = google.decode(google.search(query, irc.network, channel))
@ -82,7 +87,10 @@ class Lyrics(callbacks.Plugin):
title = r["title"].replace(":", " - ").split("|")[0] title = r["title"].replace(":", " - ").split("|")[0]
except TypeError: except TypeError:
title = r.title.replace(":", " - ").split("|")[0] title = r.title.replace(":", " - ").split("|")[0]
log.debug("Lyrics: found link using Google search") log.debug(
"Lyrics: found link using Google search: %s"
% match.group(0)
)
break break
except: except:
pass pass
@ -98,7 +106,9 @@ class Lyrics(callbacks.Plugin):
match = re.search(pattern, r[2]) match = re.search(pattern, r[2])
if match: if match:
title = r[0].replace(":", " - ").split("|")[0] title = r[0].replace(":", " - ").split("|")[0]
log.debug("Lyrics: found link using DDG") log.debug(
"Lyrics: found link using DDG: %s" % match.group(0)
)
break break
except: except:
pass pass
@ -107,8 +117,9 @@ class Lyrics(callbacks.Plugin):
else: else:
return None, None return None, None
def getlyrics(self, query): def getlyrics(self, query, retries=0):
lyrics = None lyrics = None
if retries < 3:
if "lyrics.fandom.com/wiki/" in query: if "lyrics.fandom.com/wiki/" in query:
try: try:
log.debug("Lyrics: requesting {0}".format(query)) log.debug("Lyrics: requesting {0}".format(query))
@ -119,15 +130,19 @@ class Lyrics(callbacks.Plugin):
try: try:
log.debug("Lyrics: requesting {0}".format(query)) log.debug("Lyrics: requesting {0}".format(query))
query = query.split(",", 1) query = query.split(",", 1)
lyrics = pylyrics3.get_song_lyrics(query[0].strip(), query[1].strip()) lyrics = pylyrics3.get_song_lyrics(
query[0].strip(), query[1].strip()
)
except Exception: except Exception:
pass pass
if lyrics: if lyrics:
lyrics = re.sub(r"(?<!\.|\!|\?)\s+\n", ".", lyrics) lyrics = re.sub(r"(?<!\.|\!|\?)\s+\n", ".", lyrics)
lyrics = re.sub(r"\s+\n", "", lyrics) lyrics = re.sub(r"\s+\n", "", lyrics)
return lyrics return lyrics
else: else:
self.getLyrics(query, retries + 1)
else:
log.info("Lyrics: maximum number of retries (3) reached.")
return return
def lyric(self, irc, msg, args, lyric): def lyric(self, irc, msg, args, lyric):
@ -165,6 +180,7 @@ class Lyrics(callbacks.Plugin):
return return
lyric = wrap(lyric, ["text"]) lyric = wrap(lyric, ["text"])
lyrics = lyric
Class = Lyrics Class = Lyrics