-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
smudge.el
213 lines (185 loc) · 7.89 KB
/
smudge.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
;;; smudge.el --- Control the Spotify app -*- lexical-binding: t; -*-
;; Copyright (C) 2014-2018 Daniel Fernandes Martins
;; Keywords: multimedia, music, spotify, smudge
;; Package: smudge
;; Package-Requires: ((emacs "27.1") (simple-httpd "20230821.1458") (request "0.3") (oauth2 "0.16"))
;; Version: 1.0.0
;; Homepage: https://github.com/danielfm/smudge
;; SPDX-License-Identifier: GPL-3.0-or-later
;;; Commentary:
;; This mode requires at least GNU Emacs 24.4
;; Before using this mode, first go the Spotify Web API console
;; <https://developer.spotify.com/my-applications> and create a new application,
;; adding <http://localhost:8080/smudge-callback> as the redirect URI (or
;; whichever port you have specified via customize).
;; After requiring `smudge', make sure to define the client id and client
;; secrets, along with some other important settings. See README.md for the
;; complete list of settings and usage information.
;;; Code:
(when (version< emacs-version "24.4")
(error "Smudge requires at least GNU Emacs 24.4"))
(require 'subr-x)
(require 'json)
(require 'tabulated-list)
(require 'easymenu)
(require 'smudge-api)
(require 'smudge-track)
(require 'smudge-playlist)
(require 'smudge-device-select)
(require 'smudge-controller)
(require 'smudge-remote)
(smudge-when-darwin (require 'smudge-apple))
(smudge-when-gnu-linux (require 'smudge-dbus))
(require 'smudge-connect)
(defgroup smudge nil
"Smudge Spotify client."
:version "0.0.1"
:group 'multimedia)
;;;###autoload
(defun smudge-track-search (query)
"Search for tracks that match the given QUERY string."
(interactive "sSpotify Search (Tracks): ")
(let ((buffer (get-buffer-create (format "*Track Search: %s*" query))))
(with-current-buffer buffer
(smudge-track-search-mode)
(smudge-track-search-update query 1))))
;;;###autoload
(defun smudge-playlist-search (query)
"Search for playlists that match the given QUERY string."
(interactive "sSpotify Search (Playlists): ")
(let ((buffer (get-buffer-create (format "*Playlist Search: %s*" query))))
(with-current-buffer buffer
(smudge-playlist-search-mode)
(smudge-playlist-search-update query 1))))
;;;###autoload
(defun smudge-recently-played ()
"Display recently played tracks."
(interactive)
(let ((buffer (get-buffer-create "*Recently Played*")))
(with-current-buffer buffer
(smudge-track-search-mode)
(smudge-track-recently-played-tracks-update 1))))
;;;###autoload
(defun smudge-my-playlists ()
"Display the current user's playlists."
(interactive)
(smudge-api-current-user
(lambda (user)
(smudge-user-playlists (smudge-api-get-item-id user)))))
;;;###autoload
(defun smudge-user-playlists (user-id)
"Display the public playlists of the given user with USER-ID."
(interactive "sSpotify User ID: ")
(let ((buffer (get-buffer-create (format "*Playlists: %s*" user-id))))
(with-current-buffer buffer
(smudge-playlist-search-mode)
(smudge-playlist-user-playlists-update user-id 1))))
;;;###autoload
(defun smudge-featured-playlists ()
"Display Spotify's featured playlists."
(interactive)
(let ((buffer (get-buffer-create "*Featured Playlists*")))
(with-current-buffer buffer
(smudge-playlist-search-mode)
(smudge-playlist-featured-playlists-update 1))))
;;;###autoload
(defun smudge-create-playlist (name public)
"Create an empty playlist owned by the current user.
Prompt for the NAME and whether it should be made PUBLIC."
(interactive
(list (read-string "Playlist name: ")
(y-or-n-p "Make the playlist public? ")))
(if (string= name "")
(message "Playlist name not provided; aborting")
(smudge-api-current-user
(lambda (user)
(smudge-api-playlist-create
(smudge-api-get-item-id user)
name
public
(lambda (new-playlist)
(if new-playlist
(message "Playlist '%s' created" (smudge-api-get-item-name new-playlist))
(message "Error creating the playlist"))))))))
;;;###autoload
(defun smudge-select-device ()
"Allow for the selection of a device via Spotify Connect for transport functions."
(interactive)
(smudge-api-current-user
(lambda (user)
(if (not (string= (gethash 'product user) "premium"))
(message "This feature requires a Spotify premium subscription.")
(let ((buffer (get-buffer-create "*Devices*")))
(with-current-buffer buffer
(smudge-device-select-mode)
(smudge-device-select-update)))))))
(defvar smudge-command-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "M-r") #'smudge-controller-toggle-repeat)
(define-key map (kbd "M-s") #'smudge-controller-toggle-shuffle)
(define-key map (kbd "M-p") #'smudge-controller-toggle-play)
(define-key map (kbd "M-b") #'smudge-controller-previous-track)
(define-key map (kbd "M-u") #'smudge-controller-volume-up)
(define-key map (kbd "M-d") #'smudge-controller-volume-down)
(define-key map (kbd "M-f") #'smudge-controller-next-track)
(define-key map (kbd "p m") #'smudge-my-playlists)
(define-key map (kbd "p f") #'smudge-featured-playlists)
(define-key map (kbd "p u") #'smudge-user-playlists)
(define-key map (kbd "p s") #'smudge-playlist-search)
(define-key map (kbd "p c") #'smudge-create-playlist)
(define-key map (kbd "t s") #'smudge-track-search)
(define-key map (kbd "d") #'smudge-select-device)
map)
"Keymap for Spotify commands after \\='smudge-keymap-prefix\\='.")
(fset 'smudge-command-map smudge-command-map)
(defvar smudge-transient-command-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "M-b") #'smudge-controller-previous-track)
(define-key map (kbd "M-f") #'smudge-controller-next-track)
(define-key map (kbd "M-u") #'smudge-controller-volume-up)
(define-key map (kbd "M-d") #'smudge-controller-volume-down)
map)
"Transient keymap for commands that are likely to be repeated.")
(easy-menu-add-item nil '("Tools")
'("Smudge"
["Play/Pause" smudge-controller-toggle-play]
["Previous Track" smudge-controller-previous-track]
["Next Track" smudge-controller-next-track]
"--"
["Select Device" smudge-select-device]
["Mute/Unmute" smudge-controller-volume-mute-unmute]
"--"
["Shuffle" smudge-controller-toggle-shuffle]
["Repeat" smudge-controller-toggle-repeat]
"--"
["Search Tracks..." smudge-track-search]
["Featured Playlists" smudge-featured-playlists]
["My Playlists" smudge-my-playlists]
["User Playlists..." smudge-user-playlists]
["Search Playlists..." smudge-playlist-search]
["Create Playlist..." smudge-create-playlist]
"--"
["Smudge Remote Mode" global-smudge-remote-mode :style toggle :selected global-smudge-remote-mode]))
(defun smudge-remote-popup-menu ()
"Popup menu when in smudge-remote-mode."
(interactive)
(popup-menu
'("Smudge"
["Play/Pause" smudge-controller-toggle-play]
["Previous Track" smudge-controller-previous-track]
["Next Track" smudge-controller-next-track]
"--"
["Select Device" smudge-select-device]
["Mute/Unmute" smudge-controller-volume-mute-unmute]
"--"
["Shuffle" smudge-controller-toggle-shuffle]
["Repeat" smudge-controller-toggle-repeat]
"--"
["Search Tracks..." smudge-track-search]
["Featured Playlists" smudge-featured-playlists]
["My Playlists" smudge-my-playlists]
["User Playlists..." smudge-user-playlists]
["Search Playlists..." smudge-playlist-search]
["Create Playlist..." smudge-create-playlist])))
(provide 'smudge)
;;; smudge.el ends here