summaryrefslogtreecommitdiffhomepage
path: root/emms
diff options
context:
space:
mode:
authorJose Antonio Ortega Ruiz <jao@gnu.org>2010-10-12 23:39:29 +0200
committerJose Antonio Ortega Ruiz <jao@gnu.org>2010-10-12 23:39:29 +0200
commit1b0abee054235ea4ac7589dd92d3939e1386d24a (patch)
tree91d2aa7d5a810bb0032dfe1585ad8e0023c5f81e /emms
downloadelibs-1b0abee054235ea4ac7589dd92d3939e1386d24a.tar.gz
elibs-1b0abee054235ea4ac7589dd92d3939e1386d24a.tar.bz2
Initial contents
Diffstat (limited to 'emms')
-rw-r--r--emms/jao-emms-info-track.el123
-rw-r--r--emms/jao-emms-lyrics.el171
-rw-r--r--emms/jao-emms-random-album.el113
-rw-r--r--emms/jao-emms.el27
-rwxr-xr-xemms/leoslyrics.py84
-rwxr-xr-xemms/lyricwiki.rb51
6 files changed, 569 insertions, 0 deletions
diff --git a/emms/jao-emms-info-track.el b/emms/jao-emms-info-track.el
new file mode 100644
index 0000000..5fac8f3
--- /dev/null
+++ b/emms/jao-emms-info-track.el
@@ -0,0 +1,123 @@
+;; jao-emms-info-track.el -- utilities to show tracks
+
+;; Copyright (C) 2009, 2010 Jose Antonio Ortega Ruiz
+
+;; Author: Jose Antonio Ortega Ruiz <jao@gnu.org>
+;; Start date: Sat Jul 04, 2009 13:47
+
+;; This file is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation; either version 3 of the License, or
+;; (at your option) any later version.
+
+;; This file is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+;;; Code:
+
+(require 'emms)
+(require 'jao-osd)
+(require 'jao-emms)
+
+(defgroup jao-emms-faces nil "Faces"
+ :group 'faces
+ :group 'jao-emms)
+
+(defface jao-emms-font-lock-album '((t (:foreground "lightgoldenrod2")))
+ "Album name in EMMS track message."
+ :group 'jao-emms-faces)
+
+(defface jao-emms-font-lock-track '((t (:bold t)))
+ "Track number in EMMS track message."
+ :group 'jao-emms-faces)
+
+(defface jao-emms-font-lock-title '((t (:foreground "dodgerblue2")))
+ "Track title in EMMS track message."
+ :group 'jao-emms-faces)
+
+(defface jao-emms-font-lock-artist '((t (:foreground "dodgerblue3")))
+ "Artist name in EMMS track message."
+ :group 'jao-emms-faces)
+
+(defcustom jao-emms-show-osd-p nil
+ "Whether to show osd notices on track change"
+ :group 'jao-emms)
+
+
+
+(defun jao-emms-info-track-stream (track)
+ "Return track info for streams"
+ (let ((name (emms-track-name track))
+ (title (or (emms-track-get track 'title nil)
+ (car (emms-track-get track 'metadata nil)))))
+ (format "♪ %s (%s)" title (if title (emms-track-type track) name))))
+
+(defsubst jao--put-face (str face)
+ (put-text-property 0 (length str) 'face face str)
+ str)
+
+(defun jao-emms-info-track-file (track)
+ "Return a description of the current track."
+ (let ((no (string-to-number (emms-track-get track 'info-tracknumber "0")))
+ (time (emms-track-get track 'info-playing-time))
+ (artist (emms-track-get track 'info-artist ""))
+ (composer (emms-track-get track 'info-composer nil))
+ (title (emms-track-get track 'info-title ""))
+ (album (emms-track-get track 'info-album))
+ (last-played (or (emms-track-get track 'last-played) '(0 0 0)))
+ (play-count (or (emms-track-get track 'play-count) 0)))
+ (if (or (not title) (not album))
+ (emms-track-simple-description track)
+ (format "♪ %s%s%s%s%s %s"
+ (if time (format "[%02d:%02d] " (/ time 60) (mod time 60)) "")
+ (jao--put-face artist 'jao-emms-font-lock-artist)
+ (jao--put-face (if composer (format " [%s]" composer) "")
+ 'jao-emms-font-lock-artist)
+ (jao--put-face (if album (format " (%s)" album) " *")
+ 'jao-emms-font-lock-album)
+ (jao--put-face (if (zerop no) "" (format " %02d." no))
+ 'jao-emms-font-lock-track)
+ (jao--put-face title
+ 'jao-emms-font-lock-title)))))
+
+(defun jao-emms-info-track-description (track)
+ (if (memq (emms-track-type track) '(streamlist url))
+ (jao-emms-info-track-stream track)
+ (jao-emms-info-track-file track)))
+
+(defun jao-emms-toggle-osd ()
+ (interactive)
+ (setq jao-emms-show-osd-p (not jao-emms-show-osd-p))
+ (message "Emms OSD %s" (if jao-emms-show-osd-p "enabled" "disabled")))
+
+(defsubst jao-emms-current-track-str ()
+ (substring-no-properties (jao-emms-info-track-description
+ (emms-playlist-current-selected-track))))
+
+(defun jao-emms-show-osd ()
+ (interactive)
+ (let ((str (jao-emms-current-track-str)))
+ (when str (jao-osd-cat 'emms (substring str 2)))
+ t))
+
+(defun jao-emms-show-osd-hook ()
+ (interactive)
+ (when jao-emms-show-osd-p (jao-emms-show-osd))
+ t)
+
+(defun jao-emms-info-setup (&optional show-osd show-echo-line)
+ (setq emms-track-description-function 'jao-emms-info-track-description)
+ (setq jao-emms-show-osd-p show-osd)
+ (add-hook 'emms-player-started-hook 'jao-emms-show-osd-hook)
+ (unless show-echo-line
+ (eval-after-load 'emms-player-mpd
+ '(remove-hook 'emms-player-started-hook 'emms-player-mpd-show))))
+
+
+(provide 'jao-emms-info-track)
+;;; jao-emms-info-track.el ends here
diff --git a/emms/jao-emms-lyrics.el b/emms/jao-emms-lyrics.el
new file mode 100644
index 0000000..965f7cd
--- /dev/null
+++ b/emms/jao-emms-lyrics.el
@@ -0,0 +1,171 @@
+;; jao-emms-lyrics.el -- simple show lyrics in emms
+
+;; Copyright (C) 2009, 2010 Jose Antonio Ortega Ruiz
+
+;; Author: Jose Antonio Ortega Ruiz <jao@gnu.org>
+;; Start date: Sat Jul 04, 2009 13:41
+
+;; This file is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation; either version 3 of the License, or
+;; (at your option) any later version.
+
+;; This file is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+;;; Code:
+
+(require 'emms)
+
+(defvar jao-emms-lyrics-cache-dir "~/.emacs.d/emms-lyrics")
+
+(defun jao-emms-lyrics--filename (artist title)
+ (expand-file-name (format "%s.lyr" title)
+ (jao-emms-lyrics--ensure-dir artist)))
+
+(defun jao-emms-lyrics--ensure-dir (artist)
+ (let ((candidate (expand-file-name artist jao-emms-lyrics-cache-dir)))
+ (unless (file-directory-p candidate)
+ (make-directory candidate t))
+ candidate))
+
+(defun jao-emms-lyrics--get-cached (artist title)
+ (let ((candidate (jao-emms-lyrics--filename artist title)))
+ (and (file-exists-p candidate)
+ (with-current-buffer (find-file-noselect candidate)
+ (prog1
+ (buffer-string)
+ (kill-buffer))))))
+
+(defun jao-emms-lyrics--cache (artist title lyrics)
+ (with-current-buffer
+ (find-file-noselect (jao-emms-lyrics--filename artist title))
+ (delete-region (point-min) (point-max))
+ (insert lyrics)
+ (save-buffer)
+ (kill-buffer)))
+
+(make-variable-buffer-local
+ (defvar jao-emms-lyrics--path nil))
+
+(defvar jao-emms-lyrics-mode-map)
+(setq jao-emms-lyrics-mode-map
+ (let ((map (make-keymap)))
+ (suppress-keymap map)
+ (define-key map [?q] 'bury-buffer)
+ (define-key map [?g] 'jao-emms-show-lyrics)
+ (define-key map [?G] 'jao-emms-show-lyrics-force)
+ (define-key map [?e] 'jao-emms-edit-lyrics)
+ map))
+
+(defvar jao-emms-font-lock-artist 'bold)
+(defvar jao-emms-font-lock-title 'bold)
+
+(defun jao-emms-lyrics-mode ()
+ (interactive)
+ (kill-all-local-variables)
+ (use-local-map jao-emms-lyrics-mode-map)
+ (setq major-mode 'jao-emms-lyrics-mode)
+ (setq mode-name "lyrics")
+ (toggle-read-only 1))
+
+(defun jao-emms-lyrics-buffer ()
+ (or (get-buffer "*Emms Lyrics*")
+ (with-current-buffer (get-buffer-create "*Emms Lyrics*")
+ (jao-emms-lyrics-mode)
+ (current-buffer))))
+
+(defun jao-emms-lyrics-track-data ()
+ (let ((track (or (emms-playlist-current-selected-track)
+ (error "No playing track"))))
+ (cons (or (emms-track-get track 'info-artist nil)
+ (error "No artist"))
+ (or (emms-track-get track 'info-title nil)
+ (error "No artist")))))
+
+(defun jao-emms-edit-lyrics ()
+ (interactive)
+ (unless jao-emms-lyrics--path
+ (error "No track data available."))
+ (find-file-other-window jao-emms-lyrics--path))
+
+
+
+(defconst jao-emms--wiki-fmt
+ (concat"http://lyrics.wikia.com/api.php?action=lyrics&fmt=xml&func=getSong"
+ "&artist=%s&song=%s"))
+
+(defun jao-emms-show-lyrics/wiki ()
+ (interactive)
+ (let* ((a/t (jao-emms-lyrics-track-data))
+ (artist (car a/t))
+ (title (cdr a/t))
+ (buffer (jao-emms-lyrics-buffer))
+ (url (format jao-emms--wiki-fmt
+ (url-hexify-string artist)
+ (url-hexify-string title)))
+ (url-request-method "GET")
+ (data-buffer (url-retrieve-synchronously url))
+ (inhibit-read-only t))
+ (set-buffer data-buffer)
+ ;; (unless (re-search-forward "<pre>" nil t)
+ ;; (error "Lyrics not found"))
+ (let ((begin (point)))
+ ;; (unless (re-search-forward "</pre>" nil t)
+ ;; (error "Lyrics not found"))
+ (copy-to-buffer buffer begin (match-beginning 0)))
+ (with-current-buffer buffer
+ (goto-char (point-min))
+ (insert (format "♪ %s - %s\n" artist title)))
+ (pop-to-buffer buffer)))
+
+(defvar jao-emms-show-lyrics/script
+ (expand-file-name "lyricwiki.rb" (file-name-directory load-file-name)))
+
+(defun jao-emms-lyrics--download (artist title)
+ (message "Retrieving lyrics...")
+ (prog1
+ (shell-command-to-string (format "%s \"%s\" \"%s\""
+ jao-emms-show-lyrics/script
+ artist title))
+ (message nil)))
+
+(defun jao-emms-show-lyrics (&optional force)
+ (interactive "P")
+ (let* ((a/t (jao-emms-lyrics-track-data))
+ (artist (or (car a/t) ""))
+ (title (or (cdr a/t) ""))
+ (buffer (jao-emms-lyrics-buffer))
+ (cached (and (not force) (jao-emms-lyrics--get-cached artist title)))
+ (cached (and (not (zerop (length cached))) cached))
+ (lyrics (or cached (jao-emms-lyrics--download artist title)))
+ (inhibit-read-only t))
+ (with-current-buffer buffer
+ (delete-region (point-min) (point-max))
+ (insert (format "♪ %s - %s\n\n"
+ (propertize artist 'face jao-emms-font-lock-artist)
+ (propertize title 'face jao-emms-font-lock-title)))
+ (when lyrics
+ (insert lyrics)
+ (goto-char (point-min))
+ (when (not cached)
+ (save-excursion
+ (while (search-forward " " nil t)
+ (replace-match "" nil t)))))
+ (when (and lyrics (not cached))
+ (jao-emms-lyrics--cache artist title lyrics))
+ (setq jao-emms-lyrics--path (jao-emms-lyrics--filename artist title)))
+ (pop-to-buffer buffer)))
+
+(defun jao-emms-show-lyrics-force ()
+ (interactive)
+ (jao-emms-show-lyrics t))
+
+
+(provide 'jao-emms-lyrics)
+;;; jao-emms-lyrics.el ends here
diff --git a/emms/jao-emms-random-album.el b/emms/jao-emms-random-album.el
new file mode 100644
index 0000000..04dcd89
--- /dev/null
+++ b/emms/jao-emms-random-album.el
@@ -0,0 +1,113 @@
+;; jao-emms-random-album.el -- play random albums in emms
+
+;; Copyright (C) 2009, 2010 Jose Antonio Ortega Ruiz
+
+;; Author: Jose Antonio Ortega Ruiz <jao@gnu.org>
+;; Start date: Sat Jul 04, 2009 13:06
+
+;; This file is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation; either version 3 of the License, or
+;; (at your option) any later version.
+
+;; This file is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+(require 'emms)
+
+(defvar jao-emms-random-album-p t)
+(defvar jao-emms-random-lines nil)
+(defvar jao-emms-random-lines-file
+ (expand-file-name "~/.emacs.d/random-lines"))
+
+(defun jao-emms-random-lines ()
+ (or jao-emms-random-lines
+ (and (file-exists-p jao-emms-random-lines-file)
+ (with-current-buffer
+ (find-file-noselect jao-emms-random-lines-file)
+ (goto-char (point-min))
+ (setq jao-emms-random-lines (read (current-buffer)))))
+ (dotimes (n (1- (line-number-at-pos (point-max)))
+ jao-emms-random-lines)
+ (push (1+ n) jao-emms-random-lines))))
+
+(defun jao-emms-random-lines-save ()
+ (with-current-buffer (find-file-noselect jao-emms-random-lines-file)
+ (delete-region (point-min) (point-max))
+ (insert (format "%s\n" jao-emms-random-lines))
+ (save-buffer)))
+
+(defun jao-emms-goto-random-album ()
+ (let* ((pos (random (length (jao-emms-random-lines))))
+ (line (nth pos jao-emms-random-lines)))
+ (setq jao-emms-random-lines (remove line jao-emms-random-lines))
+ (jao-emms-random-lines-save)
+ (goto-line line)))
+
+(defun jao-emms-next-noerror ()
+ (interactive)
+ (when emms-player-playing-p
+ (error "A track is already being played"))
+ (cond (emms-repeat-track
+ (emms-start))
+ ((condition-case nil
+ (progn
+ (emms-playlist-current-select-next)
+ t)
+ (error nil))
+ (emms-start))
+ (t
+ (if jao-emms-random-album-p
+ (jao-emms-random-album-next)
+ (message "No next track in playlist")))))
+
+
+;; User interface
+(defun jao-emms-random-album-start ()
+ (interactive)
+ (setq jao-emms-random-album-p t)
+ (jao-emms-random-album-next))
+
+(defun jao-emms-random-album-stop ()
+ (interactive)
+ (setq jao-emms-random-album-p nil)
+ (emms-stop))
+
+(defun jao-emms-random-album-toggle ()
+ (interactive)
+ (setq jao-emms-random-album-p (not jao-emms-random-album-p))
+ (message "Random album %s"
+ (if jao-emms-random-album-p "enabled" "disabled")))
+
+(defun jao-emms-random-album-next ()
+ (interactive)
+ (let ((buffer (emms-browser-get-buffer)))
+ (save-excursion
+ (if buffer (set-buffer buffer) (emms-browser))
+ (ignore-errors (emms-browser-clear-playlist))
+ (emms-browse-by-album)
+ (jao-emms-goto-random-album)
+ (emms-browser-add-tracks-and-play)
+ (jao-osd-cat 'emms
+ (format "Next album %s"
+ (substring-no-properties (thing-at-point 'line)
+ 0 -1)))
+ (emms-browser-bury-buffer))))
+
+(defun jao-emms-random-album-reset ()
+ (interactive)
+ (setq jao-emms-random-lines nil)
+ (jao-emms-random-lines-save))
+
+(defun jao-emms-random-album-setup ()
+ (setq emms-player-next-function 'jao-emms-next-noerror))
+
+
+(provide 'jao-emms-random-album)
+;;; jao-emms-random-album.el ends here
diff --git a/emms/jao-emms.el b/emms/jao-emms.el
new file mode 100644
index 0000000..53b3513
--- /dev/null
+++ b/emms/jao-emms.el
@@ -0,0 +1,27 @@
+;; jao-emms.el -- shared bits
+
+;; Copyright (C) 2009, 2010 Jose Antonio Ortega Ruiz
+
+;; Author: Jose Antonio Ortega Ruiz <jao@gnu.org>
+;; Start date: Sat Jul 04, 2009 13:51
+
+;; This file is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation; either version 3 of the License, or
+;; (at your option) any later version.
+
+;; This file is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+;;; Code:
+
+(defgroup jao-emms nil "Emms extensions" :group 'emms)
+
+
+(provide 'jao-emms)
+;;; jao-emms.el ends here
diff --git a/emms/leoslyrics.py b/emms/leoslyrics.py
new file mode 100755
index 0000000..5e4f8c8
--- /dev/null
+++ b/emms/leoslyrics.py
@@ -0,0 +1,84 @@
+#!/usr/bin/python
+#
+# (c) 2004-2008 The Music Player Daemon Project
+# http://www.musicpd.org/
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#
+
+#
+# Load lyrics from leoslyrics.com
+#
+
+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).encode('utf-8')
diff --git a/emms/lyricwiki.rb b/emms/lyricwiki.rb
new file mode 100755
index 0000000..db7b970
--- /dev/null
+++ b/emms/lyricwiki.rb
@@ -0,0 +1,51 @@
+#!/usr/bin/env ruby
+#
+# (c) 2004-2008 The Music Player Daemon Project
+# http://www.musicpd.org/
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#
+
+#
+# Load lyrics from lyrics.wikia.com, formerly lyricwiki.org
+#
+
+require 'uri'
+require 'net/http'
+require 'cgi'
+
+url = "http://lyrics.wikia.com/api.php?action=lyrics&fmt=xml&func=getSong" + \
+ "&artist=#{URI.escape(ARGV[0])}&song=#{URI.escape(ARGV[1])}"
+response = Net::HTTP.get(URI.parse(url))
+
+if not response =~ /<url>\s*(.*?)\s*<\/url>/im
+ $stderr.puts "No URL in response!"
+ exit(1)
+end
+
+url = $1
+exit(69) if url =~ /action=edit$/
+
+response = Net::HTTP.get(URI.parse(url))
+if not response =~ /<div class='lyricbox'>\s*(.*?)\s*<!--/im
+ $stderr.puts "No <div class='lyricbox'> in lyrics page!\n"
+ exit(1)
+end
+
+if not $1 =~ /^.*<\/div>(.*?)$/im
+ $stderr.puts "Couldn't remove leading XML tags in lyricbox!\n"
+ exit(1)
+end
+
+puts CGI::unescapeHTML($1.gsub(/<br \/>/, "\n"))