~ubuntu-branches/ubuntu/oneiric/rhythmbox/oneiric

« back to all changes in this revision

Viewing changes to plugins/lyrics/LeoslyricsParser.py

  • Committer: Bazaar Package Importer
  • Author(s): Rico Tzschichholz
  • Date: 2011-07-29 16:41:38 UTC
  • mto: This revision was merged to the branch mainline in revision 191.
  • Revision ID: james.westby@ubuntu.com-20110729164138-wwicy8nqalm18ck7
Tags: upstream-2.90.1~20110802
ImportĀ upstreamĀ versionĀ 2.90.1~20110802

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- Mode: python; coding: utf-8; tab-width: 8; indent-tabs-mode: t; -*-
 
2
#
 
3
# Copyright (C) 2006 Jonathan Matthew
 
4
# Copyright (C) 2007 James Livingston
 
5
#
 
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)
 
9
# any later version.
 
10
#
 
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.
 
18
#
 
19
#
 
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.
 
24
#
 
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.
 
28
 
 
29
 
 
30
import urllib
 
31
import re
 
32
import rb
 
33
 
 
34
# these numbers pulled directly from the air
 
35
artist_match = 0.8
 
36
title_match = 0.5
 
37
 
 
38
# Python 2.4 compatibility
 
39
try:
 
40
        from xml.etree import cElementTree
 
41
except:
 
42
        import cElementTree
 
43
 
 
44
 
 
45
 
 
46
class LeoslyricsParser(object):
 
47
        def __init__(self, artist, title):
 
48
                self.artist = artist
 
49
                self.title = title
 
50
        
 
51
        def search(self, callback, *data):
 
52
                artist = urllib.quote(self.artist)
 
53
                title = urllib.quote(self.title)
 
54
 
 
55
                htstring = 'http://api.leoslyrics.com/api_search.php?auth=Rhythmbox&artist=%s&songtitle=%s' % (artist, title)
 
56
                        
 
57
                loader = rb.Loader()
 
58
                loader.get_url (htstring, self.got_lyrics, callback, *data)
 
59
 
 
60
        def got_lyrics (self, lyrics, callback, *data):
 
61
                if lyrics is None:
 
62
                        callback (None, *data)
 
63
                        return
 
64
 
 
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)
 
69
                        return
 
70
 
 
71
                match = None
 
72
                matches = element.find("searchResults").findall("result")
 
73
                print "got %d result(s)" % (len(matches))
 
74
                for m in matches:
 
75
                        matchtitle = m.findtext("title")
 
76
                        matchartist = m.findtext("artist/name")
 
77
 
 
78
                        # if we don't know the artist, then anyone will do
 
79
                        if self.artist != "":
 
80
                                artist_str = rb.string_match(self.artist, matchartist)
 
81
                        else:
 
82
                                artist_str = artist_match + 0.1
 
83
 
 
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)
 
87
                                match = m
 
88
                                break
 
89
                        else:
 
90
                                print "skipping match, artist: %s (%f), title: %s (%f)" % (matchartist, artist_str, matchtitle, title_str)
 
91
 
 
92
                if match is not None:
 
93
                        hid = m.attrib['hid'].encode('utf-8')
 
94
                        lurl = "http://api.leoslyrics.com/api_lyrics.php?auth=Rhythmbox&hid=%s" % (urllib.quote(hid))
 
95
                        loader = rb.Loader()
 
96
                        loader.get_url (lurl, self.parse_lyrics, callback, *data)
 
97
                else:
 
98
                        print "no acceptable match found"
 
99
                        callback (None, *data)
 
100
 
 
101
 
 
102
        def parse_lyrics(self, result, callback, *data):
 
103
                if result is None:
 
104
                        callback (None, *data)
 
105
                        return
 
106
 
 
107
                element = cElementTree.fromstring(result)
 
108
 
 
109
                lyrics = element.find('lyric').find('text').text
 
110
                lyrics += "\n\nLyrics provided by leoslyrics.com"
 
111
 
 
112
                callback (lyrics, *data)
 
113