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
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ spotify-backup

A Python script that exports all of your Spotify playlists, useful for paranoid Spotify users like me, afraid that one day Spotify will go under and take all of our playlists with it!

To run the script, [save it from here](https://raw.githubusercontent.com/caseychu/spotify-backup/master/spotify-backup.py) and double-click it. It'll ask you for a filename and then pop open a web page so you can authorize access to the Spotify API. Then the script will load your playlists and save a tab-separated file with your playlists that you can open in Excel. You can even copy-paste the rows from Excel into a Spotify playlist.
To run the script, [save it from here](https://raw.githubusercontent.com/caseychu/spotify-backup/master/spotify-backup.py) and double-click it. It'll ask you for a filename and then pop open a web page so you can authorize access to the Spotify API. Then the script will load your playlists and save a tab-separated file with your playlists that you can open in Excel. You can even copy-paste the rows from Excel into a Spotify playlist. For ease of import into other music streaming services, you can choose to output titles and artist names only, in the format of `<title> - <artist name>`.

You can run the script from the command line:

python spotify-backup.py playlists.txt

or, to get a JSON dump, use:
To get a JSON dump, use:

python spotify-backup.py playlists.json --format=json

To output titles and artists only, use:

python spotify-backup.py playlists.txt --format=plain-txt

By default, it includes your playlists. To include your Liked Songs, you can use:

python spotify-backup.py playlists.txt --dump=liked,playlists
Expand Down
33 changes: 20 additions & 13 deletions spotify-backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def main():
+ '`playlist-read-private` permission)')
parser.add_argument('--dump', default='playlists', choices=['liked,playlists', 'playlists,liked', 'playlists', 'liked'],
help='dump playlists or liked songs, or both (default: playlists)')
parser.add_argument('--format', default='txt', choices=['json', 'txt'], help='output format (default: txt)')
parser.add_argument('--format', default='txt', choices=['json', 'txt', 'plain-txt'], help='output format (default: txt)')
parser.add_argument('file', help='output filename', nargs='?')
args = parser.parse_args()

Expand Down Expand Up @@ -190,30 +190,37 @@ def main():

# Tab-separated file.
else:
f.write('Playlists: \r\n\r\n')
f.write('Playlists:\n\n')
for playlist in playlists:
f.write(playlist['name'] + '\r\n')
f.write(playlist['name'] + '\n')
for track in playlist['tracks']:
if track['track'] is None:
continue
f.write('{name}\t{artists}\t{album}\t{uri}\t{release_date}\r\n'.format(
uri=track['track']['uri'],
name=track['track']['name'],
artists=', '.join([artist['name'] for artist in track['track']['artists']]),
album=track['track']['album']['name'],
release_date=track['track']['album']['release_date']
))
f.write('\r\n')
uri=track['track']['uri']
name=track['track']['name']
artists=', '.join([artist['name'] for artist in track['track']['artists']])
release_date=track['track']['album']['release_date']
album=track['track']['album']['name']

if args.format == 'txt':
f.write(f'{name}\t{artists}\t{album}\t{uri}\t{release_date}\n')
else:
f.write(f'{name} - {artists}\n')
f.write('\n')

if len(liked_albums) > 0:
f.write('Liked Albums: \r\n\r\n')
f.write('Liked Albums: \n')
for album in liked_albums:
uri = album['album']['uri']
name = album['album']['name']
artists = ', '.join([artist['name'] for artist in album['album']['artists']])
release_date = album['album']['release_date']
album = f'{artists} - {name}'

f.write(f'{name}\t{artists}\t-\t{uri}\t{release_date}\r\n')
if args.format == 'txt':
f.write(f'{name}\t{artists}\t-\t{uri}\t{release_date}\n')
else:
f.write(f'{name} - {artists}\n')

logging.info('Wrote file: ' + args.file)

Expand Down