parent
cdc4c6de0f
commit
7b41e738aa
@ -1,3 +1,3 @@
|
||||
lib/
|
||||
lib/spotify_ripper
|
||||
.env
|
||||
.token_cache
|
||||
|
@ -0,0 +1,22 @@
|
||||
#!/bin/bash
|
||||
|
||||
source .env
|
||||
|
||||
## Check if we have a non expired token in cache, or get it
|
||||
if [[ -f .token_cache ]]; then
|
||||
cached_timestamp=$(cat .token_cache | jq .expires_time)
|
||||
timestamp=$(date +%s)
|
||||
if [[ $timestamp -lt $cached_timestamp ]]; then
|
||||
sp_token=$(cat .token_cache | jq -r .token)
|
||||
[[ $sp_token == "null" ]] && unset sp_token
|
||||
fi
|
||||
fi
|
||||
if [[ ! $sp_token ]]; then
|
||||
base64_id="$(echo -n $sp_client_id:$sp_client_secret | base64 -w 0)"
|
||||
sp_token=$(curl -s --location --request POST 'https://accounts.spotify.com/api/token' --header "Authorization: Basic $base64_id" --data-urlencode 'grant_type=client_credentials' | jq -r .access_token)
|
||||
timestamp=$(date +%s)
|
||||
((timestamp=timestamp+3600))
|
||||
echo -e "{\n\"token\": \"$sp_token\",\n\"expires_time\": $timestamp\n}" | jq . > .token_cache
|
||||
fi
|
||||
|
||||
echo -n $sp_token
|
@ -0,0 +1,17 @@
|
||||
#!/bin/bash
|
||||
|
||||
[[ ! $1 ]] && echo "Please give a playlist ID" && exit 1
|
||||
playlist_id="$1"
|
||||
|
||||
sp_token=$(./lib/get_token.sh)
|
||||
json=$(curl -s https://api.spotify.com/v1/playlists/$playlist_id?access_token=$sp_token)
|
||||
name=$(echo "$json" | jq -r .name)
|
||||
links=$(echo "$json" | jq -r .tracks.items[].track.external_urls.spotify)
|
||||
|
||||
mapfile -t links <<< "$links"
|
||||
for link in "${links[@]}"; do
|
||||
links_format+="\"$link\","
|
||||
done
|
||||
links_format=${links_format::-1}
|
||||
|
||||
echo "{\"name\": \"$name\", \"tracks\": [$links_format]}"
|
Loading…
Reference in new issue