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
from rb.stringmatch import string_match
36
# these numbers pulled directly from the air
40
# Python 2.4 compatibility
42
from xml.etree import cElementTree
48
class LeoslyricsParser(object):
49
def __init__(self, artist, title):
53
def search(self, callback, *data):
54
artist = urllib.quote(self.artist)
55
title = urllib.quote(self.title)
57
htstring = 'http://api.leoslyrics.com/api_search.php?auth=Rhythmbox&artist=%s&songtitle=%s' % (artist, title)
60
loader.get_url (htstring, self.got_lyrics, callback, *data)
62
def got_lyrics (self, lyrics, callback, *data):
64
callback (None, *data)
67
element = cElementTree.fromstring(lyrics)
68
if element.find("response").attrib['code'] is not '0':
69
print "got failed response:" + lyrics
70
callback (None, *data)
74
matches = element.find("searchResults").findall("result")
75
print "got %d result(s)" % (len(matches))
77
matchtitle = m.findtext("title")
78
matchartist = m.findtext("artist/name")
80
# if we don't know the artist, then anyone will do
82
artist_str = string_match(self.artist, matchartist)
84
artist_str = artist_match + 0.1
86
title_str = string_match(self.title, matchtitle)
87
if artist_str > artist_match and title_str > title_match:
88
print "found acceptable match, artist: %s (%f), title: %s (%f)" % (matchartist, artist_str, matchtitle, title_str)
92
print "skipping match, artist: %s (%f), title: %s (%f)" % (matchartist, artist_str, matchtitle, title_str)
95
hid = m.attrib['hid'].encode('utf-8')
96
lurl = "http://api.leoslyrics.com/api_lyrics.php?auth=Rhythmbox&hid=%s" % (urllib.quote(hid))
98
loader.get_url (lurl, self.parse_lyrics, callback, *data)
100
print "no acceptable match found"
101
callback (None, *data)
104
def parse_lyrics(self, result, callback, *data):
106
callback (None, *data)
109
element = cElementTree.fromstring(result)
111
lyrics = element.find('lyric').find('text').text
112
lyrics += "\n\nLyrics provided by leoslyrics.com"
114
callback (lyrics, *data)