aboutsummaryrefslogtreecommitdiffstats
path: root/lyrics
diff options
context:
space:
mode:
Diffstat (limited to 'lyrics')
-rwxr-xr-xlyrics/hd.py22
-rwxr-xr-xlyrics/leoslyrics.py68
-rwxr-xr-xlyrics/lyricswiki.rb16
3 files changed, 106 insertions, 0 deletions
diff --git a/lyrics/hd.py b/lyrics/hd.py
new file mode 100755
index 000000000..173342c98
--- /dev/null
+++ b/lyrics/hd.py
@@ -0,0 +1,22 @@
+#!/usr/bin/python
+#
+# Load lyrics from the user's home directory
+#
+# Author: Max Kellermann <max@duempel.org>
+#
+
+from sys import argv, exit, stdout
+from os import environ
+from os.path import expanduser
+
+path = expanduser("~/.lyrics/%s - %s.txt" % (argv[1], argv[2]))
+try:
+ f = file(path)
+except IOError:
+ exit(2)
+
+while True:
+ x = f.read(4096)
+ if not x:
+ break
+ stdout.write(x)
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)
diff --git a/lyrics/lyricswiki.rb b/lyrics/lyricswiki.rb
new file mode 100755
index 000000000..39aa4a5b7
--- /dev/null
+++ b/lyrics/lyricswiki.rb
@@ -0,0 +1,16 @@
+#!/usr/bin/env ruby
+#
+# Load lyrics from lyricswiki.org
+#
+# Author: Max Kellermann <max@duempel.org>
+#
+
+require 'uri'
+require 'net/http'
+
+url = "http://lyricwiki.org/api.php" + \
+ "?artist=#{URI.escape(ARGV[0])}&song=#{URI.escape(ARGV[1])}"
+response = Net::HTTP.get(URI.parse(url))
+
+exit(2) unless response =~ /<pre>\s*(.*?)\s*<\/pre>/im
+puts $1