-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemms-lyrics-lrclib.el
120 lines (100 loc) · 5.14 KB
/
emms-lyrics-lrclib.el
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
;;; emms-lyrics-lrclib.el --- Fetch synchronized lyrics through LRCLIB -*- lexical-binding: t; -*-
;; Copyright (C) 2024 Free Software Foundation, Inc.
;; Author: Daniel Semyonov <[email protected]>
;; This file is part of EMMS.
;; EMMS 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, or (at your option)
;; any later version.
;; EMMS 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 EMMS; see the file COPYING. If not, write to the Free
;; Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
;; MA 02110-1301, USA.
;;; Commentary:
;; This file provides a command/track initialization function which
;; automatically fetches synchronized lyrics for tracks (the current
;; track interactively) through an LRCLIB server.
;;; Code:
(eval-when-compile
(declare-function json-parse-buffer "json.c"))
(require 'emms-lyrics)
(require 'emms-later-do)
(defgroup emms-lyrics-lrclib nil
"EMMS module for fetching synchronized lyrics through LRCLIB servers."
:group 'emms-lyrics
:prefix "emms-lyrics-lrclib-")
(defcustom emms-lyrics-lrclib-url "https://lrclib.net/api/"
"Base URL for LRCLIB API requests."
:type 'string)
(defconst emms-lyrics-lrclib-max-requests 250
"Maximum number of concurrent requests to LRCLIB.")
(defvar emms-lyrics-lrclib-requests 0
"Current number of concurrent requests to LRCLIB.")
(defun emms-lyrics-lrclib-encode-name (name)
"Encode (artist/album/track) NAME for an LRCLIB search."
(and (stringp name) (string-replace " " "+" name)))
(defun emms-lyrics-lrclib-parse (_ file track interactive)
"Parse and save synced lyrics in FILE.
If TRACK is the selected track in the current playlist, catch up.
When INTERACTIVE is non-nil, display messages and confirm overwrite."
(unwind-protect
(let (lyrics)
(search-forward "\n\n")
(if-let* (((functionp 'json-available-p))
((json-available-p))
(p (json-parse-buffer :null-object nil)))
(and (hash-table-p p) (setq lyrics (gethash "syncedLyrics" p)))
(when-let* ((beg (search-forward "\"syncedLyrics\":\"" nil t))
(end (1- (search-forward-regexp "[^\\]\"" nil t))))
(replace-string-in-region "\\n" "\n" beg end)
(setq lyrics (buffer-substring-no-properties
beg (1- (point))))))
(and lyrics interactive (file-exists-p file)
(not (y-or-n-p (format "Overwrite existing file (\"%s\")?" file)))
(setq lyrics nil))
(when lyrics
(with-temp-file file (insert lyrics))
(when interactive (message "Saved synced lyrics at \"%s\"" file))
(and (boundp 'emms-lyrics-display-p)
emms-lyrics-display-p emms-player-playing-p
(equal track (emms-playlist-current-selected-track))
(emms-lyrics-catchup file))))
(setq emms-lyrics-lrclib-requests (1- emms-lyrics-lrclib-requests))))
;;;###autoload
(defun emms-lyrics-lrclib-get (&optional track force interactive)
"Search for synchronized lyrics for TRACK through LRCLIB's API.
If TRACK is omitted or nil, use the selected track in the current playlist.
The lyrics are saved in an \".lrc\" file alongside the track, unless the
file already exists (in which case the search isn't performed).
When called interactively (non-nil INTERACTIVE), display informative
messages, and with prefix argument FORCE, ask to overwrite existing
\".lrc\" files."
(interactive (list nil current-prefix-arg t))
(if (> emms-lyrics-lrclib-requests emms-lyrics-lrclib-max-requests)
(emms-later-do #'emms-lyrics-lrclib-get track force interactive)
(when-let* ((track (or track (emms-playlist-current-selected-track)))
((eq (emms-track-type track) 'file))
(file (emms-track-name track))
(lrc (replace-regexp-in-string "\\.[^.]+\\'" ".lrc" file))
((or force (not (file-exists-p lrc))))
((file-writable-p lrc))
(title (emms-lyrics-lrclib-encode-name
(emms-track-get track 'info-title)))
(artist (emms-lyrics-lrclib-encode-name
(emms-track-get track 'info-artist)))
(album (emms-lyrics-lrclib-encode-name
(emms-track-get track 'info-album)))
(time (emms-track-get track 'info-playing-time)))
(setq emms-lyrics-lrclib-requests (1+ emms-lyrics-lrclib-requests))
(when interactive (message "Searching for lyrics..."))
(url-retrieve
(url-encode-url
(format "%sget?artist_name=%s&track_name=%s&album_name=%s&duration=%d"
emms-lyrics-lrclib-url artist title album time))
#'emms-lyrics-lrclib-parse (list lrc track interactive)))))
(provide 'emms-lyrics-lrclib)
;;; emms-lyrics-lrclib.el ends here