-
Notifications
You must be signed in to change notification settings - Fork 0
/
eaf-netease-cloud-music.el
325 lines (290 loc) · 13.7 KB
/
eaf-netease-cloud-music.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
;;; eaf-netease-cloud-music.el --- EAF Netease Cloud Music App -*- lexical-binding: t -*-
;; Author: SpringHan
;; Maintainer: SpringHan
;; 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:
;;; Code:
(defcustom eaf-netease-cloud-music-user+list '()
""
:type 'cons)
(defcustom eaf-netease-cloud-music-keybinding
'(("<f12>" . "open_devtools")
("<up>" . "eaf--netease-cloud-music-move-song-up")
("<down>" . "eaf--netease-cloud-music-move-song-down")
("SPC" . "netease-cloud-music-pause-or-continue")
("<return>" . "eaf--netease-cloud-music-switch-enter")
("C-n" . "js_scroll_up")
("C-p" . "js_scroll_down")
("M-v" . "js_scroll_up_page")
("M-V" . "js_scroll_down_page")
("M-<" . "js_scroll_to_begin")
("M->" . "js_scroll_to_bottom")
("M-f" . "netease-cloud-music-switch-next-page")
("M-b" . "netease-cloud-music-switch-prev-page")
("M-n" . "js_scroll_playlist_up")
("M-p" . "js_scroll_playlist_down")
("q" . "netease-cloud-music-back")
("Q" . "netease-cloud-music-quit")
("r" . "netease-cloud-music-change-repeat-mode")
("x" . "netease-cloud-music-kill-current-song")
("/" . "eaf--netease-cloud-music-play-with-index")
("n" . "js_play_next")
("N" . "netease-cloud-music-random-play")
("p" . "js_play_prev")
("P" . "netease-cloud-music-playlist-play")
("c" . "netease-cloud-music-change-lyric-type")
("d" . "eaf--netease-cloud-music-delete-song-from-playlist")
("D" . "netease-cloud-music-delete-playing-song")
("<" . "netease-cloud-music-seek-backward")
(">" . "netease-cloud-music-seek-forward")
("k" . "netease-cloud-music-clear-playlist")
("w" . "eaf--netease-cloud-music-write-mode-enter")
("s" . "eaf--netease-cloud-music-switch-playlist")
("f" . "netease-cloud-music-search-song")
("F" . "netease-cloud-music-search-playlist")
("a" . "eaf--netease-cloud-music-add-to-playlist")
("A" . "netease-cloud-music-switch-add-page")
("C" . "netease-cloud-music-create-playlist")
("m" . "netease-cloud-music-change-playlist-name")
("M" . "netease-cloud-music-delete-playlist")
("g" . "eaf--netease-cloud-music-cancel-search")
("l" . "netease-cloud-music-login")
("e" . "netease-cloud-music-get-recommend-songs")
("E" . "netease-cloud-music-get-recommend-playlists")
("j" . "netease-cloud-music-storage-song")
("J" . "netease-cloud-music-add-storage-to-current-playlist")
("o" . "netease-cloud-music-show-storage")
("O" . "netease-cloud-music-delete-song-from-storage")
("K" . "netease-cloud-music-clear-storage")
)
"The keybinding of EAF Netease Cloud Music."
:type 'cons)
(add-to-list 'eaf-app-binding-alist '("netease-cloud-music" . eaf-netease-cloud-music-keybinding))
(setq eaf-netease-cloud-music-module-path (concat (file-name-directory load-file-name) "buffer.py"))
(add-to-list 'eaf-app-module-path-alist '("netease-cloud-music" . eaf-netease-cloud-music-module-path))
(defun eaf--netease-cloud-music-change-playlist (pid)
"Change the current playlist to PID."
(when (featurep 'netease-cloud-music)
(when (stringp pid)
(setq pid (string-to-number pid)))
(let (playlist)
(cond ((= pid 0)
(setq netease-cloud-music-use-local-playlist t
netease-cloud-music-playlists-songs nil
netease-cloud-music-playlist-id nil)
(when netease-cloud-music-playlist-refresh-timer
(cancel-timer netease-cloud-music-playlist-refresh-timer)
(setq netease-cloud-music-playlist-refresh-timer nil))
(setq playlist netease-cloud-music-playlist))
((and netease-cloud-music-playlists
(netease-cloud-music-alist-cdr
pid netease-cloud-music-playlists))
(setq netease-cloud-music-use-local-playlist nil
netease-cloud-music-playlist-id pid
netease-cloud-music-playlists-songs
(netease-cloud-music-get-playlist-songs
netease-cloud-music-playlist-id))
(setq playlist netease-cloud-music-playlists-songs))
(t (na-error "The pid cannot be found!")))
(eaf-call-async "eval_js_function" eaf--buffer-id
"change_playlist_style"
(json-encode
(if netease-cloud-music-use-local-playlist
0
(1+ (cl-position
(netease-cloud-music-alist-cdr
netease-cloud-music-playlist-id
netease-cloud-music-playlists)
netease-cloud-music-playlists)))))
(eaf-call-async "eval_js_function" eaf--buffer-id
"set_playlist" (json-encode-array playlist)))
(when netease-cloud-music-process
(netease-cloud-music-kill-current-song))))
(defun eaf--netease-cloud-music-change-play-status ()
"Change play status."
(let ((icon (pcase netease-cloud-music-process-status
("playing" "pause-circle")
("paused" "play-circle")
(_ "play-circle"))))
(eaf-call-async "eval_js_function" eaf--buffer-id
"set_play_icon_status"
(json-encode icon))))
(defun eaf--netease-cloud-music-init ()
"Init the netease-cloud-music."
(when eaf-netease-cloud-music-user+list
(eaf-call-async "eval_js_function" eaf--buffer-id
"update_user_info" (json-encode eaf-netease-cloud-music-user+list)))
(when netease-cloud-music-playlists
(eaf-call-async "eval_js_function" eaf--buffer-id
"set_user_playlists"
(json-encode-array
(netease-cloud-music--cons-to-list
netease-cloud-music-playlists))))
(eaf-call-async "eval_js_function" eaf--buffer-id
"change_playlist_style"
(json-encode
(list (if netease-cloud-music-use-local-playlist
0
(1+ (cl-position
(netease-cloud-music-alist-cdr
netease-cloud-music-playlist-id
netease-cloud-music-playlists)
netease-cloud-music-playlists)))
t)))
(eaf-call-async "eval_js_function" eaf--buffer-id
"set_repeat_mode"
(json-encode netease-cloud-music-repeat-mode))
(eaf-call-async "eval_js_function" eaf--buffer-id
"set_panel_song_info"
(json-encode
(if netease-cloud-music-process
(list (car netease-cloud-music-current-song)
(nth 1 netease-cloud-music-current-song))
(list "" ""))))
(eaf--netease-cloud-music-change-play-status)
(netease-cloud-music-get-playlist)
(unless (file-exists-p netease-cloud-music-cache-directory)
(make-directory netease-cloud-music-cache-directory))
(when (and (netease-cloud-music--api-downloaded)
(not (netease-cloud-music-api-process-live-p)))
(netease-cloud-music-start-api))
(when (file-exists-p netease-cloud-music-user-loginfo-file)
(let ((loginfo (netease-cloud-music-get-loginfo)))
(when loginfo
(setq netease-cloud-music-phone (car loginfo)
netease-cloud-music-user-password (cdr loginfo)
netease-cloud-music-login-timer
(run-with-timer
1 2
(lambda ()
(if (and netease-cloud-music-user-id
netease-cloud-music-username)
(progn
(cancel-timer netease-cloud-music-login-timer)
(setq netease-cloud-music-login-timer nil))
(netease-cloud-music--get-user-info)
(ignore-errors
(setq netease-cloud-music-playlists
(netease-cloud-music-get-user-playlist
netease-cloud-music-user-id))
(with-current-buffer "eaf-netease-cloud-music"
(eaf-call-async "eval_js_function" eaf--buffer-id
"set_user_playlists"
(json-encode-array
(netease-cloud-music--cons-to-list
netease-cloud-music-playlists))))))))))))
(eaf-call-async "eval_js_function" eaf--buffer-id
"set_playlist"
(json-encode-array
(if netease-cloud-music-use-local-playlist
netease-cloud-music-playlist
netease-cloud-music-playlists-songs)))
(eaf--netease-cloud-music--update-song-style))
(defun eaf--netease-cloud-music--update-song-style ()
"Update song style."
(when (string= "playing" netease-cloud-music-process-status)
(eaf-call-async "eval_js_function" eaf--buffer-id
"change_song_style" netease-cloud-music-playlist-song-index)))
(defun eaf--netease-cloud-music-play-with-index ()
"Read index from Minibuffer."
(interactive)
(let* ((index (read-number "Enter the song's index: "))
(song (nth (1- index)
(if netease-cloud-music-use-local-playlist
netease-cloud-music-playlist
netease-cloud-music-playlists-songs))))
(if (null song)
(user-error "[EAF/Netease-Cloud-Music]: The index is error!")
(netease-cloud-music-play
(car song) (nth 1 song) (nth 3 song)))))
(defun eaf--netease-cloud-music-delete-song-from-playlist ()
"Delete song from playlist read from Minibuffer."
(interactive)
(let ((index (read-number "Enter the song's index: ")))
(netease-cloud-music-delete-song-from-playlist (1- index))))
(defun eaf--netease-cloud-music-move-song (up)
"Move the song read from Minibuffer up or down.
If Up is not non-nil, move the song up.Otherwise move it down."
(let ((index (read-number "Enter the song's index: ")))
(if (string= up "True")
(netease-cloud-music-move-up (1- index))
(netease-cloud-music-move-down (1- index)))))
(defun eaf--netease-cloud-music-move-song-up ()
"Move song up."
(interactive)
(eaf--netease-cloud-music-move-song t))
(defun eaf--netease-cloud-music-move-song-down ()
"Move song down."
(interactive)
(eaf--netease-cloud-music-move-song nil))
(defun eaf--netease-cloud-music-write-mode-enter ()
"Enter the write mode."
(interactive)
(switch-to-buffer (get-buffer-create "eaf-netease-cloud-music-write"))
(netease-cloud-music-write-mode))
(defun eaf--netease-cloud-music-switch-enter (&optional index)
"Add current song or playlist into current playlist."
(interactive)
(cond ((null index)
(setq index (1- (read-number "Enter the item's index: "))))
((stringp index)
(setq index (string-to-number index))))
(if (eq netease-cloud-music-search-type 'song)
(netease-cloud-music-switch-enter index)
(netease-cloud-music-playlist-enter index)))
(defun eaf--netease-cloud-music-switch-playlist ()
"Switch playlist by getting index."
(interactive)
(with-current-buffer "eaf-netease-cloud-music"
(eaf-call-async "eval_js_function" eaf--buffer-id
"set_index_style" (json-encode t))
(let ((index (1- (read-number "Enter the playlist's index: "))))
(eaf--netease-cloud-music-change-playlist
(if (= index 0)
index
(cdr (nth (1- index) netease-cloud-music-playlists)))))
(eaf-call-async "eval_js_function" eaf--buffer-id
"set_index_style" (json-encode nil))))
(defun eaf--netease-cloud-music-add-to-playlist ()
"Add the search songs or playlists to current playlist."
(interactive)
(if (eq netease-cloud-music-search-type 'song)
(netease-cloud-music-switch-add-to-playlist)
(netease-cloud-music-playlist-add-all)))
(defun eaf--netease-cloud-music-cancel-search ()
"Cancel search."
(interactive)
(eaf-call-async "eval_js_function" eaf--buffer-id
"change_playlist_mode"
(json-encode nil))
(eaf-call-async "eval_js_function" eaf--buffer-id
"set_playlist"
(json-encode-array
(if netease-cloud-music-use-local-playlist
netease-cloud-music-playlist
netease-cloud-music-playlists-songs)))
(netease-cloud-music-adjust-song-index))
;;;###autoload
(defun eaf-open-netease-cloud-music ()
"Open EAF netease cloud music."
(interactive)
(if (ignore-errors (or (featurep 'netease-cloud-music)
(load-library "netease-cloud-music")))
(progn
(setq netease-cloud-music-last-buffer (current-buffer))
(if (get-buffer "eaf-netease-cloud-music")
(switch-to-buffer "eaf-netease-cloud-music")
(eaf-open "eaf-netease-cloud-music" "netease-cloud-music")))
(user-error "[EAF/Netease-Cloud-Music]: You haven't install the package netease-cloud-music.")))
(provide 'eaf-netease-cloud-music)
;;; eaf-netease-cloud-music.el ends here