Add pretty-printed JSON format

pull/33/head
Paweł Szramowski 2020-12-07 01:49:43 +01:00
parent 0e0f14eea5
commit fb0b171d99
2 changed files with 9 additions and 3 deletions

View File

@ -9,10 +9,12 @@ You can run the script from the command line:
python spotify-backup.py playlists.txt 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=json
python spotify-backup.py playlists.json --format=pretty-json
By default, it includes your playlists. To include your Liked Songs, you can use: By default, it includes your playlists. To include your Liked Songs, you can use:
python spotify-backup.py playlists.txt --dump=liked,playlists python spotify-backup.py playlists.txt --dump=liked,playlists

View File

@ -135,7 +135,7 @@ def main():
+ '`playlist-read-private` permission)') + '`playlist-read-private` permission)')
parser.add_argument('--dump', default='playlists', choices=['liked,playlists', 'playlists,liked', 'playlists', 'liked'], parser.add_argument('--dump', default='playlists', choices=['liked,playlists', 'playlists,liked', 'playlists', 'liked'],
help='dump playlists or liked songs, or both (default: playlists)') 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='?') parser.add_argument('file', help='output filename', nargs='?')
args = parser.parse_args() args = parser.parse_args()
@ -179,8 +179,12 @@ def main():
# Write the file. # Write the file.
logging.info('Writing files...') logging.info('Writing files...')
with open(args.file, 'w', encoding='utf-8') as f: 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. # JSON file.
if args.format == 'json': elif args.format == 'json':
json.dump(playlists, f) json.dump(playlists, f)
# Tab-separated file. # Tab-separated file.