From 7b41e738aa8f9bf1a50d11e0c78640aee6526af7 Mon Sep 17 00:00:00 2001 From: poka Date: Sat, 7 May 2022 20:46:32 +0200 Subject: [PATCH] Ability to downlaod playlist; Cut functions into libs --- .gitignore | 2 +- lib/get_token.sh | 22 ++++++++++++++++++++++ lib/get_tracks_from_playlist.sh | 17 +++++++++++++++++ spotify-dl.sh | 30 +++++++++++++----------------- 4 files changed, 53 insertions(+), 18 deletions(-) create mode 100755 lib/get_token.sh create mode 100755 lib/get_tracks_from_playlist.sh diff --git a/.gitignore b/.gitignore index 0d3fa21..5c29cdd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ -lib/ +lib/spotify_ripper .env .token_cache diff --git a/lib/get_token.sh b/lib/get_token.sh new file mode 100755 index 0000000..a095770 --- /dev/null +++ b/lib/get_token.sh @@ -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 diff --git a/lib/get_tracks_from_playlist.sh b/lib/get_tracks_from_playlist.sh new file mode 100755 index 0000000..a10d6d6 --- /dev/null +++ b/lib/get_tracks_from_playlist.sh @@ -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]}" diff --git a/spotify-dl.sh b/spotify-dl.sh index a8c4983..d6fb5b2 100755 --- a/spotify-dl.sh +++ b/spotify-dl.sh @@ -14,27 +14,23 @@ mkdir -p "$output" 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 +if [[ $(echo "$track" | grep "open.spotify.com") || $(echo "$track" | grep "spotify:") ]]; then 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 - ## 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 + 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