Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pretty-printed JSON format #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ You can run the script from the command line:

python spotify-backup.py playlists.txt

or, to get a JSON dump, use:
or, to get a JSON dump (possibly pretty-printed), use:

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

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

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
8 changes: 6 additions & 2 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=['pretty-json', 'json', 'txt'], help='output format (default: %(default)s)')
parser.add_argument('file', help='output filename', nargs='?')
args = parser.parse_args()

Expand Down Expand Up @@ -179,8 +179,12 @@ def main():
# Write the file.
logging.info('Writing files...')
with open(args.file, 'w', encoding='utf-8') as f:
# Pretty-printed JSON file.
if args.format == 'pretty-json':
json.dump(playlists, f, indent=2, separators=(', ', ': '))

# JSON file.
if args.format == 'json':
elif args.format == 'json':
json.dump(playlists, f)

# Tab-separated file.
Expand Down