1
# -*- Mode: python; coding: utf-8; tab-width: 8; indent-tabs-mode: t; -*-
3
# Copyright (C) 2005 Eduardo Gonzalez
4
# Copyright (C) 2006 Jonathan Matthew
5
# Copyright (C) 2007 James Livingston
7
# This program is free software; you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 2, or (at your option)
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
17
# You should have received a copy of the GNU General Public License
18
# along with this program; if not, write to the Free Software
19
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
25
from xml.dom import minidom
28
class LeoslyricsParser(object):
29
def __init__(self, artist, title):
33
def search(self, callback, *data):
34
artist = urllib.quote(self.artist)
35
title = urllib.quote(self.title)
37
htstring = 'http://api.leoslyrics.com/api_search.php?auth=Rhythmbox&artist=%s&songtitle=%s' % (artist, title)
40
loader.get_url (htstring, self.got_lyrics, callback, *data)
42
def got_lyrics (self, lyrics, callback, *data):
44
callback (None, *data)
48
xmldoc = minidom.parseString(lyrics).documentElement
51
callback (None, *data)
54
result_code = xmldoc.getElementsByTagName('response')[0].getAttribute('code')
55
if result_code != '0':
57
callback (None, *data)
60
matches = xmldoc.getElementsByTagName('result')[:10]
64
title = match.getElementsByTagName('title')[0].firstChild.data
65
artist = match.getElementsByTagName('name')[0].firstChild.data
67
if (re.search(self.title.lower().strip(), title.lower().strip()) and
68
re.search(self.artist.lower().strip(), artist.lower().strip())):
74
hids = map(lambda x: x.getAttribute('hid'), matches)
78
callback (None, *data)
83
lurl = "http://api.leoslyrics.com/api_lyrics.php?auth=Rhythmbox&hid=%s" % (urllib.quote(hids[0].encode('utf-8')))
85
loader.get_url (lurl, self.parse_lyrics, callback, *data)
88
def parse_lyrics(self, result, callback, *data):
90
callback (None, *data)
94
xmldoc = minidom.parseString(result).documentElement
97
callback (None, *data)
100
lyrics = xmldoc.getElementsByTagName('title')[0].firstChild.nodeValue
101
lyrics += ' - ' + xmldoc.getElementsByTagName('artist')[0].getElementsByTagName('name')[0].firstChild.nodeValue + '\n\n'
102
lyrics += xmldoc.getElementsByTagName('text')[0].firstChild.nodeValue
105
lyrics += "\n\nLyrics provided by leoslyrics.com"
107
callback (lyrics, *data)