Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.txt
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ You can also run the script from the command line:

Adding `--format=json` will give you a JSON dump with everything that the script gets from the Spotify API. If for some reason the browser-based authorization flow doesn't work, you can also [generate an OAuth token](https://developer.spotify.com/web-api/console/get-playlists/) on the developer site (with the `playlist-read-private` permission) and pass it with the `--token` option.

Adding `--playlist_name="name of playlist you want"` will filter the results to only include a specifically named playlist.

Collaborative playlists and playlist folders don't show up in the API, sadly.

*The [last version compatible with Python 2.7](https://raw.githubusercontent.com/bitsofpancake/spotify-backup/1f7e76a230e10910aa2cfa5d83ced4c271377af4/spotify-backup.py) probably still works.
13 changes: 13 additions & 0 deletions spotify-backup.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def main():
+ '`playlist-read-private` permission)')
parser.add_argument('--format', default='txt', choices=['json', 'txt'], help='output format (default: txt)')
parser.add_argument('--scope', default='playlist-read-collaborative', choices=['playlist-read-private', 'playlist-read-collaborative'], help='Spotify Scope to use, to get private or private and collaborative lists. (default: playlist-read-collaborative)')
parser.add_argument('--playlist_name', help='specific playlist to export (optional)')
parser.add_argument('file', help='output filename', nargs='?')
args = parser.parse_args()

Expand All @@ -137,12 +138,24 @@ def main():
else:
spotify = SpotifyAPI.authorize(client_id='5c098bcc800e45d49e476265bc9b6934', scope=args.scope)

if (args.playlist_name):
log("only looking for playlist: " + args.playlist_name)

# Get the ID of the logged in user.
me = spotify.get('me')
log('Logged in as {display_name} ({id})'.format(**me))

# List all playlists and all track in each playlist.
playlists = spotify.list('users/{user_id}/playlists'.format(user_id=me['id']), {'limit': 50})

# Optionally filter the playlist
if (args.playlist_name):
playlists = list(filter(lambda playlist: playlist['name'] == args.playlist_name, playlists))

# If the list is empty, show a warning, return.
if (len(playlists) == 0):
log('Playlist with name ' + args.playlist_name + ' not found on your playlist... list. Did you spell it correctly? Also ensure that if it has fancy font, you copy the font too.')
return
for playlist in playlists:
log('Loading playlist: {name} ({tracks[total]} songs)'.format(**playlist))
playlist['tracks'] = spotify.list(playlist['tracks']['href'], {'limit': 100})
Expand Down