Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 176 lines (159 sloc) 7.195 kb
343d5fd @talau Initial commit for emms-get-lyrics.el
authored
1 ;;; emms-get-lyrics.el --- Get the lyrics of the song emms is currently playing
2
3 ;; Copyright (C) 2007 Jay Belanger
4 ;; Copyright (C) 2010 Marcos Talau
5
6 ;; emms-get-lyrics.el is free software; you can redistribute it and/or
7 ;; modify it under the terms of the GNU General Public License as
8 ;; published by the Free Software Foundation; either version 2, or
9 ;; (at your option) any later version.
10
11 ;; emms-get-lyrics.el is distributed in the hope that it will be
12 ;; useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13 ;; of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;; General Public License for more details.
15
16 ;;; Commentary:
17
18 ;; The function 'emms-get-lyrics-current-song' tries to
19 ;; get the lyrics to the song that emms is currently playing.
20 ;; It currently requires w3m to get the lyrics.
21 ;; It copies the lyrics to a file ending in .lyrics; if the variable
22 ;; `emms-get-lyrics-use-file' is nil, it will just display the lyrics
23 ;; in a buffer without saving them to a file.
24 ;; If the variable `emms-get-lyrics-dir' is non-nil, then the lyrics will
25 ;; be put in this directory with the file ARTIST-TITLE.lyrics;
26 ;; otherwise it will be put in the same directory as the song file, in
27 ;; a file with the same name as the song file except the extension will
28 ;; by ".lyrics".
29
30 ;;; Code:
31
32 (defvar emms-get-lyrics-version "2.0")
33
34 ;;; User Customization
35
36 (defvar emms-get-lyrics-use-files t)
37 (defvar emms-get-lyrics-dir nil)
38 (defvar emms-get-lyrics-match-lyric-found "From LyricWiki")
39 (defvar emms-get-lyrics-match-lyric-notfound "This page needs content.")
40 (defvar emms-get-lyrics-match-ad-top "Ringtone to your Cell phone")
41 (defvar emms-get-lyrics-match-ad-bottom "Ringtone to your Cell phone")
42 (defvar emms-get-lyrics-debug nil)
43
44 (defun emms-get-lyrics-url (artist title)
45 "Return the URL of lyric."
46 (let ((url (concat
47 "http://www.lyricwiki.org/index.php?title="
48 (replace-regexp-in-string
49 " " "_"
50 (concat
51 artist
52 ":"
53 title))
54 "&printable=yes")))
55 (if emms-get-lyrics-debug
56 (message "emms-get-lyrics-url %s" url)
57 )
58 url))
59
60 (defun emms-get-lyrics-w3m (url buffer)
61 "Get the lyric in the Internet and put it in the BUFFER."
62 (if emms-get-lyrics-debug
63 (message "calling: w3m -dump %s" url))
64 (call-process "w3m" nil buffer nil "-dump" url))
65
66 (defun emms-get-lyrics-redirect-check (url buffer)
67 "If need to redirect to another site, do this!"
68 (goto-char (point-min))
69 (cond ((search-forward "#REDIRECT" nil t)
70 (if emms-get-lyrics-debug
71 (message "Redirecting to %s" url))
72 (delete-region (point-min) (point-max))
73 (emms-get-lyrics-w3m url buffer))))
74
75 (defun emms-get-lyrics-write-lyric (bname title artist)
76 "When Lyric is not found, ask the user if he wants to write it."
77 (switch-to-buffer bname)
78 (delete-region (point-min) (point-max))
79 (insert ";; This lyric is not found at LyricWiki.org\n")
80 (insert ";; You can write the lyric or try to find it\n")
81 (insert ";; in the Internet.\n")
82 (insert ";; Once you writted or finded, please, post it in LyricWiki.org\n\n")
83 (insert title " (" artist ")\n\n"))
84
85 (defun emms-get-lyrics (artist title fn &optional file)
86 "Core function. Get lyrics, put it in the buffer and format it."
87 (let ((bname (concat "Lyrics: " title " (" artist ")")))
88 (cond ((get-buffer bname)
89 (switch-to-buffer bname))
90 ((and file (file-exists-p file))
91 (find-file file)
92 (rename-buffer bname))
93 (t
94 (let ((buffer (if file
95 (find-file-noselect file)
96 (get-buffer-create bname))))
97 (set-buffer buffer)
98 (funcall fn (emms-get-lyrics-url artist title) buffer)
99 (goto-char (point-min))
100 (if (and
101 (search-forward emms-get-lyrics-match-lyric-found nil t)
102 (not (search-forward
103 emms-get-lyrics-match-lyric-notfound nil t)))
104 ; where lyrics from?
105 (let ((frominsert
106 (save-excursion
107 (if (re-search-forward "^Retrieved from")
108 (buffer-substring-no-properties
109 (+ (line-beginning-position) 16)
110 (- (line-end-position) 1))
111 "From LyricWiki"))))
112 (emms-get-lyrics-redirect-check frominsert buffer)
113 (goto-char (point-min))
114 (insert "(from " frominsert ")\n\n")
115 (insert title " (" artist ")\n")
116 ; delete ads, and save disk space ;D
117 (let ((trash-point (point)))
118 (search-forward emms-get-lyrics-match-ad-top)
119 (delete-region trash-point (point)))
120 (search-forward emms-get-lyrics-match-ad-bottom)
121 (move-beginning-of-line nil)
122 (kill-line)
123 ; delete bottom
124 (goto-char (point-max)) (move-beginning-of-line nil)
125 (if (or
126 (search-backward "External links" nil t)
127 (search-backward "Retrieved from" nil t))
128 (delete-region (point) (point-max)))
129 (when file
130 (rename-buffer bname)
131 (save-buffer))
132 (goto-char (point-min))
133 (switch-to-buffer buffer)
134 (goto-char (point-min)))
135 (restore-buffer-modified-p nil)
136 (if (y-or-n-p (concat "Sorry, unable to find lyrics for " artist " (" file "). Do you want help writing this lyrics now? (say yes :-) ) "))
137 (emms-get-lyrics-write-lyric bname title artist)
138 (kill-buffer))
139 ))))))
140
141 (defun emms-get-lyrics-current-song ()
142 "From current song extract info and find the Lyric."
143 (interactive)
144 (let* ((track (emms-playlist-current-selected-track))
145 (artist (cdr (assoc 'info-artist track)))
146 (title (cdr (assoc 'info-title track))))
147 (cond ((not emms-player-playing-p)
148 (message "Nothing playing right now"))
149 ((not (executable-find "w3m"))
150 (message "You need the program w3m"))
151 ((not (executable-find emms-info-mp3info-program-name))
152 (message "You need the program %s (If you install it, reopen your Emacs.)" emms-info-mp3info-program-name))
153 ((not artist)
154 (message "No artist name in current song"))
155 ((not title)
156 (message "No title in current song"))
157 (t (emms-get-lyrics artist title 'emms-get-lyrics-w3m
158 (if emms-get-lyrics-use-files
159 (if emms-get-lyrics-dir
160 (concat
161 emms-get-lyrics-dir
162 "/"
163 (replace-regexp-in-string
164 " " "_"
165 (concat
166 artist
167 "-"
168 title
169 ".lyrics")))
170 (concat
171 (file-name-sans-extension (cdr (assoc 'name track)))
172 ".lyrics"))))))))
173
174 (provide 'emms-get-lyrics)
175 ;;; emms-get-lyrics.el ends here
Something went wrong with that request. Please try again.