hbashton / spotify-ripper

https://github.com/jrnewell/spotify-ripper has been revived
MIT License
494 stars 99 forks source link

downloading playlists #61

Open pfandipfandautomat opened 4 years ago

pfandipfandautomat commented 4 years ago

Hey,

is there a workaround for downloading playlists? The actual spotify uri format for playlists is: spotify:playlist:0SyciLmBQVD3POK0jTD4BW

but the format spotify-ripper wants is like this: spotify:user:username:playlist:4vkGNcsS8lRXj4q945NIA4

thanks in advance

pfandipfandautomat commented 4 years ago

after some research, I've found out that spotify changed the API for playlists in 2018: https://developer.spotify.com/community/news/2018/06/12/changes-to-playlist-uris/

and after some more digging, I've found out that the handling of the uris is being done by libspotify, which is deprecated.

Unfortunatly my python is not the best, but I might ask someone I know who could maybe help to fix the handling of the spotify playlist uris. Doesn't seem too complicated

pfandipfandautomat commented 4 years ago

With the help of Spotipy (https://github.com/plamere/spotipy), I've created a little script, which returns the track uris of a playlist. It's a fine workaround for me right now, I just paste the track uris into a file, and let spotify-ripper do the rest.

from spotipy.oauth2 import SpotifyClientCredentials
import spotipy

def getOffset():
    results= sp.playlist_tracks(playlist_id, offset=0, fields='total')
    total = results["total"]
    return total

def getTrackURI(offset):
    results = sp.playlist_tracks(playlist_id, offset=offset, limit=1, fields='items.track.uri')
    tracks = []
    for item in results["items"]:
        tracks.append(item["track"]["uri"])
    return tracks

if __name__ == '__main__':

    client_credentials_manager = SpotifyClientCredentials()
    sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)

    playlist_id = 'spotify:playlist:45eABbQmeehl9auEpxwNDo'

    for x in range(getOffset()):
       tracks = getTrackURI(x)
       for y in tracks:
           print y
enkoder commented 3 years ago

@arthurkoch your code doesn't account for a large playlist, you'll need to handle the case where the number of tracks exceeds the limit. This code will continue to hit the API until there are no more in the playlist.

def get_track_uris_from_playlist(playlist_uri):
    client = spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials())
    track_uris = []

    # initial fetch
    results = client.playlist_tracks(playlist_uri, limit=100)
    for track in results['items']:
        track_uris.append(track['track']['uri'])

    # pagination next calls
    while results and results['next']:
        results = client.next(results)
        for track in results['items']:
            track_uris.append(track['track']['uri'])

    return track_uris
pfandipfandautomat commented 3 years ago

@enkoder Currently I'm using https://github.com/kmille/deezer-downloader, which is working very well. It also accepts Spotify Playlists.