Generate access_token from client_id and client_secret; cache it with timestamp; comment code

This commit is contained in:
poka 2022-05-07 16:42:59 +02:00
parent 297c836fdd
commit cdc4c6de0f
5 changed files with 34 additions and 4 deletions

View File

@ -1,7 +1,8 @@
# Authentification
sp_id=""
sp_password=""
sp_token=""
sp_client_id=""
sp_client_secret=""
# Tracks format and location
output="$HOME/music"

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
lib/
.env
.token_cache

View File

@ -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

View File

@ -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

View File

@ -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"