-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify-notification.clj
executable file
·49 lines (41 loc) · 1.7 KB
/
spotify-notification.clj
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
#!/usr/bin/env bb
(ns spotify-notification
(:require
[babashka.curl :as curl]
[cheshire.core :as json]))
(def icons {"start" "/usr/share/icons/gnome/32x32/actions/player_play.png"
"change" "/usr/share/icons/gnome/32x32/actions/player_fwd.png"
"stop" "/usr/share/icons/gnome/32x32/actions/player_stop.png"})
(def notify-send-cmd ["notify-send" "--urgency=low" "--expire-time=7000"])
(defn get-access-token
[client-id secret-id]
(-> (curl/post "https://accounts.spotify.com/api/token" {:basic-auth [client-id secret-id]
:body "grant_type=client_credentials"
:throw false})
:body
(json/parse-string true)
:access_token))
(defn get-track-info
[access-token]
(-> (curl/get (str "https://api.spotify.com/v1/tracks/" (System/getenv "TRACK_ID"))
{:headers {"Accept" "application/json"
"Content-Type" "application/json"
"Authorization" (str "Bearer " access-token)}
:throw false})
:body
(json/parse-string true)))
(defn notify-send
[track]
(when-not (contains? track :error)
(let [event (System/getenv "PLAYER_EVENT")]
(-> (ProcessBuilder. (conj notify-send-cmd (str "--icon=" (get icons event)) "spotifyd" (:name track)))
(.start)
(.waitFor)))))
(defn display-notification
[client-id secret-id]
(some-> (get-access-token client-id secret-id)
(get-track-info)
(notify-send)))
(let [[client-id secret-id] *command-line-args*]
(when (and client-id secret-id)
(display-notification client-id secret-id)))