~timo-jyrinki/ubuntu/utopic/rhythmbox/enable_grilo_rhythmbox

« back to all changes in this revision

Viewing changes to plugins/lyrics/lyrics/LeoslyricsParser.py

Tags: upstream-0.11.1
ImportĀ upstreamĀ versionĀ 0.11.1

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) 2005 Eduardo Gonzalez
 
4
# Copyright (C) 2006 Jonathan Matthew
 
5
# Copyright (C) 2007 James Livingston
 
6
#
 
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)
 
10
# any later version.
 
11
#
 
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.
 
16
#
 
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.
 
20
 
 
21
 
 
22
import urllib
 
23
import re
 
24
import rb
 
25
from xml.dom import minidom
 
26
 
 
27
 
 
28
class LeoslyricsParser(object):
 
29
        def __init__(self, artist, title):
 
30
                self.artist = artist
 
31
                self.title = title
 
32
        
 
33
        def search(self, callback, *data):
 
34
                artist = urllib.quote(self.artist)
 
35
                title = urllib.quote(self.title)
 
36
 
 
37
                htstring = 'http://api.leoslyrics.com/api_search.php?auth=Rhythmbox&artist=%s&songtitle=%s' % (artist, title)
 
38
                        
 
39
                loader = rb.Loader()
 
40
                loader.get_url (htstring, self.got_lyrics, callback, *data)
 
41
 
 
42
        def got_lyrics (self, lyrics, callback, *data):
 
43
                if lyrics is None:
 
44
                        callback (None, *data)
 
45
                        return
 
46
 
 
47
                try:
 
48
                        xmldoc = minidom.parseString(lyrics).documentElement
 
49
                except e:
 
50
                        print e
 
51
                        callback (None, *data)
 
52
                        return
 
53
 
 
54
                result_code = xmldoc.getElementsByTagName('response')[0].getAttribute('code')
 
55
                if result_code != '0':
 
56
                        xmldoc.unlink()
 
57
                        callback (None, *data)
 
58
                        return
 
59
                
 
60
                matches = xmldoc.getElementsByTagName('result')[:10]
 
61
                
 
62
                i = 0
 
63
                for match in matches:
 
64
                        title = match.getElementsByTagName('title')[0].firstChild.data
 
65
                        artist = match.getElementsByTagName('name')[0].firstChild.data
 
66
                        
 
67
                        if (re.search(self.title.lower().strip(), title.lower().strip()) and
 
68
                                        re.search(self.artist.lower().strip(), artist.lower().strip())):
 
69
                                continue
 
70
 
 
71
                        matches = matches[i:]
 
72
                        i += 1
 
73
                
 
74
                hids = map(lambda x: x.getAttribute('hid'), matches)
 
75
 
 
76
                if len(hids) == 0:
 
77
                        xmldoc.unlink()
 
78
                        callback (None, *data)
 
79
                        return
 
80
 
 
81
                xmldoc.unlink()
 
82
 
 
83
                lurl = "http://api.leoslyrics.com/api_lyrics.php?auth=Rhythmbox&hid=%s" % (urllib.quote(hids[0].encode('utf-8')))
 
84
                loader = rb.Loader()
 
85
                loader.get_url (lurl, self.parse_lyrics, callback, *data)
 
86
 
 
87
 
 
88
        def parse_lyrics(self, result, callback, *data):
 
89
                if result is None:
 
90
                        callback (None, *data)
 
91
                        return
 
92
 
 
93
                try:
 
94
                        xmldoc = minidom.parseString(result).documentElement
 
95
                except e:
 
96
                        print e
 
97
                        callback (None, *data)
 
98
                        return
 
99
 
 
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
 
103
                xmldoc.unlink()
 
104
 
 
105
                lyrics += "\n\nLyrics provided by leoslyrics.com"
 
106
 
 
107
                callback (lyrics, *data)