Ability to downlaod playlist; Cut functions into libs
This commit is contained in:
parent
cdc4c6de0f
commit
7b41e738aa
|
@ -1,3 +1,3 @@
|
||||||
lib/
|
lib/spotify_ripper
|
||||||
.env
|
.env
|
||||||
.token_cache
|
.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]}"
|
|
@ -14,27 +14,23 @@ mkdir -p "$output"
|
||||||
track=$(echo "$@" | sed 's/ /%20/g ')
|
track=$(echo "$@" | sed 's/ /%20/g ')
|
||||||
|
|
||||||
## Check if we have a direct spotify link, or search words
|
## Check if we have a direct spotify link, or search words
|
||||||
if [[ $(echo "$track" | grep "open.spotify.com") ]]; then
|
if [[ $(echo "$track" | grep "open.spotify.com") || $(echo "$track" | grep "spotify:") ]]; then
|
||||||
link="$track"
|
link="$track"
|
||||||
[[ $(echo "$link" | grep "open.spotify.com/album") ]] && format="{artist} - {album}/{track_name}.{ext}"
|
if [[ $(echo "$link" | grep "open.spotify.com/album") ]]; then
|
||||||
|
format="{artist} - {album}/{track_name}.{ext}"
|
||||||
|
elif [[ $(echo "$link" | grep "open.spotify.com/playlist") ]]; then
|
||||||
|
playlist_id=$(echo "$link" | awk -F '/' '{print $NF}' | awk -F? '{print $1}')
|
||||||
|
json_rep=$(./lib/get_tracks_from_playlist.sh $playlist_id)
|
||||||
|
name=$(echo $json_rep | jq -r .name)
|
||||||
|
echo $json_rep | jq -r .tracks[] > /tmp/list_links.txt
|
||||||
|
link="/tmp/list_links.txt"
|
||||||
|
format="$name/{artist} - {track_name}.{ext}"
|
||||||
|
fi
|
||||||
else
|
else
|
||||||
## Check if we have a non expired token in cache, or get it
|
sp_token=$(./lib/get_token.sh)
|
||||||
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)
|
|
||||||
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
|
|
||||||
link=$(curl -sX "GET" "https://api.spotify.com/v1/search?q=$track&type=track" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer $sp_token" | jq -r '.[].items[0].external_urls.spotify')
|
link=$(curl -sX "GET" "https://api.spotify.com/v1/search?q=$track&type=track" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer $sp_token" | jq -r '.[].items[0].external_urls.spotify')
|
||||||
fi
|
fi
|
||||||
|
|
||||||
## Start download
|
## Start download
|
||||||
SPOTIPY_CLIENT_ID="" SPOTIPY_CLIENT_SECRET="" SPOTIPY_REDIRECT_URI="" python lib/spotify_ripper/spotify_ripper/main.py -k .spotify_appkey.key -u "$sp_id" -p "$sp_password" $link -f "$output/$format"
|
SPOTIPY_CLIENT_ID="" SPOTIPY_CLIENT_SECRET="" SPOTIPY_REDIRECT_URI="" python lib/spotify_ripper/spotify_ripper/main.py -k .spotify_appkey.key -u "$sp_id" -p "$sp_password" $link -f "$output/$format"
|
||||||
|
rm -f /tmp/list_links.txt
|
||||||
|
|
Loading…
Reference in New Issue