From cdc4c6de0f6a06a8d158b5951107e08c3316ea37 Mon Sep 17 00:00:00 2001 From: poka Date: Sat, 7 May 2022 16:42:59 +0200 Subject: [PATCH] Generate access_token from client_id and client_secret; cache it with timestamp; comment code --- .env.template | 3 ++- .gitignore | 1 + README.md | 4 ++-- install.sh | 6 +++++- spotify-dl.sh | 24 ++++++++++++++++++++++++ 5 files changed, 34 insertions(+), 4 deletions(-) diff --git a/.env.template b/.env.template index dc2e36a..2090133 100644 --- a/.env.template +++ b/.env.template @@ -1,7 +1,8 @@ # Authentification sp_id="" sp_password="" -sp_token="" +sp_client_id="" +sp_client_secret="" # Tracks format and location output="$HOME/music" diff --git a/.gitignore b/.gitignore index acc7008..0d3fa21 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ lib/ .env +.token_cache diff --git a/README.md b/README.md index 113b76e..c4acec6 100644 --- a/README.md +++ b/README.md @@ -5,8 +5,8 @@ This is not a youtube scrapper. This is a real Spotify music downloader, to mp3 **You will need a premium account to Spotify and set your ids in `.env` file,** and configure your tracks output format and custom location. -If you want to use keywords search, you will also need a developer token, set to the same file (TODO: bypass this token). -You can get this token here: https://developer.spotify.com/console/get-search-item/, but this one expire after one hour... +If you want to use keywords search, you will also need `client_id` and `client_secret`, set to the same file (TODO: bypass this ids). +You can get this ids here: https://developer.spotify.com/dashboard/applications ### Installation diff --git a/install.sh b/install.sh index 7bc3251..77c9d88 100755 --- a/install.sh +++ b/install.sh @@ -1,5 +1,9 @@ #!/bin/bash +MP="`dirname \"$0\"`" # relative +MP="`( cd \"$MP\" && pwd )`" # absolutized and normalized +cd $MP + if [[ ! $(which python) ]]; then if [[ ! $(which pyenv) ]]; then echo "pyenv installation..." @@ -12,7 +16,7 @@ if [[ ! $(which python) ]]; then pyenv global 3.9.6 fi -sudo apt install lame build-essential libffi-dev +sudo apt install lame build-essential libffi-dev jq curl wget -q -O - https://apt.mopidy.com/mopidy.gpg | sudo apt-key add - #add mopidy's libspotify repository sudo wget -q -O /etc/apt/sources.list.d/mopidy.list https://apt.mopidy.com/stretch.list #valid for Debian Stretch. sudo apt update && sudo apt install libspotify12 libspotify-dev python-spotify #install libspotify from mopidy's repository diff --git a/spotify-dl.sh b/spotify-dl.sh index dd10cab..a8c4983 100755 --- a/spotify-dl.sh +++ b/spotify-dl.sh @@ -1,16 +1,40 @@ #!/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"