aboutsummaryrefslogtreecommitdiffstats
path: root/lyrics/leoslyrics.py
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2008-09-16 19:11:40 +0200
committerMax Kellermann <max@duempel.org>2008-09-16 19:11:40 +0200
commitf41b9942af7278ab67dc799ad6c17ad74dc0aa1b (patch)
tree69c7c715ff3abd78bb26a7a4d80dd1da5fd5c08b /lyrics/leoslyrics.py
parent4d01c183b4c1e4c51dff3d9aeec1c01ce13a4323 (diff)
downloadmpd-f41b9942af7278ab67dc799ad6c17ad74dc0aa1b.tar.gz
mpd-f41b9942af7278ab67dc799ad6c17ad74dc0aa1b.tar.xz
mpd-f41b9942af7278ab67dc799ad6c17ad74dc0aa1b.zip
lyrics: converted in-process plugins to external programs
In-process plugins are very problematic. It is much easier and flexible to move the lyrics plugins to external programs, with a trivial protocol. This is work in progress, among the things missing: - protocol specification, including exit codes - plugin installation - plugin search directory - run-time configuration (currently hard coded) - automatic polling (using glib's main loop?) - better and more robust error handling
Diffstat (limited to 'lyrics/leoslyrics.py')
-rwxr-xr-xlyrics/leoslyrics.py68
1 files changed, 68 insertions, 0 deletions
diff --git a/lyrics/leoslyrics.py b/lyrics/leoslyrics.py
new file mode 100755
index 000000000..0cee65aee
--- /dev/null
+++ b/lyrics/leoslyrics.py
@@ -0,0 +1,68 @@
+#!/usr/bin/python
+#
+# Load lyrics from leoslyrics.com
+#
+# Author: Max Kellermann <max@duempel.org>
+#
+
+from sys import argv, exit
+from urllib import urlencode, urlopen
+from xml.sax import make_parser, SAXException
+from xml.sax.handler import ContentHandler
+
+class SearchContentHandler(ContentHandler):
+ def __init__(self):
+ self.code = None
+ self.hid = None
+
+ def startElement(self, name, attrs):
+ if name == 'response':
+ self.code = int(attrs['code'])
+ elif name == 'result':
+ if self.hid is None or attrs['exactMatch'] == 'true':
+ self.hid = attrs['hid']
+
+def search(artist, title):
+ query = urlencode({'auth': 'ncmpc',
+ 'artist': artist,
+ 'songtitle': title})
+ url = "http://api.leoslyrics.com/api_search.php?" + query
+ f = urlopen(url)
+ handler = SearchContentHandler()
+ parser = make_parser()
+ parser.setContentHandler(handler)
+ parser.parse(f)
+ return handler.hid
+
+class LyricsContentHandler(ContentHandler):
+ def __init__(self):
+ self.code = None
+ self.is_text = False
+ self.text = None
+
+ def startElement(self, name, attrs):
+ if name == 'text':
+ self.text = ''
+ self.is_text = True
+ else:
+ self.is_text = False
+
+ def characters(self, chars):
+ if self.is_text:
+ self.text += chars
+
+def lyrics(hid):
+ query = urlencode({'auth': 'ncmpc',
+ 'hid': hid})
+ url = "http://api.leoslyrics.com/api_lyrics.php?" + query
+ f = urlopen(url)
+ handler = LyricsContentHandler()
+ parser = make_parser()
+ parser.setContentHandler(handler)
+ parser.parse(f)
+ return handler.text
+
+hid = search(argv[1], argv[2])
+if hid is None:
+ exit(2)
+print lyrics(hid)