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

« back to all changes in this revision

Viewing changes to plugins/lyrics/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
 
from rb.stringmatch import string_match
35
 
 
36
 
# these numbers pulled directly from the air
37
 
artist_match = 0.8
38
 
title_match = 0.5
39
 
 
40
 
# Python 2.4 compatibility
41
 
try:
42
 
        from xml.etree import cElementTree
43
 
except:
44
 
        import cElementTree
45
 
 
46
 
 
47
 
 
48
 
class LeoslyricsParser(object):
49
 
        def __init__(self, artist, title):
50
 
                self.artist = artist
51
 
                self.title = title
52
 
        
53
 
        def search(self, callback, *data):
54
 
                artist = urllib.quote(self.artist)
55
 
                title = urllib.quote(self.title)
56
 
 
57
 
                htstring = 'http://api.leoslyrics.com/api_search.php?auth=Rhythmbox&artist=%s&songtitle=%s' % (artist, title)
58
 
                        
59
 
                loader = rb.Loader()
60
 
                loader.get_url (htstring, self.got_lyrics, callback, *data)
61
 
 
62
 
        def got_lyrics (self, lyrics, callback, *data):
63
 
                if lyrics is None:
64
 
                        callback (None, *data)
65
 
                        return
66
 
 
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)
71
 
                        return
72
 
 
73
 
                match = None
74
 
                matches = element.find("searchResults").findall("result")
75
 
                print "got %d result(s)" % (len(matches))
76
 
                for m in matches:
77
 
                        matchtitle = m.findtext("title")
78
 
                        matchartist = m.findtext("artist/name")
79
 
 
80
 
                        # if we don't know the artist, then anyone will do
81
 
                        if self.artist != "":
82
 
                                artist_str = string_match(self.artist, matchartist)
83
 
                        else:
84
 
                                artist_str = artist_match + 0.1
85
 
 
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)
89
 
                                match = m
90
 
                                break
91
 
                        else:
92
 
                                print "skipping match, artist: %s (%f), title: %s (%f)" % (matchartist, artist_str, matchtitle, title_str)
93
 
 
94
 
                if match is not None:
95
 
                        hid = m.attrib['hid'].encode('utf-8')
96
 
                        lurl = "http://api.leoslyrics.com/api_lyrics.php?auth=Rhythmbox&hid=%s" % (urllib.quote(hid))
97
 
                        loader = rb.Loader()
98
 
                        loader.get_url (lurl, self.parse_lyrics, callback, *data)
99
 
                else:
100
 
                        print "no acceptable match found"
101
 
                        callback (None, *data)
102
 
 
103
 
 
104
 
        def parse_lyrics(self, result, callback, *data):
105
 
                if result is None:
106
 
                        callback (None, *data)
107
 
                        return
108
 
 
109
 
                element = cElementTree.fromstring(result)
110
 
 
111
 
                lyrics = element.find('lyric').find('text').text
112
 
                lyrics += "\n\nLyrics provided by leoslyrics.com"
113
 
 
114
 
                callback (lyrics, *data)
115