-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotify.sh
executable file
·220 lines (161 loc) · 4.84 KB
/
spotify.sh
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
#! /bin/bash
#TODO: use jq for json parsing?
function req {
RET=$(request -X "$@" -H "Authorization: Bearer $TOKEN" );
# if token is expired -> refresh the access token
#RET='{ "error": { "status": 401, "message": "The access token expired" } }';
if [[ "$RET" =~ (The access token expired) ]]; then
#echo "> access token expired -> get new key!";
refresh_token;
#query again with refresehed token
RET=$(request -X "$@" -H "Authorization: Bearer $TOKEN" );
fi
echo "$RET";
}
function request {
curl -s "$@";
}
function get_accesscode {
local SCOPES="user-read-private%20user-modify-playback-state%20user-read-playback-state%20user-read-currently-playing%20playlist-read-private%20playlist-read-collaborative";
echo "open in your browser":
echo "https://accounts.spotify.com/authorize?client_id=${CLIENTID}&response_type=code&redirect_uri=${REDIRECTURL}&scope=$SCOPES&state=optionalrandomstuff";
}
function token_for_code {
if [[ "$1" =~ code=[a-zA-Z0-9_\-]* ]]; then
CODE="${BASH_REMATCH[0]:5}"
else
echo "no access granted or wrong url?";
exit;
fi
RES=$(request -H "Authorization: Basic ${B64}" -d grant_type=authorization_code -d code=$CODE -d redirect_uri=$REDIRECTURL https://accounts.spotify.com/api/token);
#echo $RES;
ERR=0;
if [[ "$RES" =~ \"access_token\":\"[a-zA-Z0-9_\-]* ]]; then
TOKEN="${BASH_REMATCH[0]:16}";
#echo "$TOKEN";
else
ERR=1;
fi
if [[ "$RES" =~ \"refresh_token\":\"[a-zA-Z0-9_\-]* ]]; then
local RFT="${BASH_REMATCH[0]:17}";
#echo "$RFT";
else
ERR=1;
fi
if [ "$ERR" = 0 ]; then
save_auth "$TOKEN" "$RFT";
else
echo "Error:";
echo "$RES";
fi
}
function refresh_token {
RES=$(request -H "Authorization: Basic $B64" -d grant_type=refresh_token -d "refresh_token=$REFRESH_TOKEN" https://accounts.spotify.com/api/token);
# echo "$RES";
if [[ "$RES" =~ \"access_token\":\"[a-zA-Z0-9_\-]* ]]; then
TOKEN="${BASH_REMATCH[0]:16}";
save_auth "$TOKEN" "$REFRESH_TOKEN";
else
echo "Error getting a new accesstoken:";
echo "$RES";
exit;
fi
}
function save_auth {
#TODO: less save calls/ no rewrite of the first lines
echo "CLIENTID=\"$CLIENTID\"" > .spotifyconfig;
echo "SECRET=\"$SECRET\"" >> .spotifyconfig;
echo "REDIRECTURL=\"$REDIRECTURL\"" >> .spotifyconfig;
echo "TOKEN=\"$1\"" >> .spotifyconfig;
echo "REFRESH_TOKEN=\"$2\"" >> .spotifyconfig;
echo "" >> .spotifyconfig;
echo "auth saved";
}
function list_devices {
RES=$(req GET https://api.spotify.com/v1/me/player/devices -d "");
echo "$RES";
}
function activate_device {
ID="$1";
RES=$(req PUT "https://api.spotify.com/v1/me/player" -H "Content-Type: application/json" -d "{\"device_ids\":[\"${ID}\"]}" -d play=true );
echo "$RES";
}
function volume {
PERCENT="$1";
RES=$(req PUT "https://api.spotify.com/v1/me/player/volume?volume_percent=$PERCENT" -d "");
echo "$RES";
}
function play {
if [ "$1" = "" ]; then
#play current song
RES=$(req PUT "https://api.spotify.com/v1/me/player/play" -d "");
else
#play a track
RES=$(req PUT "https://api.spotify.com/v1/me/player/play" -H "Accept: application/json" -H "Content-Type: application/json" -d "{\"uris\": [\"$1\"]}" );
fi
echo "$RES";
}
function play_list {
#play an allbum/playlist whatever is given
RES=$(req PUT "https://api.spotify.com/v1/me/player/play" -H "Accept: application/json" -H "Content-Type: application/json" -d "{\"context_uri\": \"$1\"}" );
echo "$RES";
}
function pause {
RES=$(req PUT "https://api.spotify.com/v1/me/player/pause" -d "" )
echo "$RES";
}
function previous {
RES=$(req POST "https://api.spotify.com/v1/me/player/previous" -d "");
echo "$RES";
}
function next {
RES=$(req POST "https://api.spotify.com/v1/me/player/next" -d "");
echo "$RES";
}
function my_playlists {
RES=$(req GET "https://api.spotify.com/v1/me/playlists");
if [[ "$RES" =~ \"href\"\ :\ \"[^\"]* ]]; then
URL="${BASH_REMATCH[0]:10}";
echo "URL: $URL";
#get first entries
local RET=$(req GET "$URL");
# get more playlist entries
while [ "$URL" != "null" ]; do
if [[ "$RET" =~ \"next\"\ :\ (\"[^\"]*|null) ]]; then
URL="${BASH_REMATCH[0]:10}"
local RET=$(req GET "$URL");
echo "$RET";
# TODO: filter uri and name
else
URL="null"; #break
fi
done;
fi
# echo "$RES";
}
function load_credentials {
# auth data exists?
if [ ! -e .spotifyconfig ]; then
echo "insert CLIENTID and CLIENTSECRET from your spotify developer account into .spotifyconfig";
# TODO: ui for inserting
save_auth;
exit 0;
fi
#load credentials
. .spotifyconfig
if [ "$CLIENTID" = "" ]; then
echo "insert ClientId and ClientSecret into the config file \".spotifyconfig\" .";
exit;
fi
B64=$(echo -n "$CLIENTID:$SECRET" | openssl base64 -A);
if [ "$TOKEN" = "" ]; then
get_accesscode;
echo "copy redirected url from browser";
read FORWARDEDURL;
token_for_code "$FORWARDEDURL";
fi
}
function logout {
save_auth "" "";
exit;
}