-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvotd.el
59 lines (51 loc) · 2.32 KB
/
votd.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
(defvar bsb/vinyl-collection
(with-current-buffer (find-file-noselect (bsb/init-file "records" "dat"))
(goto-char (point-min))
(read (current-buffer))))
(defun bsb/discogs-search (artist album)
(let* ((token (password-store-get "discogs/kingcons"))
(auth-header (format "Discogs token=%s" token))
(url-request-extra-headers `(("Authorization" . ,auth-header)))
(base-url "https://api.discogs.com/")
(base-params "?type=release&format=vinyl")
(params (format "%s&artist=%s&release_title=%s" base-params artist album))
(request-url (format "%s/database/search?%s" base-url params)))
(with-current-buffer
(url-retrieve-synchronously request-url)
(goto-char (point-min))
(let ((data (when (re-search-forward "\r?\n\r?\n" nil t)
(json-read))))
(kill-buffer (current-buffer))
data))))
(defun bsb/show-cover-art (response)
(cl-labels ((cover-art (x) (cdr (assoc 'cover_image x)))
(blank-art? (x) (string-match-p "spacer.gif" (cover-art x))))
(let* ((results (cdr (assoc 'results response)))
(record (car (seq-remove #'blank-art? results))))
(if (null record)
(message "No matching album found!")
(let ((cover-art-url (cover-art record)))
(browse-web cover-art-url)
(message (format "Now Playing: %s" (cdr (assoc 'title record)))))))))
(defun bsb/filter-vinyl (attribute value)
(let ((results (seq-copy bsb/vinyl-collection)))
(cl-flet ((match? (plist) (string= (plist-get plist attribute) value)))
(seq-filter #'match? results))))
(defun bsb/choose-record (attribute value)
(let ((candidates (bsb/filter-vinyl attribute value)))
(bsb/show-random-album candidates)))
(defun bsb/show-random-album (candidates)
(let* ((record (seq-random-elt candidates))
(artist (plist-get record :artist))
(album (plist-get record :album))
(response (bsb/discogs-search artist album)))
(message "Searching for %s - %s" artist album)
(bsb/show-cover-art response)))
(defun bsb/gimme-genre (genre)
(interactive "sWhat genre? ")
(bsb/choose-record :genre genre))
(defun bsb/gimme-techno ()
(interactive)
(bsb/choose-record :genre "Techno"))
(global-set-key (kbd "s-g") 'bsb/gimme-genre)
(global-set-key (kbd "s-n") 'bsb/gimme-techno)