Skip to content

Instantly share code, notes, and snippets.

@yanyaoer
Created July 30, 2020 06:57
Show Gist options
  • Save yanyaoer/a7c90f8084358c48862c5d63b34f3c08 to your computer and use it in GitHub Desktop.
Save yanyaoer/a7c90f8084358c48862c5d63b34f3c08 to your computer and use it in GitHub Desktop.
spotify cli with web api
#! /bin/bash
client_id=
client_secret=
port=8888
redirect_uri=http%3A%2F%2Flocalhost%3A$port%2Fcallback
auth_endpoint=https://accounts.spotify.com/authorize/?response_type=code\&client_id=$client_id\&redirect_uri=$redirect_uri
tmp_code=/tmp/.sp_code
# token=$(curl -s -X "POST" -u $client_id:$client_secret -d grant_type=client_credentials https://accounts.spotify.com/api/token | jq .access_token -r)
# curl -v -s -X GET "https://api.spotify.com/v1/me/playlists" -H "Authorization:\"Bearer $token\""
shopt -s expand_aliases
function require {
hash $1 2>/dev/null || {
echo >&2 "Error: '$1' is required, but was not found."; exit 1;
}
}
require curl
require jq
scopes="playlist-read-private user-read-playback-state user-modify-playback-state"
if [[ ! -z $scopes ]]; then
encoded_scopes=$(echo $scopes| tr ' ' '%' | sed s/%/%20/g)
auth_endpoint=$auth_endpoint\&scope=$encoded_scopes
fi
function sp-code {
if [[ -z $code ]]; then
open $auth_endpoint
response=$(echo "HTTP/1.1 200 OK\n\n<html><script>open(location, '_self').close();</script></html>\n" | nc -l -c $port)
code=$(echo "$response" | grep GET | cut -d' ' -f 2 | cut -d'=' -f 2)
fi
echo $code
}
function sp-token-data {
echo $(curl -s -X POST -u $client_id:$client_secret -d $@ https://accounts.spotify.com/api/token)
}
function sp-token {
refresh_token=$(jq -r .refresh_token $tmp_code)
# echo $( -f $tmp_code && -z $refresh_token && echo '123' )
if [[ -f $tmp_code && -z $refresh_token ]]; then
# sp-refresh
echo freshing
response=$(sp-token-data "grant_type=refresh_token&refresh_token=$refresh_token")
else
open $auth_endpoint
# echo $auth_endpoint
# echo '=========='
read -p "auth_code: " auth_code
response=$(sp-token-data "grant_type=authorization_code&code=$auth_code&redirect_uri=$redirect_uri")
fi
echo $response
echo $response > $tmp_code
echo $response | jq .access_token -r
}
# token=$(sp-token)
# echo $token
function curl-token {
# sp-token
token=$(jq -r .access_token $tmp_code)
echo curl -H "Authorization:\"Bearer $token\"" $@
}
function sp-device {
token=$(jq -r .access_token $tmp_code)
curl -H "Authorization:\"Bearer $token\"" -X GET "https://api.spotify.com/v1/me/player/devices"
}
function sp-pause {
token=$(jq -r .access_token $tmp_code)
curl -H "Authorization:\"Bearer $token\"" -X PUT "https://api.spotify.com/v1/me/player/pause"
}
function sp-play {
token=$(jq -r .access_token $tmp_code)
curl -H "Authorization:\"Bearer $token\"" -X PUT "https://api.spotify.com/v1/me/player/play" -d '{"context_uri":"spotify:playlist:$@"}'
}
function sp-search {
curl-token -X POST "https://api.spotify.com/v1/search?type=track" -G -d "q=$@"
}
function sp-help {
echo "readme"
}
subcommand="$1"
if [[ -z "$subcommand" ]]; then
sp-help
else
if $(type sp-$subcommand > /dev/null 2> /dev/null); then
shift
eval "sp-$subcommand $@"
else
eval "sp-search $@"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment