-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrespot-onevent
205 lines (189 loc) · 6.12 KB
/
librespot-onevent
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
#!/bin/bash
## Configurações
## CACHE_PATH
## BEARER_TOKEN_FILE
## CLIEND_ID
## CLIENT_SECRET
## VOLUME_ICON
source '/home/modolo/.config/librespot/config.env'
## Requisita um novo token
request_access_token() {
local bearer
bearer="$(echo -n "$CLIEND_ID:$CLIENT_SECRET" | base64 -w0 )"
curl --silent -H "Authorization: Basic $bearer" \
-d "grant_type=client_credentials" \
-X "POST" 'https://accounts.spotify.com/api/token'
}
## Retorna as informações do track no formato json
request_track_info() {
local access_token="$1"
local track_id="$2"
local market="$3"
curl --silent -X "GET" "https://api.spotify.com/v1/tracks/$track_id?market=$market" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $access_token"
}
## Persiste o token
save_acess_token() {
local bearer="$1"
echo -n "$bearer" >"$CACHE_PATH/$BEARER_TOKEN_FILE"
}
## Recupera um token salvo
retrieve_access_token() {
cat "$CACHE_PATH/$BEARER_TOKEN_FILE"
}
## Testa de é um track válido
# TODO - Melhorar o teste de erros
is_access_token_expired() {
local track_info="$1"
local expired
expired=$(echo "$track_info" | jq -r '.error')
if [ "$expired" == "null" ]
then
echo "VALID"
else
echo "$track_info" | systemd-cat -t librespot -p info
echo "INVALID"
fi
}
## Obtem um novo token
refresh_token() {
local token_info
token_info=$(request_access_token)
local access_token
access_token=$(echo "$token_info" | jq -r '.access_token')
save_acess_token "$access_token"
echo "$access_token"
}
## Transforma mileseconds em horas, minutos e segundos
format_time() {
local mileseconds=$1
local seconds
seconds=$(( "$mileseconds"/1000 ))
if [ "$seconds" -ge "3600" ]
then
date -d@${seconds} -u +%H:%M:%S
else
date -d@${seconds} -u +%M:%S
fi
}
## Notifica alterações de volume
notify_volume() {
local new_volume=$1
# Gnome e Ubuntu ignoram esse valor
local expire_time=${2:-5000}
local volume
volume=$(echo "scale=4;($new_volume/65535)*100" | bc)
volume=$(printf "%02.2f" "$(echo "$volume" | tr '.' ',')")
local hints
# urgency 'low' é 0
hints="{\"urgency\": <byte 0>, \"transient\": <int32 1>}"
local id
id=$(cat "$XDG_RUNTIME_DIR/librespot.volume.id" 2>/dev/null)
local re='^[0-9]+$'
if [[ ! ${id} =~ $re ]] ; then
id=1
fi
echo "volume_id: ${id}" | systemd-cat -t librespot -p info
echo "volume: ${volume}" | systemd-cat -t librespot -p info
id=$(gdbus call --session --dest org.freedesktop.Notifications \
--object-path /org/freedesktop/Notifications \
--method org.freedesktop.Notifications.Notify \
"LIBRESPOT_VOLUME" "$id" \
"${VOLUME_ICON}" "Spotify" "Volume [ $volume% ]" [] "$hints" "$expire_time" \
| sed -e 's/(uint32 //' -e 's/,)//')
echo "$id" >"$XDG_RUNTIME_DIR/librespot.volume.id"
}
## Notifica as informações da trilha
notify_track() {
local track_info="$1"
# Gnome e Ubuntu ignoram esse valor
local expire_time=${2:-2500}
# shellcheck disable=SC2155
declare -A track=$(echo "$track_info" | jq '.album.name,.album.images[2].url,.name,.duration_ms' \
| tr '\n' '\t' \
| awk -F'\t' '{printf "( [album_name]=%s [album_image_url]=%s [name]=%s [duration_ms]=%s )",$1,$2,$3,$4}')
local album_name=${track[album_name]}
local album_image_url=${track[album_image_url]}
local track_name=${track[name]}
local track_duration
track_duration=$(format_time "${track[duration_ms]}")
# Remove a anterior caso exista
rm -f "${XDG_RUNTIME_DIR}"/librespot.album_image.* 2>/dev/null
local album_image
album_image=$(mktemp "$XDG_RUNTIME_DIR/librespot.album_image.XXXXXXXXXX")
wget -q "${album_image_url}" -O "${album_image}"
echo "Album: ${album_name}" | systemd-cat -t librespot -p info
echo "Image URL: ${album_image_url}" | systemd-cat -t librespot -p info
echo "Image info: $(file -b "${album_image}")" | systemd-cat -t librespot -p info
echo "Track: ${track_name}" | systemd-cat -t librespot -p info
echo "Duration: ${track_duration}" | systemd-cat -t librespot -p info
local hints
# urgency 'low' é 0
hints="{\"urgency\": <byte 0>, \"transient\": <int32 1>}"
local id
id=$(cat "$XDG_RUNTIME_DIR/librespot.track.id" 2>/dev/null)
local re='^[0-9]+$'
if [[ ! ${id} =~ $re ]] ; then
id=1
fi
id=$(gdbus call --session --dest org.freedesktop.Notifications \
--object-path /org/freedesktop/Notifications \
--method org.freedesktop.Notifications.Notify \
"LIBRESPOT_TRACK" "$id" \
"${album_image}" "${album_name}" "<b>${track_name}</b> ${track_duration}" [] "$hints" "$expire_time" \
| sed -e 's/(uint32 //' -e 's/,)//')
echo "$id" >"$XDG_RUNTIME_DIR/librespot.track.id"
}
## Trilha tocando
playing_track() {
local track_id="$1"
local market="BR"
local track_info=""
if [ -f "$CACHE_PATH/$BEARER_TOKEN_FILE" ]
then
local access_token
local track_info
local expired
access_token=$(retrieve_access_token)
track_info=$(request_track_info "$access_token" "$track_id" "$market")
# TODO - Testar os possíveis erros retornados
expired=$(is_access_token_expired "$track_info")
if [ "$expired" == "INVALID" ]
then
echo "Renovando o token" | systemd-cat -t librespot -p info
access_token=$(refresh_token)
track_info=$(request_track_info "$access_token" "$track_id" "$market")
fi
else
echo "Criando o arquivo de token em $CACHE_PATH/$BEARER_TOKEN_FILE" | systemd-cat -t librespot -p info
access_token=$(refresh_token)
track_info=$(request_track_info "$access_token" "$track_id" "$market")
fi
echo "$track_info"
}
## Testa os vários eventos
case "$PLAYER_EVENT" in
"playing")
# shellcheck disable=SC2153
track_info=$(playing_track "$TRACK_ID")
notify_track "$track_info" 5000
exit 0
;;
"volume_set")
if [[ -f "${CACHE_PATH}/started" || -f "${CACHE_PATH}/reloaded" ]]
then
rm -f "${CACHE_PATH}/started" &>/dev/null
rm -f "${CACHE_PATH}/reloaded" &>/dev/null
exit 0
fi
re='^[0-9]+$'
# shellcheck disable=SC2153
if [[ ${VOLUME} =~ $re ]] ; then
# shellcheck disable=SC2153
notify_volume "$VOLUME" 3000
fi
exit 0
;;
esac