error handling

This commit is contained in:
Gordon Shumway 2019-03-17 03:02:26 -04:00 committed by GitHub
parent 8639354ff9
commit b01dbb47fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 31 additions and 19 deletions

View File

@ -30,6 +30,7 @@ class Lyrics(callbacks.Plugin):
threaded = True
def dosearch(self, lyric):
try:
searchurl = "https://www.google.com/search?&q={0} site:lyrics.wikia.com/wiki \"Lyrics\"".format(lyric)
ua = UserAgent()
header = {'User-Agent':str(ua.random)}
@ -38,25 +39,36 @@ class Lyrics(callbacks.Plugin):
url = soup.find('cite').getText()
title = soup.find("h3").getText()
url = "http://{0}".format(url)
except Exception:
return
else:
return title, url
def getlyrics(self, url):
try:
lyrics = pylyrics3.get_lyrics_from_url(url)
lyrics = re.sub('(?<!\.|\!|\?)\s\\n', '.', lyrics).replace(" \n", "")
except Exception:
lyrics = pylyrics3.get_lyrics_from_url(url)
lyrics = re.sub('(?<!\.|\!|\?)\s\\n', '.', lyrics).replace(" \n", "")
else:
return lyrics
def lyric(self, irc, msg, args, lyric):
"""<text>
Get song lyrics from Lyrics Wiki. Search powered by Google.
"""
try:
title, url = self.dosearch(lyric)
if url:
except Exception:
irc.reply("No results found for {0}".format(lyric))
else:
try:
lyrics = self.getlyrics(url)
irc.reply(title.replace(":", " - "))
irc.reply(lyrics)
else:
irc.reply("No lyrics found... or some other error.")
except Exception:
irc.reply("Unable to retrieve lyrics from {0}".format(url))
lyric = wrap(lyric, ['text'])
Class = Lyrics