forked from SpringHan/netease-cloud-music.el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetease-cloud-music-functions.el
338 lines (295 loc) · 11.8 KB
/
netease-cloud-music-functions.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
;;; netease-cloud-music-functions.el --- Netease Cloud Music client for Emacs -*- lexical-binding: t -*-
;; Author: SpringHan
;; Maintainer: SpringHan
;; Version: 2.1
;; This file is not part of GNU Emacs
;; 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, 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.
;; For a full copy of the GNU General Public License
;; see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Netease Cloud Music Client for Emacs.
;;; Code:
(require 'request)
(defcustom netease-cloud-music-api-type (cond ((executable-find "npm") 'npm)
((or (executable-find "docker")
(executable-find "podman"))
'docker))
"How to manage the api.
Its value can be as following:
npm: download api project into `netease-cloud-music-api-dir' and run npm
locally.
docker: use docker to start the api."
:group 'netease-cloud-music
:type '(choice (const :tag "Native" native)
(const :tag "Docker" docker)))
(defvar eaf--buffer-id)
(defcustom netease-cloud-music-api-port "3000"
"The port for the API process."
:type 'string
:group 'netease-cloud-music)
(defvar netease-cloud-music-phone nil
"Phone number.")
(defvar netease-cloud-music-user-password nil
"Password.")
(eval-and-compile
(defmacro netease-cloud-music-api-defun (name arg &optional docstring &rest body)
"Like `defun', but it will check the third-party api's status first.
NAME is the function's name.
ARG is the arguments for function.
DOCSTRING is the doc-string for the function.
BODY is the main codes for the function."
(declare (indent defun)
(debug defun)
(doc-string 3))
`(defun ,name ,arg
,(when docstring
docstring)
,(when (equal (car (car body)) 'interactive)
(prog1 (car body)
(pop body)))
(if (not (or (not (netease-cloud-music--api-need-downloaded))
(netease-cloud-music--api-downloaded))) ; = !(x -> y)
(netease-cloud-music-error "The third-party API has not been donwloaded!")
,@body)))
(defmacro netease-cloud-music-for-eaf (&rest body)
"The macro for eaf. BODY is the Lisp you want to execute."
(let ((with-eaf-buffer (eq (car body) :eaf-buffer)))
(when with-eaf-buffer
(pop body))
`(when (get-buffer "eaf-netease-cloud-music")
,(if with-eaf-buffer
`(with-current-buffer "eaf-netease-cloud-music"
,@body)
`(eval ,@body)))))
(defmacro netease-cloud-music-eaf-defun (name args &rest body)
"If the NAME function is not exists, define it.
ARGS is the arguments.
BODY is the body of the function."
(declare (indent defun))
(unless (or (functionp (symbol-function name))
(macrop (symbol-function name)))
`(defun ,name ,args
,@body)))
(defmacro netease-cloud-music-expand-form (&rest form)
"Expand FORM in function-form."
`(cl-function
(lambda (&key data &allow-other-keys)
,@form)))
(defmacro netease-cloud-music--api-func (action)
"Call the function which is matched with `netease-cloud-music--.*-api-.*'.
ACTION is its function."
(when netease-cloud-music-api-type
(let* ((sfunc (format "netease-cloud-music--%s-api-%s"
(symbol-name action)
(symbol-name netease-cloud-music-api-type)))
(func (intern sfunc)))
`(,func)))))
(defun netease-cloud-music-error (&rest seq)
"Print the error message, it's SEQ."
(let ((main (pop seq)))
(eval `(user-error (concat "[Netease-Cloud-Music]: " ,main)
,@seq))))
(defun netease-cloud-music-ask (type)
"Ask user TYPE of the question.
If user reply y, return t.
Otherwise return nil."
(let ((ask (pcase type
('song "The info of the song is here, do you want to listen it?")
('add-song-to-playlist "Do you want to add the current song into playlist?")
('delete-song-from-playlist "Do you want to delete the song from playlist?")))
result)
(setq result (read-char (concat ask "(y/n)")))
(when (= result 121)
t)))
(defun netease-cloud-music-get-song (data)
"Read the Netease Music json DATA and return the result."
(let (song artist result)
(if (/= 200 (alist-get 'code data))
(netease-cloud-music-error "The song you search is error!")
(setq data (alist-get 'songs (alist-get 'result data)))
(dotimes (n (length data))
(setq song (aref data n)
artist (aref (alist-get 'artists song) 0))
(setq result (append result
(list
(list (alist-get 'id song)
(alist-get 'name song)
(alist-get 'id artist)
(alist-get 'name artist))))))
result)))
(defun netease-cloud-music-get-playlists (data)
"Read the playlist json DATA searched from API."
(let (playlist result)
(if (/= 200 (alist-get 'code data))
(netease-cloud-music-error "The playlist you search is error!")
(setq data (alist-get 'playlists (alist-get 'result data)))
(dotimes (n (length data))
(setq playlist (aref data n))
(setq result (append result
(list
(cons (alist-get 'name playlist)
(alist-get 'id playlist))))))
result)))
(defun netease-cloud-music--current-lyric (string)
"Get the lyric from STRING."
(ignore-errors
(match-string
(if (string-match "\\[\\(.*\\):\\(.*\\)\\.\\(.*\\)\\]\\(.*\\)" string)
4
(when (string-match
"\\[\\(.*\\):\\(.*\\)\\]\\(.*\\)"
string)
3))
string)))
(defun netease-cloud-music--index (ele list)
"Get the index of ELE in LIST. Use `equal' to check."
(let ((index 0))
(catch 'stop
(dolist (item list)
(when (equal ele item)
(throw 'stop t))
(setq index (1+ index))))
index))
(defun netease-cloud-music--memeq (ele list)
"Check if ELE is in LIST.
Like `memq', but use `equal'."
(catch 'result
(let ((index 0))
(dolist (item list)
(when (equal ele item)
(throw 'result index))
(setq index (1+ index)))
nil)))
(defun netease-cloud-music--api-need-downloaded ()
"Check if `netease-cloud-music-api-type' is depended on downloaded repo."
(memq netease-cloud-music-api-type '(npm)))
(defun netease-cloud-music-api-request (url)
"Request with the user info.
URL is the url to request."
(let (result)
(setq url (format "http://localhost:%s/%s"
netease-cloud-music-api-port url))
(request (format "http://localhost:%s/login/cellphone?phone=%s&md5_password=%s&countrycode=%s"
netease-cloud-music-api-port
(cdr netease-cloud-music-phone)
netease-cloud-music-user-password
(car netease-cloud-music-phone))
:success (netease-cloud-music-expand-form
data ;PlaceHolder
(request url
:parser 'buffer-string
:success (netease-cloud-music-expand-form
(with-current-buffer (get-buffer-create " *Request*")
(erase-buffer)
(insert data)))
:sync t))
:error (cl-function (lambda (&rest args &key error-thrown &allow-other-keys)
(when (string-match-p "^exited abnormally with code \\(.*\\)"
(cdr error-thrown))
(message nil)) ;Ignore the warning when API has not finished starting.
(when (get-buffer " *Request")
(with-current-buffer " *Request*"
(erase-buffer)))))
:sync t)
(when (get-buffer " *Request*")
(with-current-buffer " *Request*"
(unless (string-empty-p (buffer-string))
(setq result (json-read-from-string (buffer-string))))))
result))
(defun netease-cloud-music--request (url)
"Like `netease-cloud-music-api-request', but do not login."
(let (result)
(request url
:parser 'json-read
:success (netease-cloud-music-expand-form (setq result data))
:sync t)
result))
(defun netease-cloud-music-alist-cdr (key list)
"Find the first item in LIST which `cdr' is equal to KEY.
Use `equal' to compare.
If the item is exists, return the cons."
(if (not (listp list))
(user-error "The %S is not a list!" list)
(let (item)
(setq item
(catch 'stop
(dolist (ele list)
(when (equal key (cdr ele))
(throw 'stop ele)))))
item)))
(defun netease-cloud-music--car-eq (key list &optional index all)
"Find the item whose `car' is equal to KEY in LIST.
If index is non-nil, return the item's index.
Otherwise return item itself.
When ALL is non-nil, return item & its index."
(when (consp list)
(catch 'result
(dotimes (i (length list))
(when (eq key (car (nth i list)))
(throw 'result (cond (all (cons i (nth i list)))
(index i)
(t (nth i list)))))))))
(defun netease-cloud-music--get-lyric-time (lyric)
"Get the LYRIC's time."
(let (min sec)
(progn
(string-match "\\[\\(.*\\):\\(.*\\)\\.\\(.*\\)\\]\\(.*\\)"
lyric)
(setq min (match-string 2 lyric)
sec (match-string 3 lyric)))
(string-to-number (concat min "." sec))))
(defun netease-cloud-music--format-lyric-time (time)
"Format lyric TIME."
(if (or (< time 0)
(< (length (number-to-string time)) 5))
time
(let ((time-string (number-to-string time))
sec msec)
(progn
(string-match "\\(.*\\)\\.\\(.*\\)" time-string)
(setq sec (match-string 1 time-string)
msec (match-string 2 time-string)))
(string-to-number
(concat sec "." (substring msec
0 2))))))
(defun netease-cloud-music--cons-to-list (cons)
"Convert cons list to list."
(let (list)
(dolist (item cons)
(setq list (append list
(list (list (car item)
(cdr item))))))
list))
(defun netease-cloud-music--slice (list start end)
"Get slice of LIST from START to END."
(when (< start 0)
(setq start 0))
(let (result)
(catch 'stop
(dotimes (i (length list))
(when (= i end)
(throw 'stop t))
(when (>= i start)
(setq result (append result (list (nth i list)))))))
result))
(netease-cloud-music-eaf-defun eaf-call-async (&rest args)
args)
(netease-cloud-music-eaf-defun eaf--netease-cloud-music--update-song-style ())
(netease-cloud-music-eaf-defun eaf--netease-cloud-music-change-play-status ())
(defun netease-cloud-music-call-js (func &optional args)
"Call js FUNC with ARGS."
(interactive)
(unless args
(setq args ""))
;; Ensure this is only called from EAF buffer
(when (derived-mode-p 'eaf-mode)
(eaf-call-async "execute_js_function" eaf--buffer-id (string-trim-left func "js_") args)))
(provide 'netease-cloud-music-functions)
;;; netease-cloud-music-functions.el ends here