pull/20/merge
William Daniels 2020-09-04 12:20:42 -06:00 committed by GitHub
commit c2439c2028
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 0 deletions

1
.gitignore vendored 100644
View File

@ -0,0 +1 @@
*.txt

View File

@ -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
spotify-backup.py 100755 → 100644
View File

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