This repository has been archived by the owner on May 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotbash
executable file
·358 lines (356 loc) · 14.8 KB
/
spotbash
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/bin/sh
# First run
[ -d "${XDG_CACHE_HOME:-$HOME/.cache/}/spotbash/" ] || mkdir -p "${XDG_CACHE_HOME:-$HOME/.cache/}/spotbash/"
##
has() {
case "$(command -v $1 2>/dev/null)" in
alias*|"") return 1
esac
}
cat() {
[ "$1" ] && {
while read -r p || [ "$p" ]; do
printf '%s\n' "$p"
done < $1
} || {
while read -r p || [ -n $p ]; do
printf '%s\n' "$p"
done
}
}
deps() {
unset IFS
deps="jq curl"; missing=0 # Make sure deps can be found
for i in $deps; do
if ! has $i; then
echo "$i IS MISSING!"; : $((missing+=1))
fi
done; [ $missing -gt "0" ] && exit 1
# Check for ssl
curl -V | grep tls -i -q || {
curl -V | grep ssl -i -q || {
echo "!!SSL/TLS SUPPORT IS MISSING FROM CURL!!"
echo "!!SPOTBASH REQUIRES SSL SUPPORT!!"
exit 1
}
}
# old missing=no was literally useless ...
}
clean_up(){
clean="search uri info"
for i in $clean; do
[ -f "${XDG_CACHE_HOME:-$HOME/.cache/}/spotbash/$i" ] && rm "${XDG_CACHE_HOME:-$HOME/.cache/}/spotbash/$i" 2>/dev/null
done
}
auth() {
IFS=""; # eval $(cat) # eval STDIN - allows for authkeys to be passed via STDIN
# above needs a way to check if reading from STDIN
[ "$REKEY" -a "$CLID" -a "$CLSEC" ] || for i in ./authkeys "${0%/*}/authkeys" "${XDG_CACHE_HOME:-$HOME/.cache/}/spotbash/authkeys"; do # only run loop all variables for AUTHKEY are unset
[ -e $i -a ! "$REKEY" ] && . ${i}
done # much cleaner then old method
AUTHKEY=$(curl -X POST -d grant_type=refresh_token -d refresh_token="$REKEY" -d client_id="$CLID" -d client_secret="$CLSEC" https://accounts.spotify.com/api/token -s | jq .access_token -r)
export AUTHKEY
}
get_info(){
curl -s -X "GET" "https://api.spotify.com/v1/me/player" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer $AUTHKEY" > "${XDG_CACHE_HOME:-$HOME/.cache/}/spotbash/info"
#cat "${XDG_CACHE_HOME:-$HOME/.cache/}/spotbash/info"
export infoF="${XDG_CACHE_HOME:-$HOME/.cache/}/spotbash/info"
}
track_info(){
curl -s -X "GET" "https://api.spotify.com/v1/tracks/${2:-$1}" -H "Authorization: Bearer $AUTHKEY" -H "Accept: application/json" -H "Content-Type: application/json"
}
id() {
# the following is an unedited snippit from makefile - thanks :)
id=$(curl -X "GET" "https://api.spotify.com/v1/me/player/devices" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer $AUTHKEY" -s | jq '[.devices[] | select(.is_active) | .id][0]' -r)
}
name(){
IFS=""
artemp=$(mktemp)
artistC=$(jq .item.name,.item.album.artists[].name < "$infoF" | sed -E -e 's/"//g' -e '0,/^/{s/^/Playing: /}' | tr '\n' '~' | sed -E -e 's/~/\nBy: /' | awk -F"~" '/~/ {print NF-1}')
artist=$(jq .item.name,.item.album.artists[].name < "$infoF" | sed -E -e 's/"//g' -e '0,/^/{s/^/Playing: /}' | tr '\n' '~' | sed -E 's/~/\nBy: /' )
if [ "$artistC" != 1 ]; then
echo $artist | sed -E -e 's/~/\, /' -e 's/~/\n/' | tr '\n' '~' | sed -E 's/~~/\n/' | tr '~' '\n' > $artemp
else
echo $artist | sed -E 's/~/\n/' | tr '\n' '~' | sed -E 's/~~/\n/' | tr '~' '\n' > $artemp
fi
tac $artemp | grep $'\n*' -m 1 | grep ',' -q || {
cat $artemp | tr '\n' '~' | sed -E -E -e 's/(.*)~/\1SPOTEXLINE/' -e 's/(.*)~/\1, /' -e 's/SPOTEXLINE/~/' | tr '~' '\n' | cat > $artemp
# tr often breaks when being redirected
# FINAL FIX - Replace ',' with a newline when only 1 line exists
[ ! "$(wc -l < $artemp)" -gt 1 ] && {
sed -E 's/,/\n/' -i $artemp
sed -E 's/ By:/By:/' -i $artemp
}
}
cat $artemp ; rm $artemp -rf
} # TODO - FIXME # using mktemp is bad practice
get_volume(){
jq .device.volume_percent < "$infoF"
}
pause(){
curl -X PUT -s -d device_id="$id" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer $AUTHKEY" https://api.spotify.com/v1/me/player/pause
}
resume(){
curl -s --fail -X PUT --data "{}" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer $AUTHKEY" https://api.spotify.com/v1/me/player/play
}
next(){
curl -X "POST" -s -d device_id="$id" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer $AUTHKEY" https://api.spotify.com/v1/me/player/next
}
previous(){
curl -X "POST" -s -d device_id="$id" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer $AUTHKEY" https://api.spotify.com/v1/me/player/previous
}
device(){
[ -n "$id" ] && echo "Device: ${id}" || echo "No device..."
}
devices(){
IFS=""
curl -X "GET" "https://api.spotify.com/v1/me/player/devices" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer $AUTHKEY" -s | jq '.devices[] | .name,.id' -r
}
set_volume(){
curl -s -X PUT -d device_id="$id" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer $AUTHKEY" "https://api.spotify.com/v1/me/player/volume?volume_percent=$2"
}
search_track(){ # only searching tracks is currently supported
IFS=""; set -f # globbing is bad here
[ -f "${XDG_CACHE_HOME:-$HOME/.cache/}/spotbash/search" ] && . "${XDG_CACHE_HOME:-$HOME/.cache/}/spotbash/search" || search="$2"
{ printf '%s' "${search}" | grep -q ' '; } && export search="$(printf '%s' "${search}" | sed -E 's/[[:space:]]/%20/g')" # grep '[[:space:]]' may fail here
curl -sX "GET" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer $AUTHKEY" "https://api.spotify.com/v1/search?q=$search&limit=9&type=track" | jq '.tracks.items,del(.[]) | .[] | .name+" By: "+.artists[].name+" @ "+.uri' -r -M
set +f
}
play_track(){
[ -f "${XDG_CACHE_HOME:-$HOME/.cache/}/spotbash/uri" ] && . "${XDG_CACHE_HOME:-$HOME/.cache/}/spotbash/uri" || uri="$2"
curl -s -X PUT "https://api.spotify.com/v1/me/player/play?device_${id}" --data "{\"uris\":[\"$uri\"]}" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer $AUTHKEY"
}
state() {
printf 'Playing: %s\n' $(jq .is_playing -r < "$infoF")
}
loop(){
curl -X "PUT" -d device_id="$id" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer $AUTHKEY" "https://api.spotify.com/v1/me/player/repeat?state=track"
}
repeat(){
curl -X "PUT" -d device_id="$id" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer $AUTHKEY" "https://api.spotify.com/v1/me/player/repeat?state=context"
}
repeat_off(){
curl -X "PUT" -d device_id="$id" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer $AUTHKEY" "https://api.spotify.com/v1/me/player/repeat?state=off"
}
repeat_state(){
printf 'Loop State: %s\n' $(jq -r .repeat_state < "$infoF")
}
uri_handle(){ # handles spotify uris by ripping there id's
IFS=":"; set -- "${@}"; export uri-id="$3"; export uri-type="$2"
}
playlist(){
[ "$2" ] && {
case "$2" in
list|ls|show) [ "$3" ] && { pl - "$3";} || playlists ;;
add) [ "$3" ] && { pa - "$3" "$4";} || [ ] ;; # [ ] returns 1; which would cause || { to be called
# rip) [ "$3" ] && { prip - "$3" "$4";} || [ ] ;; # WIP
esac
} || {
printf 'playlist: playlist subcommands
---
[list|ls] - list playlists || uses old playlists()
[show|list|ls] - lists all songs in $2 || requires $2
[add] - add track to $2
'
# [rip] - runs download() for all items in playlist $2 || WIP
# [del] - remove $3 from $2
# [del] - delete playlist $2 || if $3 is unset # add double check
# [create|make] - create new playlist with name $2
# TODO ^
}
}
pl() { # playlist loop || show
len="$(length - $2)"
n=0; while [ $n -le $len ]; do
curl -sL -X "GET" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer $AUTHKEY" "https://api.spotify.com/v1/playlists/$2/tracks?fields=items(track(name%2Curi))&limit=100&offset=${n}" | jq '.[] | .[].track | .name + " - " + .uri + " "' -r | sed '/ - /d' # + " " is for sed; if 2 spaces after - then uri was missing; so delete
: $((n+=100))
done
# rip will use this in the future
}
pa() { # add func
IFS=", "; for i in ${3}; do
printf 'Adding %s to %s\n' "$i" "$2"
curl -sL -X "POST" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer $AUTHKEY" "https://api.spotify.com/v1/playlists/$2/tracks?uris=${i}" | jq -M || printf 'failed to add %s\n' "$i"
done
}
#prip(){ # download() would not work for this; WIP
# slist=$(pl - $2); pid="$2"; skipuri="$3"; for i in "$slist"; do
# IFS="-"; set -- "$i"; unset IFS # uri is now $2 with name as $1
# n="$(printf '%s' ${1} | sed -e 's/ /_/g' -e 's/,//g' -e 's,/,,g' -e 's/(.*)//g')"
# uri_handle - "$2"; IFS=" "; for s in ${skipuri}; do
# [ ! "$s" = "$uri-id" ] && {
# "$uri-id" "$n.${format:-${3:-mp3}}"
# :;} || { echo "$s was set in skipuri... skipping"; } # this allows skipping songs that may not be found as the first match
# done
# done
#}
playlists(){
echo 'Playlists:' && curl -s -X GET "https://api.spotify.com/v1/me/playlists" -H 'Accept: application/json' -H 'Content-Type: application/json' -H "Authorization: Bearer $AUTHKEY" | jq '.items[] | .name,.uri' -r
}
play(){
curl -X "PUT" "https://api.spotify.com/v1/me/player/play?device_${id}" --data "{\"context_uri\":\$2\"}" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer $AUTHKEY"
}
trans(){
[ ! -z $2 ] && {
export TRANS_ID=${2}
} || {
export TRANS_ID=$id
}
curl -X "PUT" "https://api.spotify.com/v1/me/player" --data "{\"device_ids\":[\"${TRANS_ID}\"]}" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer $AUTHKEY"
}
shuffle_state(){
printf 'Shuffle State: %\n' $(jq .shuffle_state < "$infoF")
}
shuffle() {
shvalue_pre=$(jq .shuffle_state < "$infoF") # Should add a system to grab values - Readability at the cost of speed
case $shvalue_pre in
true)
shvalue="false";;
false)
shvalue="true";;
*)
[ ${shvalue} ] && {
curl -X "PUT" "https://api.spotify.com/v1/me/player/shuffle?state=${shvalue}" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer $AUTHKEY"
} || {
[ ${2} ] && {
curl -X "PUT" "https://api.spotify.com/v1/me/player/shuffle?state=${2}" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer $AUTHKEY"
} || {
echo "Could not determine the current shuffle state! :("
echo "Try again later or use 'arg2' (See help)"; exit 1
}
}
;;
esac
}
# Songs func would go here - Spot(y)RIP is overkill, prob will add a rip function
download() {
has youtube-dl && {
n="${3:-$(name | sed -E -e 's/.*: //g' -e 's/\,.*//' | tr '\n' ' ')}"
youtube-dl ytsearch1:"$n" $([ "$1" = "download" ] && echo $@ | cut -d' ' -f2-)
} || {
echo "youtube-dl is missing!"; exit 1
}
}
status() {
name
}
playing() {
name
}
skip(){
next
}
pre(){
previous
}
prev(){
pre
}
length() {
curl -X "GET" "https://api.spotify.com/v1/playlists/$2/tracks?market=US" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer $AUTHKEY" -s | jq .total -Mr
}
grab() {
has eyeD3 && {
[ -f "$2" ] && {
Tname=$(eyeD3 "$2" | grep "title" | cut -d':' -f2); Tname=${Tname#' '}
Tart=$(eyeD3 "$2" | grep "artist" | cut -d':' -f2); Tart=${Tart#' '}
echo "$Tname $Tart"
Tid=$(search_track - "$Tname $Tart" | head -1 | sed -E 's/.*://')
Aurl=$(track_info "$Tid" | jq '.album.images[1].url' -r)
[ "$Aurl" != "null" ] && { curl -L# "$Aurl" --output "$2.jpg"; } || {
echo "$2 Could not be found..."; exit 1
}
eyeD3 --add-image "$2.jpg:FRONT_COVER" "$2"
rm "$2.jpg"
} || { echo "Your file is missing..."; exit 1 ; }
} || { echo "eyeD3 is required for (a)grab()..."; exit 1; }
}
####################################################################################
####################################################################################
help() {
while read -r p; do
printf '%s\n' "$p"
done << 'EOF'
***
Spotbash: Spotify Control API/Client ~ Written in POSIX sh ~ By Mia
***
[auth] - Print '$AUTHKEY' and exit
[device] - Print first device ID and exit
[devices] - List ALL devices and exit
[search_play] - Search for $2 and play | $2 must be a track
[search_track] - Search for $2 and output URI & Artist & name | $2is interpreted as a track
[play_track] - Play Track URI
[get_volume] - Output player volume and exit
[set_volume] - Set player volume to $2: and exit
[get_info] - Get debugging info about currently playing device
[pre | prev | previous] - Change playback to previous song
[skip | next] - Change playback to next song
[pause] - Pause playback
[resume] - Resume playback
[loop] - Loop track
[repeat] - Repeat current album/playlist
[state] - Output playback status and exit
[repeat_off] - Disable loop/repeat
[repeat_state] - Output playback repeat status and exit
[play] - Play $2 | $2 must be Album or Playlist URI
[playing] - Print Song name followed by artist and exit
[left] - Print time left in MS (exact) and exit
[minleft] - Print time left in Mins (approximate) and exit
[shuffle] - Enable/Disable Shuffle | Set to $2 if specified
[shuffle_state] - Print state of shuffle and exit
[download] - Download currently playing song using ytdl | $@ are ytdl flags
[grab|agrab] - Grab the album art for a file by searching for it using mp4 mediainfo and embed | $2 must be a file
[track_info] - Return info for $2 | $2 must be a track ID
--- Parents/Profiles
[playlist] - playlist subcommands/children | see playlist()
EOF
} # should be noted that subcommands can be called without their parent
# IE: playlists <>; pa <> <> # would work perfectly fine
####################################################################################
####################################################################################
clean_up
deps && {
auth && get_info && id
} || {
exit 1
}
case $1 in
help)
help; exit 1
;;
auth)
echo "$AUTHKEY" && clean_up; exit 0
;;
get_info)
cat "$infoF"; exit 0
;;
search_play)
auth
export id
printf "search='%b'" "${2}" > "${XDG_CACHE_HOME:-$HOME/.cache/}/spotbash/search"
sed -E -i -e 's/[[:space:]]/%20/g' "${XDG_CACHE_HOME:-$HOME/.cache/}/spotbash/search"
uri=$(search_track | cut -d'@' -f2- | cut -d$'\n' -f1 | sed -E 's/[[:space:]]//g')
printf "uri='%b'" "${uri}" > "${XDG_CACHE_HOME:-$HOME/.cache/}/spotbash/uri"
play_track "$@"
clean_up
exit 0
;;
agrab)
grab
;;
"")
help && clean_up; exit 1
;;
*)
type ${1} 2>&1 1>/dev/null && {
${1} "$@" || {
echo "Whoops! Something went wrong..."
} && {
clean_up; exit 0
}
} || {
help; clean_up; exit 1
}
clean_up
;;
esac