@@ -161,81 +161,87 @@ def write(data, path):
161
161
dump (data , open (path , "w" ), indent = "\t " )
162
162
163
163
164
- def backup (sp : Spotify ):
164
+ def backup (sp : Spotify , playlist_name = None ):
165
165
print ("Backing up... This might take a while" )
166
166
167
167
backup = Path ("backup" )
168
168
backup .mkdir (parents = True , exist_ok = True )
169
169
170
- git_status = subprocess .run (
171
- ["git" , "status" , "--porcelain" ], cwd = backup , capture_output = True , text = True
172
- )
170
+ if not playlist_name :
171
+ git_status = subprocess .run (
172
+ ["git" , "status" , "--porcelain" ], cwd = backup , capture_output = True , text = True
173
+ )
173
174
174
- if git_status .returncode != 0 :
175
- subprocess .run (["git" , "init" ], cwd = backup ).check_returncode ()
175
+ if git_status .returncode != 0 :
176
+ subprocess .run (["git" , "init" ], cwd = backup ).check_returncode ()
176
177
177
- if git_status .stdout :
178
- print ("You have uncommitted changes in backup/, exiting" )
179
- exit (1 )
178
+ if git_status .stdout :
179
+ print ("You have uncommitted changes in backup/, exiting" )
180
+ exit (1 )
180
181
181
- subprocess .run (["git" , "pull" ], cwd = backup ).check_returncode ()
182
+ subprocess .run (["git" , "pull" ], cwd = backup ).check_returncode ()
182
183
183
- rmtree ("backup/playlists" , ignore_errors = True )
184
- Path ("backup/liked-songs.json" ).unlink (missing_ok = True )
185
- Path ("backup/saved-albums.json" ).unlink (missing_ok = True )
186
- Path ("backup/followed-artists.json" ).unlink (missing_ok = True )
187
- Path ("backup/blend-names.json" ).unlink (missing_ok = True )
184
+ rmtree ("backup/playlists" , ignore_errors = True )
185
+ Path ("backup/liked-songs.json" ).unlink (missing_ok = True )
186
+ Path ("backup/saved-albums.json" ).unlink (missing_ok = True )
187
+ Path ("backup/followed-artists.json" ).unlink (missing_ok = True )
188
+ Path ("backup/blend-names.json" ).unlink (missing_ok = True )
188
189
189
190
Path ("backup/playlists/owned" ).mkdir (parents = True , exist_ok = True )
190
191
Path ("backup/playlists/collaborative" ).mkdir (parents = True , exist_ok = True )
191
192
Path ("backup/playlists/followed" ).mkdir (parents = True , exist_ok = True )
192
193
193
- print ("Backing up liked songs..." )
194
- songs = get_liked_songs (sp )
195
- write (songs , "backup/liked-songs.json" )
194
+ if not playlist_name :
195
+ print ("Backing up liked songs..." )
196
+ songs = get_liked_songs (sp )
197
+ write (songs , "backup/liked-songs.json" )
196
198
197
- print ("Backing up albums..." )
198
- albums = get_albums (sp )
199
- write (albums , "backup/saved-albums.json" )
199
+ print ("Backing up albums..." )
200
+ albums = get_albums (sp )
201
+ write (albums , "backup/saved-albums.json" )
200
202
201
- print ("Backing up followed artists..." )
202
- followed = get_followed_artists (sp )
203
- write (followed , "backup/followed-artists.json" )
203
+ print ("Backing up followed artists..." )
204
+ followed = get_followed_artists (sp )
205
+ write (followed , "backup/followed-artists.json" )
204
206
205
207
print ("Backing up playlists..." )
206
208
playlists , blends = get_playlists (sp )
207
209
for playlist in playlists :
210
+ if playlist_name and playlist ["name" ] != playlist_name :
211
+ continue
212
+
208
213
playlist ["tracks" ] = get_playlist_tracks (sp , playlist )
209
214
write (
210
215
playlist ,
211
216
f"backup/playlists/{ playlist ['type' ]} /{ slugify (playlist ['name' ])} -{ slugify (playlist ['id' ])} .json" ,
212
217
)
213
218
214
- write (blends , "backup/blend-names.json" )
219
+ if not playlist_name :
220
+ write (blends , "backup/blend-names.json" )
215
221
216
- print ("Commiting and pushing changes..." )
222
+ print ("Commiting and pushing changes..." )
217
223
218
- subprocess .run (["git" , "add" , "." ], cwd = backup ).check_returncode ()
224
+ subprocess .run (["git" , "add" , "." ], cwd = backup ).check_returncode ()
219
225
220
- subprocess .run (
221
- "git diff-index --quiet HEAD || git commit -m 'Automated update'" ,
222
- cwd = backup ,
223
- shell = True ,
224
- ).check_returncode ()
226
+ subprocess .run (
227
+ "git diff-index --quiet HEAD || git commit -m 'Automated update'" ,
228
+ cwd = backup ,
229
+ shell = True ,
230
+ ).check_returncode ()
225
231
226
- subprocess .run (["git" , "push" ], cwd = backup )
232
+ subprocess .run (["git" , "push" ], cwd = backup )
227
233
228
- print ("Backup complete!" )
229
- print ("* Your liked songs were backed up" )
230
- print ("* Your followed artists were backed up" )
231
- print ("* Your saved albums were backed up" )
232
- print ("* Names of people in blends were backed up (excluding large blends)" )
233
- print ("* The following playlists were backed up:" )
234
+ print ("Backup complete!" )
235
+ print ("* Your liked songs were backed up" )
236
+ print ("* Your followed artists were backed up" )
237
+ print ("* Your saved albums were backed up" )
238
+ print ("* Names of people in blends were backed up (excluding large blends)" )
239
+ print ("* The following playlists were backed up:" )
234
240
235
- print (
236
- tabulate (
237
- [[x ["name" ], x ["type" ]] for x in playlists ],
238
- headers = ["Name" , "Type" ],
239
- showindex = range (1 , len (playlists ) + 1 ),
241
+ print (
242
+ tabulate (
243
+ [[x ["name" ], x ["type" ]] for x in playlists ],
244
+ headers = ["Name" , "Type" ],
245
+ showindex = range (1 , len (playlists ) + 1 ),
246
+ )
240
247
)
241
- )
0 commit comments