From 35c8a990dea3d81662aaaffdb25bd3bae4a2d0ab Mon Sep 17 00:00:00 2001 From: jao Date: Sat, 9 Jan 2021 03:45:30 +0000 Subject: espotify: tweaks --- media/espotify.org | 72 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 50 insertions(+), 22 deletions(-) (limited to 'media/espotify.org') diff --git a/media/espotify.org b/media/espotify.org index fb50dba..eb41a9d 100644 --- a/media/espotify.org +++ b/media/espotify.org @@ -1,5 +1,5 @@ #+title: consulting spotify -#+date: <2021-01-08> +#+date: <2021-01-08 04:02> #+filetags: emacs #+PROPERTY: header-args :tangle yes :comments no :results silent @@ -11,6 +11,10 @@ track or album identifier that we can send then to the latter to play, with emacs completion mechanisms (consult and friends in this case) providing the glue between both worlds. +Note: you can access this post as an ~org~ file in my [[https://codeberg.org/jao/elibs/src/branch/main/media/espotify.org][jao/elibs +repository]], and transform it to an emacs-lisp file with ~M-x +org-babel-tangle~. + Let's start with an umbrella customization group: #+begin_src emacs-lisp ;;; espotify.el - spotify search and play - -*- lexical-binding: t; -*- @@ -96,7 +100,7 @@ Let's start with an umbrella customization group: (when (null types) (error "Must supply a non-empty list of types to search for")) (let ((term (url-encode-url term))) - (format "%s/search?q=%s&type=%s" + (format "%s/search?q=%s&type=%s&limit=50" espotify-spotify-api-url (if filter (format "%s:%s" filter term) term) (mapconcat #'symbol-name types ",")))) @@ -104,12 +108,12 @@ Let's start with an umbrella customization group: For instance: - #+begin_src emacs-lisp :load no :tangle no + #+begin_src emacs-lisp :load no :tangle no :results replace (espotify--make-search-url "dream blue turtles" '(album)) #+end_src #+RESULTS: - : https://api.spotify.com/v1/search?q=dream%20blue%20turtles&type=album + : https://api.spotify.com/v1/search?q=dream%20blue%20turtles&type=album&limit=50 If we have an [[*Access to Spotify's API: authentication][authorisation token]] and a search URL in our hands, we can use them as in the following helper function, which will @@ -148,10 +152,7 @@ Let's start with an umbrella customization group: (sit-for 0) #+end_src - #+RESULTS: - : t - - #+begin_src emacs-lisp :load no :tangle no + #+begin_src emacs-lisp :load no :tangle no :results replace (mapcar 'car espotify-query-result) #+end_src @@ -251,6 +252,7 @@ Let's start with an umbrella customization group: (espotify--search-generator type filter) :lookup 'espotify--consult-lookup :category 'spotify-query-result + :initial "#" :require-match t)) #+end_src @@ -299,15 +301,13 @@ Let's start with an umbrella customization group: #+begin_src emacs-lisp (defun espotify--format-item (x) - (propertize (format "%s (%s%s%s)" - (alist-get 'name x) - (or (alist-get 'name (car (alist-get 'artists x))) "") - (if-let (d (alist-get 'total_tracks x)) - (format ", %s tracks" d) "") - (if-let (d (alist-get 'release_date x)) - (format ", %s" d) - "")) - 'espotify-uri (alist-get 'uri x))) + (propertize (format "%s" (alist-get 'name x)) 'espotify-item x)) + + (defun espotify--item (cand) + (get-text-property 0 'espotify-item cand)) + + (defun espotify--uri (cand) + (alist-get 'uri (espotify--item cand))) #+end_src and then we make sure that we access that original string when @@ -326,7 +326,7 @@ Let's start with an umbrella customization group: #+begin_src emacs-lisp (defun espotify--maybe-play (x) - (when-let (uri (get-text-property 0 'espotify-uri x)) + (when-let (uri (espotify--uri x)) (espotify-play-uri uri))) #+end_src @@ -339,12 +339,40 @@ Let's start with an umbrella customization group: (espotify--maybe-play (consult-spotify-by 'album filter))) #+end_src +* Adding metadata to candidates using Marginalia + + Let's add metadata fields to our candidates, so that packages like + [[https://github.com/minad/marginalia][Marginalia]] can offer it to consult or selectrum. + + #+begin_src emacs-lisp + (defun espotify-marginalia-annotate (cand) + (when-let (x (espotify--item (cand))) + (marginalia--fields + ((alist-get 'type x "") :face marginalia-mode :width 10) + ((or (alist-get 'name (car (alist-get 'artists x))) "") + :face 'marginalia-file-name :width 50) + ((if-let (d (alist-get 'total_tracks x)) (format "%s tracks" d) "") + :face 'marginalia-size :width 12) + ((if-let (d (alist-get 'release_date x)) (format "%s" d) "") + :face 'maginalia-modified :width 10)))) + + (add-to-list 'marginalia-annotators-heavy + (cons 'spotify-query-result 'espotify-marginalia-annotate)) + #+end_src + * Exercises for the reader - - Defining new interactive commands for other types and queries, - as well as standard filters. - - Pagination when we receive more than the standard 20 results. - - Metadata set for Marginalia. + Defining new interactive commands for other types and queries, + as well as standard filters shouldn't be too complicated now that we + have the above tools at our disposal. + + For instance: + #+begin_src emacs-lisp + (defun consult-spotify-playlist (&optional filter) + (interactive) + (espotify--maybe-play (consult-spotify-by 'playlist filter))) + #+end_src + * Footnotes -- cgit v1.2.3