Skip to content

Commit

Permalink
Add functionality to add songs to the queue (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
27justin authored Dec 5, 2023
1 parent 2513965 commit 04df86f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ key bindings:
| <kbd>a</kbd> | Adds track to a playlist |
| <kbd>l</kbd> | Loads the next page of results (pagination) |
| <kbd>g</kbd> | Clears the results and reloads the first page of results |
| <kbd>k</kbd> | Adds track to the queue |
| <kbd>M-RET</kbd> | Plays the track under the cursor in the context of its album [1] |

[1] D-Bus implementation for GNU/Linux do not support passing the context, so
Expand Down Expand Up @@ -285,6 +286,7 @@ bindings in the resulting buffer:
| <kbd>g</kbd> | Clears the results and reloads the first page of results |
| <kbd>f</kbd> | Follows the current playlist |
| <kbd>u</kbd> | Unfollows the current playlist |
| <kbd>k</kbd> | Adds track to the queue |
| <kbd>M-RET</kbd> | Plays the track under the cursor in the context of the playlist [1] |

Both buffers load the `global-smudge-remote-mode` by default.
Expand Down
26 changes: 26 additions & 0 deletions smudge-api.el
Original file line number Diff line number Diff line change
Expand Up @@ -596,5 +596,31 @@ Call CALLBACK if provided."
nil
callback))


(defun smudge-api-queue-add-track (track-id &optional callback)
"Add given TRACK-ID to the queue"
(smudge-api-call-async
"POST"
(concat "/me/player/queue?"
(url-build-query-string `((uri ,track-id))
nil t))
nil
callback)
)
(defun smudge-api-queue-add-tracks (track-ids &optional callback)
"Add given TRACK-IDs to the queue"
;; Spotify's API doesn't provide a endpoint that would enable us to
;; add multiple tracks to the queue at the same time.
;; Thus we have to synchronously add the tracks
;; one by one to the queue.
(if (car track-ids)
(smudge-api-queue-add-track (car track-ids)
(lambda (response)
(smudge-api-queue-add-tracks (cdr track-ids)
nil)))
(funcall callback)
)
)

(provide 'smudge-api)
;;; smudge-api.el ends here
14 changes: 14 additions & 0 deletions smudge-track.el
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
(define-key map (kbd "g") #'smudge-track-reload)
(define-key map (kbd "f") #'smudge-track-playlist-follow)
(define-key map (kbd "u") #'smudge-track-playlist-unfollow)
(define-key map (kbd "k") #'smudge-track-add-to-queue)
map)
"Local keymap for `smudge-track-search-mode' buffers.")

Expand Down Expand Up @@ -284,5 +285,18 @@ Default to sortin tracks by number when listing the tracks from an album."
(lambda (_)
(message "Song added.")))))))))

(defun smudge-track-add-to-queue ()
"Add the track under the cursor to the queue."
(interactive)
(let ((selected-track (tabulated-list-get-id)))
(let ((track-id (smudge-api-get-item-uri selected-track)))
(smudge-api-queue-add-track
track-id
(lambda(_)
(message (format "Added \"%s\" to your queue." (smudge-api-get-item-name selected-track)))))
))
)


(provide 'smudge-track)
;;; smudge-track.el ends here

0 comments on commit 04df86f

Please sign in to comment.