use gsearch and pylyrics3
This commit is contained in:
parent
64d77d1907
commit
72e3fc1010
|
@ -11,8 +11,8 @@ import supybot.plugins as plugins
|
|||
import supybot.ircutils as ircutils
|
||||
import supybot.callbacks as callbacks
|
||||
import supybot.ircmsgs as ircmsgs
|
||||
import requests
|
||||
import html
|
||||
from gsearch.googlesearch import search
|
||||
import pylyrics3
|
||||
|
||||
try:
|
||||
from supybot.i18n import PluginInternationalization
|
||||
|
@ -26,25 +26,37 @@ class Lyrics(callbacks.Plugin):
|
|||
"""Retrieves song lyrics"""
|
||||
threaded = True
|
||||
|
||||
def lyric(self, irc, msg, args, lyric):
|
||||
"""<artist | song_title>
|
||||
Get song lyrics. search must be formatted as artist | song title.
|
||||
"""
|
||||
channel = msg.args[0]
|
||||
lyrics = None
|
||||
if '|' in lyric:
|
||||
query = lyric.split('|')
|
||||
else:
|
||||
irc.reply("Searches must be formatted as artist, song title")
|
||||
query = None
|
||||
if query:
|
||||
data = requests.get("https://lyric-api.herokuapp.com/api/find/{0}/{1}".format(query[0].strip(), query[1].strip())).json()
|
||||
lyrics = html.unescape(data['lyric']).replace('\n\n', '. ').replace('?\n', '? ').replace('!\n', '! ').replace('.\n', '. ').replace(',\n', ', ').replace('...\n', '... ').replace('\n', ', ')
|
||||
if lyrics:
|
||||
irc.reply(lyrics)
|
||||
else:
|
||||
irc.reply("Nothing found.")
|
||||
def dosearch(self, lyric):
|
||||
data = search("{0} site:lyrics.wikia.com/wiki/".format(lyric))
|
||||
try:
|
||||
title, url = data[0]
|
||||
return title, url
|
||||
except:
|
||||
pass
|
||||
|
||||
def getlyrics(self, url):
|
||||
lyrics = pylyrics3.get_lyrics_from_url(url)
|
||||
return lyrics
|
||||
|
||||
def lyric(self, irc, msg, args, lyric):
|
||||
"""<text>
|
||||
Get song lyrics from Lyrics Wiki. Search powered by Google.
|
||||
"""
|
||||
retries = 0
|
||||
url = None
|
||||
while not url and retries <= 3:
|
||||
try:
|
||||
title, url = self.dosearch(lyric)
|
||||
except:
|
||||
retries += 1
|
||||
irc.reply("Retrying...")
|
||||
if url:
|
||||
lyrics = self.getlyrics(url)
|
||||
irc.reply(title.replace(":", " - "))
|
||||
irc.reply(lyrics.replace(" \n", ".").replace("\\", ""))
|
||||
else:
|
||||
irc.reply("No lyrics found... or some other error.")
|
||||
lyric = wrap(lyric, ['text'])
|
||||
|
||||
Class = Lyrics
|
||||
|
||||
|
|
Loading…
Reference in New Issue