Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ripping playlists? #42

Open
lavolp3 opened this issue Oct 12, 2018 · 12 comments
Open

Ripping playlists? #42

lavolp3 opened this issue Oct 12, 2018 · 12 comments

Comments

@lavolp3
Copy link

lavolp3 commented Oct 12, 2018

Playlists seem not able to be ripped. After logging in playlist is loaded and the program immediately stopped without giving an error.

Spotify Ripper - v2.9.1 Encoding output: MP3, VBR 0 Spotify bitrate: 320 kbps Unicode support: Yes Output directory: /media/downloads/spotify Settings directory: /root/.spotify-ripper Format String: {album_artist}/({year}) {album}/{track_num:2} - {track_name}.{ext} Overwrite files: No Logging in... Logged in as 1153815924 Loading playlist... Loading playlist... Logging out...

I think I have seen problems with playlists over at mopidy. Might it be related?

@hedwiggggg
Copy link

hedwiggggg commented Oct 13, 2018

I've got the same problem. A possible workaround is, to select all the songs from the playlist and just hit Share -> Copy Spotify URIs

Edit: And obviously put them in a .txt file and give the spotify-ripper the path to the file

@lavolp3
Copy link
Author

lavolp3 commented Oct 16, 2018

@hedwiggggg thanks for the workaround.

@jposemescouilles
Copy link

i'm trying to find the root cause of this error. Does anyone have a clue ?

@lavolp3
Copy link
Author

lavolp3 commented Oct 19, 2018 via email

@DevMiKeCL
Copy link

DevMiKeCL commented Feb 6, 2019

Aquí tengo una solución para cargar playlist
i have a little solution :)

Filename: getpl.sh
command ./getpl.sh

` ClientID=""
ClientSecret=""

#Generar el codigo de autorizacion base64 ClientID:ClientSecret
basic=$(echo -n $ClientID:$ClientSecret | base64)
basic=$(sed 's/ //g' <<< $basic)

#Capturar Token

token=$(curl -X "POST" -H "Authorization: Basic $basic" -d grant_type=client_credentials https://accounts.spotify.com/api/token)
token=$(sed 's/{"access_token":"//g' <<< $token)
token=$(sed 's/","token_type":"Bearer","expires_in":3600,"scope":""}//g' <<< $token)

#Capturar Usuario y URI de playlist
#ex spotify:user:mikers2016:playlist:2jsFLqiTfNpMDfWgq8v9Zv
echo -e "Ingrese la direccion URI de la playlist\n"
read playlist
user=$(echo $playlist | cut -d ":" -f3)
uri=$(echo $playlist | cut -d ":" -f5)

#echo "https://api.spotify.com/v1/users/"$user"/playlists/"$uri"/tracks?fields=items(track(uri))"
#echo "https://api.spotify.com/v1/users/$user/playlists/"$uri"/tracks?fields=items(track(uri))" -H "Authorization: Bearer "$token #>> lista.txt

echo "Capturando nombre de playlist"
nombreplaylist=$(GET "https://api.spotify.com/v1/users/$user/playlists/$uri?fields=name" -H "Authorization: Bearer $token")
#echo $nombreplaylist
nombreplaylist=$(sed 's/{ "name" : "//g' <<< $nombreplaylist)
nombreplaylist=$(sed 's/" }//g' <<< $nombreplaylist)
nombreplaylist=$(sed 's/ /./g' <<< $nombreplaylist)

echo -e "nombre de playlist:\n"
echo $nombreplaylist

if [ ! -d "V.A.-$nombreplaylist" ]; then
echo -e "Creando directorio, accediendo"
mkdir "V.A.-$nombreplaylist"
cd "V.A.-$nombreplaylist"
else
echo "Directorio existente, accediendo..."
cd "V.A.-$nombreplaylist"
fi

#Captura de tracks en un archivo de texto
GET "https://api.spotify.com/v1/users/$user/playlists/$uri/tracks?fields=items(track(uri))" -H "Authorization: Bearer $token" >> lista.txt
#echo -e "Generando Lista\n"
if [ -f "$nombreplaylist.txt" ]; then
echo -e "Lista existente, sobreescribiendo Lista\n"
rm $nombreplaylist.txt
else
echo -e "Generando Lista\n"
fi
grep spotify lista.txt >> $nombreplaylist.txt
rm lista.txt
find -name $nombreplaylist.txt -exec sed -i 's/ "uri" : "//g' {} ;
find -name $nombreplaylist.txt -exec sed -i 's/"//g' {} ;

echo -e "Iniciando Rippeo\n"

spotify-ripper $nombreplaylist.txt `

@reardenlife
Copy link

reardenlife commented Feb 7, 2019

@DevMiKeCL
I will simplify a little bit

$ cat spotify-get-tracks.sh
#!/usr/bin/env bash

set -e
set -o errexit

CID=${2}
CS=${3}

auth=$(echo -n "$CID:$CS" | base64 -w 0)

t=$(curl -X "POST" -H "Authorization: Basic $auth" -d grant_type=client_credentials https://accounts.spotify.com/api/token)
token=$(echo "$t" | jq -r '.access_token')

playlisturl="${1}"
user=$(echo $playlisturl | cut -d "/" -f5)
uri=$(echo $playlisturl  | cut -d "/" -f7 | cut -d "?" -f1)

t=$(curl "https://api.spotify.com/v1/users/$user/playlists/$uri?fields=name" -H "Authorization: Bearer $token")
playlist=$(echo "$t" | jq -r '.name')

offset=0
while (( $offset < 42000 )); do
  t=$(curl "https://api.spotify.com/v1/users/$user/playlists/$uri/tracks?fields=items(track(uri))&offset=$offset" -H "Authorization: Bearer $token" )
  tracks=$(echo "$t" | jq -r '.items[] | .track | .uri' )
  echo "$tracks"
  if [ -z "$tracks" ]; then
    break
  fi
  offset=$[$offset + 100]
done

@reardenlife
Copy link

@DevMiKeCL , your code also had a bug. It could not download playlists longer than 100 tracks.
I fixed it in my code above.

@pualien
Copy link

pualien commented Feb 11, 2019

if you prefer a pythonic script take a look at this simple gist

@DevMiKeCL
Copy link

@DevMiKeCL , your code also had a bug. It could not download playlists longer than 100 tracks.
I fixed it in my code above.

great, thanks! i will try your code :)

@seandex
Copy link

seandex commented May 4, 2019

@DevMiKeCL
I will simplify a little bit

$ cat spotify-get-tracks.sh
#!/usr/bin/env bash

set -e
set -o errexit

CID=${2}
CS=${3}

auth=$(echo -n "$CID:$CS" | base64 -w 0)

t=$(curl -X "POST" -H "Authorization: Basic $auth" -d grant_type=client_credentials https://accounts.spotify.com/api/token)
token=$(echo "$t" | jq -r '.access_token')

playlisturl="${1}"
user=$(echo $playlisturl | cut -d "/" -f5)
uri=$(echo $playlisturl  | cut -d "/" -f7 | cut -d "?" -f1)

t=$(curl "https://api.spotify.com/v1/users/$user/playlists/$uri?fields=name" -H "Authorization: Bearer $token")
playlist=$(echo "$t" | jq -r '.name')

offset=0
while (( $offset < 42000 )); do
  t=$(curl "https://api.spotify.com/v1/users/$user/playlists/$uri/tracks?fields=items(track(uri))&offset=$offset" -H "Authorization: Bearer $token" )
  tracks=$(echo "$t" | jq -r '.items[] | .track | .uri' )
  echo "$tracks"
  if [ -z "$tracks" ]; then
    break
  fi
  offset=$[$offset + 100]
done

damn nice

@mmichon
Copy link

mmichon commented May 25, 2019

I couldn't get the spotify-get-tracks.sh variants to work (for instance, base64 doesn't have a -w argument on MacOS), but here's a quick shell script to download from a file of track URIs:

#!/bin/sh

trap 'exit' INT

while read track; do
        echo "Ripping $track to $2"
        spotify-ripper -d $2 $track
done < $1

Open your playlist(s), select all the tracks in them, right click, Share->Copy Spotify URIs, then paste into a file. Do spotify-get-tracks.sh ~/tracks ~/music/new-playlist to download all the tracks in ~/tracks to ~/music/new-playlist. No argument checking, so be careful.

@thomasmerz
Copy link

I use this for getting the token:

# ---
# generate Spotify oauth token with given client id and secret:
client_id=$(jq -r '.id' ~/.config/spotify_client_creds.json 2>/dev/null)
client_secret=$(jq -r '.secret' ~/.config/spotify_client_creds.json 2>/dev/null)
[ "$client_id" == "" ] || [ "$client_secret" == "" ] && { sed -rn 's/^### ?//;T;p' "$0"; exit 1; }
encoded_creds="$(echo -n "$client_id:$client_secret" | base64 -w 0)"
token=$(curl -s "https://accounts.spotify.com/api/token" -d "grant_type=client_credentials" -H "Authorization: Basic $encoded_creds" | jq -r .access_token)
# ---

With a JSON file ~/.config/spotify_client_creds.json like this:

{
  "id": "xxx",
  "secret": "yyy"
}

Then checking for (mandatory) parameters:

# ---
# check for mandatory parameter:
[ -z "$2" ] && { \
  echo "Usage:   $(basename $0) user playlisturl"
  echo "Example: $(basename $0) spotify https://open.spotify.com/playlist/37i9dQZF1DWXqv38T3vrCH"
  echo -e "\nThis can be used like this:"
  echo -e "for tracks in \$(./spotify-get-tracks.sh spotify https://open.spotify.com/playlist/37i9dQZF1DWXqv38T3vrCH); do\n uri=\$tracks\n docker exec spotify-ripper1 spotify-ripper \$uri\ndone\n"
  exit 1
}
playlisturl="${2}"
user="${1}"
# ---

And finally getting everything working (after some shellshecks have been done):

# ---
uri=$(echo "$playlisturl" | cut -d "/" -f5)
t=$(curl "https://api.spotify.com/v1/users/$user/playlists/$uri?fields=name" -H "Authorization: Bearer $token" 2>/dev/null)
#playlist=$(echo "$t" | jq -r '.name') ### SC2034: playlist appears unused. Verify use (or export if used externally).
offset=0
#while (( $offset < 42000 )); do ### SC2004: $/${} is unnecessary on arithmetic variables.
while (( offset < 42000 )); do
  t=$(curl "https://api.spotify.com/v1/users/$user/playlists/$uri/tracks?fields=items(track(uri))&offset=$offset" -H "Authorization: Bearer $token" 2>/dev/null)
  tracks=$(echo "$t" | jq -r '.items[] | .track | .uri' )
  if [ -z "$tracks" ]; then
    break
  fi
  echo "$tracks"
  #offset=$[$offset + 100] ### SC2007: Use $((..)) instead of deprecated $[..]
  offset=$((offset + 100))
done
# ---

ℹ️
https:/koalaman/shellcheck/wiki/SC2004
https:/koalaman/shellcheck/wiki/SC2007
https:/koalaman/shellcheck/wiki/SC2034

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants