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