summaryrefslogtreecommitdiffhomepage
path: root/emms/jao-emms-lyrics.el
blob: 965f7cdb52e8fa1d37787d94f34aa602e2584ae3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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