1
# -*- Mode: python; coding: utf-8; tab-width: 8; indent-tabs-mode: t; -*-
3
# Copyright (C) 2006 Jonathan Matthew
4
# Copyright (C) 2007 James Livingston
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2, or (at your option)
11
# The Rhythmbox authors hereby grant permission for non-GPL compatible
12
# GStreamer plugins to be used and distributed together with GStreamer
13
# and Rhythmbox. This permission is above and beyond the permissions granted
14
# by the GPL license by which Rhythmbox is covered. If you modify this code
15
# you may extend this exception to your version of the code, but you are not
16
# obligated to do so. If you do not wish to do so, delete this exception
17
# statement from your version.
20
# This program is distributed in the hope that it will be useful,
21
# but WITHOUT ANY WARRANTY; without even the implied warranty of
22
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
# GNU General Public License for more details.
25
# You should have received a copy of the GNU General Public License
26
# along with this program; if not, write to the Free Software
27
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
34
# these numbers pulled directly from the air
38
# Python 2.4 compatibility
40
from xml.etree import cElementTree
46
class LeoslyricsParser(object):
47
def __init__(self, artist, title):
51
def search(self, callback, *data):
52
artist = urllib.quote(self.artist)
53
title = urllib.quote(self.title)
55
htstring = 'http://api.leoslyrics.com/api_search.php?auth=Rhythmbox&artist=%s&songtitle=%s' % (artist, title)
58
loader.get_url (htstring, self.got_lyrics, callback, *data)
60
def got_lyrics (self, lyrics, callback, *data):
62
callback (None, *data)
65
element = cElementTree.fromstring(lyrics)
66
if element.find("response").attrib['code'] is not '0':
67
print "got failed response:" + lyrics
68
callback (None, *data)
72
matches = element.find("searchResults").findall("result")
73
print "got %d result(s)" % (len(matches))
75
matchtitle = m.findtext("title")
76
matchartist = m.findtext("artist/name")
78
# if we don't know the artist, then anyone will do
80
artist_str = rb.string_match(self.artist, matchartist)
82
artist_str = artist_match + 0.1
84
title_str = rb.string_match(self.title, matchtitle)
85
if artist_str > artist_match and title_str > title_match:
86
print "found acceptable match, artist: %s (%f), title: %s (%f)" % (matchartist, artist_str, matchtitle, title_str)
90
print "skipping match, artist: %s (%f), title: %s (%f)" % (matchartist, artist_str, matchtitle, title_str)
93
hid = m.attrib['hid'].encode('utf-8')
94
lurl = "http://api.leoslyrics.com/api_lyrics.php?auth=Rhythmbox&hid=%s" % (urllib.quote(hid))
96
loader.get_url (lurl, self.parse_lyrics, callback, *data)
98
print "no acceptable match found"
99
callback (None, *data)
102
def parse_lyrics(self, result, callback, *data):
104
callback (None, *data)
107
element = cElementTree.fromstring(result)
109
lyrics = element.find('lyric').find('text').text
110
lyrics += "\n\nLyrics provided by leoslyrics.com"
112
callback (lyrics, *data)