11#!/usr/bin/env python3
2+ #
3+ # This file is licensed under the MIT license
4+ # This file originates from https://github.com/caseychu/spotify-backup
25
36import codecs
47import http .client
@@ -60,16 +63,13 @@ def authorize(client_id, scope):
6063
6164 @staticmethod
6265 def _construct_auth_url (client_id , scope , redirect_uri ):
63- return (
64- "https://accounts.spotify.com/authorize?"
65- + urllib .parse .urlencode (
66- {
67- "response_type" : "token" ,
68- "client_id" : client_id ,
69- "scope" : scope ,
70- "redirect_uri" : redirect_uri ,
71- }
72- )
66+ return "https://accounts.spotify.com/authorize?" + urllib .parse .urlencode (
67+ {
68+ "response_type" : "token" ,
69+ "client_id" : client_id ,
70+ "scope" : scope ,
71+ "redirect_uri" : redirect_uri ,
72+ }
7373 )
7474
7575 def _construct_url (self , url , params ):
@@ -122,7 +122,9 @@ def _handle_token(self):
122122 self .send_response (200 )
123123 self .send_header ("Content-Type" , "text/html" )
124124 self .end_headers ()
125- self .wfile .write (b"<script>close()</script>Thanks! You may now close this window." )
125+ self .wfile .write (
126+ b"<script>close()</script>Thanks! You may now close this window."
127+ )
126128 access_token = re .search ("access_token=([^&]*)" , self .path ).group (1 )
127129 raise SpotifyAPI ._Authorization (access_token )
128130
@@ -150,7 +152,9 @@ def fetch_user_data(spotify, dump):
150152 playlist_data = spotify .list ("me/playlists" , {"limit" : 50 })
151153 for playlist in playlist_data :
152154 print (f"Loading playlist: { playlist ['name' ]} " )
153- playlist ["tracks" ] = spotify .list (playlist ["tracks" ]["href" ], {"limit" : 100 })
155+ playlist ["tracks" ] = spotify .list (
156+ playlist ["tracks" ]["href" ], {"limit" : 100 }
157+ )
154158 playlists .extend (playlist_data )
155159
156160 return playlists , liked_albums
@@ -172,7 +176,10 @@ def write_to_file(file, format, playlists, liked_albums):
172176 uri = track ["track" ]["uri" ],
173177 name = track ["track" ]["name" ],
174178 artists = ", " .join (
175- [artist ["name" ] for artist in track ["track" ]["artists" ]]
179+ [
180+ artist ["name" ]
181+ for artist in track ["track" ]["artists" ]
182+ ]
176183 ),
177184 album = track ["track" ]["album" ]["name" ],
178185 release_date = track ["track" ]["album" ]["release_date" ],
@@ -183,9 +190,13 @@ def write_to_file(file, format, playlists, liked_albums):
183190
184191def main (dump = "playlists,liked" , format = "json" , file = "playlists.json" , token = "" ):
185192 print ("Starting backup..." )
186- spotify = SpotifyAPI (token ) if token else SpotifyAPI .authorize (
187- client_id = "5c098bcc800e45d49e476265bc9b6934" ,
188- scope = "playlist-read-private playlist-read-collaborative user-library-read" ,
193+ spotify = (
194+ SpotifyAPI (token )
195+ if token
196+ else SpotifyAPI .authorize (
197+ client_id = "5c098bcc800e45d49e476265bc9b6934" ,
198+ scope = "playlist-read-private playlist-read-collaborative user-library-read" ,
199+ )
189200 )
190201
191202 playlists , liked_albums = fetch_user_data (spotify , dump )
0 commit comments