3 # (c) 2004-2008 The Music Player Daemon Project
4 # http://www.musicpd.org/
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 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 # Load lyrics from leoslyrics.com
24 from sys
import argv
, exit
, stderr
25 from urllib
import urlencode
, urlopen
26 from xml
.sax
import make_parser
, SAXException
27 from xml
.sax
.handler
import ContentHandler
29 class SearchContentHandler(ContentHandler
):
34 def startElement(self
, name
, attrs
):
35 if name
== 'response':
36 self
.code
= int(attrs
['code'])
37 elif name
== 'result':
38 if self
.hid
is None or attrs
['exactMatch'] == 'true':
39 self
.hid
= attrs
['hid']
41 def search(artist
, title
):
42 query
= urlencode({'auth': 'ncmpc',
45 url
= "http://api.leoslyrics.com/api_search.php?" + query
49 stderr
.write("Failed to connect to http://api.leoslyrics.com, it seems down!\n")
51 handler
= SearchContentHandler()
52 parser
= make_parser()
53 parser
.setContentHandler(handler
)
57 stderr
.write("Failed to parse the search result!\n")
61 class LyricsContentHandler(ContentHandler
):
67 def startElement(self
, name
, attrs
):
74 def characters(self
, chars
):
79 query
= urlencode({'auth': 'ncmpc',
81 url
= "http://api.leoslyrics.com/api_lyrics.php?" + query
83 handler
= LyricsContentHandler()
84 parser
= make_parser()
85 parser
.setContentHandler(handler
)
89 stderr
.write("Failed to parse the lyrics!\n")
93 hid
= search(argv
[1], argv
[2])
96 print lyrics(hid
).encode('utf-8').rstrip()