Lyrics plugin

We can fetch and store lyrics for each song.
This commit is contained in:
Bryan Schumaker 2010-11-27 15:39:55 -05:00
parent 8af11586ac
commit 2728e343d3
1 changed files with 92 additions and 0 deletions

92
plugins/lyrics.py Normal file
View File

@ -0,0 +1,92 @@
# Bryan Schumaker (11/27/2010)
import ocarina
from ocarina import footer
import re
libsaria = ocarina.libsaria
from libsaria import web
from libsaria import xm
from libsaria import cache
gtk = ocarina.gtk
page = gtk.ScrolledWindow()
text = gtk.TextView()
buffer = text.get_buffer()
page.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
text.set_editable(False)
page.add(text)
page.show_all()
url = "http://lyrics.wikia.com/api.php?"
def decode_line(line):
string = ""
for c in line.split(";"):
if len(c) == 0:
continue
try:
val = int(c[2:])
string += chr(val)
except:
pass
return string
def decode(file, code):
lines = []
for line in code:
lines.append(decode_line(line))
string = "\n".join(lines)
file.write(string)
def parse_full(file, lyrics):
req = web.Url(lyrics)
res = None
for line in req.open():
if re.match("<div class=\'lyricbox\'>(.*?)", line):
res = line
split = res.split("</div>")
code = split[len(split) - 1]
decode(file, code.split("<br />"))
def parse(file, res):
doc = xm.parse(res)
result = xm.child(doc)
url_node = xm.get_elements(result, "url")[0]
lyrics = xm.child(url_node).data
parse_full(file, lyrics)
def fetch_lyrics(file, artist, title):
req = web.Url(url)
req["artist"] = artist
req["song"] = title
req["fmt"] = "xml"
try:
parse(file, req.open())
except:
return False
return True
def set_lyrics(filepath):
id = libsaria.sources.file_to_id(filepath)
artist, title = libsaria.sources.library.get_attrs(id, "artist", "title")
cached = cache[artist]
file = cached.get("%s.txt" % title, fetch_lyrics, artist, title)
fin = open(file)
buffer.set_text(fin.read())
def start():
footer.add_page("Lyrics", page)
libsaria.event.invite("POSTLOAD", set_lyrics, True)
def stop():
footer.remove_page("Lyrics")
def check_version():
if ocarina.__major__ != 4:
return False
if ocarina.__minor__ >= 2:
return True
return False