Create lyrics.py
This commit is contained in:
parent
3a6f18f67b
commit
81d7ce08c3
48
plugins/lyrics.py
Normal file
48
plugins/lyrics.py
Normal file
|
@ -0,0 +1,48 @@
|
|||
from util import hook, http, web
|
||||
from urllib import urlencode
|
||||
from spotify import spotifyfunc as spotify
|
||||
|
||||
url = "http://search.azlyrics.com/search.php?q="
|
||||
|
||||
|
||||
@hook.command
|
||||
def lyrics(inp):
|
||||
"""lyrics <search> - Search AZLyrics.com for song lyrics"""
|
||||
if "pastelyrics" in inp:
|
||||
dopaste = True
|
||||
inp = inp.replace("pastelyrics", "").strip()
|
||||
else:
|
||||
dopaste = False
|
||||
soup = http.get_soup(url + inp.replace(" ", "+"))
|
||||
if "Try to compose less restrictive search query" in soup.find('div', {'id': 'inn'}).text:
|
||||
return "No results. Check spelling."
|
||||
div = None
|
||||
for i in soup.findAll('div', {'class': 'sen'}):
|
||||
if "/lyrics/" in i.find('a')['href']:
|
||||
div = i
|
||||
break
|
||||
if div:
|
||||
title = div.find('a').text
|
||||
link = div.find('a')['href']
|
||||
if dopaste:
|
||||
newsoup = http.get_soup(link)
|
||||
try:
|
||||
lyrics = newsoup.find('div', {'style': 'margin-left:10px;margin-right:10px;'}).text.strip()
|
||||
pasteurl = " " + web.haste(lyrics)
|
||||
except Exception as e:
|
||||
pasteurl = " (\x02Unable to paste lyrics\x02 [%s])" % str(e)
|
||||
else:
|
||||
pasteurl = ""
|
||||
artist = div.find('b').text.title()
|
||||
try:
|
||||
spurl = " (Spotify: http://" + spotify(title + " " + artist).split("http://")[1].split("\x02")[0] + ")"
|
||||
except:
|
||||
spurl = ""
|
||||
lyricsum = div.find('div').text
|
||||
if "\r\n" in lyricsum.strip():
|
||||
lyricsum = " / ".join(lyricsum.strip().split("\r\n")[0:4]) # truncate, format
|
||||
else:
|
||||
lyricsum = " / ".join(lyricsum.strip().split("\n")[0:4]) # truncate, format
|
||||
return "\x02%s\x02 by \x02%s\x02 %s%s%s - %s" % (title, artist, web.try_isgd(link), spurl, pasteurl, lyricsum[:-3])
|
||||
else:
|
||||
return "No song results. " + url + inp.replace(" ", "+")
|
Reference in a new issue