41 lines
1.7 KiB
Bash
Executable File
41 lines
1.7 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
MP="`dirname \"$0\"`" # relative
|
|
MP="`( cd \"$MP\" && pwd )`" # absolutized and normalized
|
|
cd $MP
|
|
|
|
[[ ! $1 ]] && echo "Please give a search" && exit 1
|
|
|
|
## Get user variables and credentials
|
|
source .env
|
|
mkdir -p "$output"
|
|
|
|
## Get search or link from arguments
|
|
track=$(echo "$@" | sed 's/ /%20/g ')
|
|
|
|
## Check if we have a direct spotify link, or search words
|
|
if [[ $(echo "$track" | grep "open.spotify.com") ]]; then
|
|
link="$track"
|
|
[[ $(echo "$link" | grep "open.spotify.com/album") ]] && format="{artist} - {album}/{track_name}.{ext}"
|
|
else
|
|
## 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)
|
|
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')
|
|
fi
|
|
|
|
## 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"
|