From 38ff613f3ccfefbe63c933e96d00e4fd991dd0b6 Mon Sep 17 00:00:00 2001 From: wdaniels Date: Fri, 4 Sep 2020 12:10:27 -0600 Subject: [PATCH 1/2] adding support for grabbing a playlist by name --- .gitignore | 1 + spotify-backup.py | 13 +++++++++++++ 2 files changed, 14 insertions(+) create mode 100644 .gitignore mode change 100755 => 100644 spotify-backup.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2211df6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.txt diff --git a/spotify-backup.py b/spotify-backup.py old mode 100755 new mode 100644 index 1a1fc70..4f9f360 --- a/spotify-backup.py +++ b/spotify-backup.py @@ -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() @@ -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}) From 0e07b690bb785e3865ad092e33853998849fc810 Mon Sep 17 00:00:00 2001 From: wdaniels Date: Fri, 4 Sep 2020 12:20:29 -0600 Subject: [PATCH 2/2] updating the README to include the new param --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index b3fa7df..b685976 100644 --- a/README.md +++ b/README.md @@ -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.