spotify-dl/spotify-dl.sh

37 lines
1.5 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") || $(echo "$track" | grep "spotify:") ]]; then
link="$track"
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
sp_token=$(./lib/get_token.sh)
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"
rm -f /tmp/list_links.txt