@@ -32,19 +32,27 @@ def get_ytmusic() -> YTMusic:
32
32
sys .exit (1 )
33
33
34
34
35
- def _ytmusic_create_playlist (yt : YTMusic , title : str , description : str ) -> str :
35
+ def _ytmusic_create_playlist (
36
+ yt : YTMusic , title : str , description : str , privacy_status : str = "PRIVATE"
37
+ ) -> str :
36
38
"""Wrapper on ytmusic.create_playlist
37
39
38
40
This wrapper does retries with back-off because sometimes YouTube Music will
39
41
rate limit requests or otherwise fail.
42
+
43
+ privacy_status can be: PRIVATE, PUBLIC, or UNLISTED
40
44
"""
41
45
42
- def _create (yt : YTMusic , title : str , description : str ) -> Union [str , dict ]:
46
+ def _create (
47
+ yt : YTMusic , title : str , description : str , privacy_status : str
48
+ ) -> Union [str , dict ]:
43
49
exception_sleep = 5
44
50
for _ in range (10 ):
45
51
try :
46
52
"""Create a playlist on YTMusic, retrying if it fails."""
47
- id = yt .create_playlist (title = title , description = description )
53
+ id = yt .create_playlist (
54
+ title = title , description = description , privacy_status = privacy_status
55
+ )
48
56
return id
49
57
except Exception as e :
50
58
print (
@@ -57,7 +65,7 @@ def _create(yt: YTMusic, title: str, description: str) -> Union[str, dict]:
57
65
"s2yt error" : 'ERROR: Could not create playlist "{title}" after multiple retries'
58
66
}
59
67
60
- id = _create (yt , title , description )
68
+ id = _create (yt , title , description , privacy_status )
61
69
# create_playlist returns a dict if there was an error
62
70
if isinstance (id , dict ):
63
71
print (f"ERROR: Failed to create playlist (name: { title } ): { id } " )
@@ -73,16 +81,20 @@ def load_playlists_json(filename: str = "playlists.json", encoding: str = "utf-8
73
81
return json .load (open (filename , "r" , encoding = encoding ))
74
82
75
83
76
- def create_playlist (pl_name : str ) -> None :
84
+ def create_playlist (pl_name : str , privacy_status : str = "PRIVATE" ) -> None :
77
85
"""Create a YTMusic playlist
78
86
79
87
80
88
Args:
81
89
`pl_name` (str): The name of the playlist to create. It should be different to "".
90
+
91
+ `privacy_status` (str: PRIVATE, PUBLIC, UNLISTED) The privacy setting of created playlist.
82
92
"""
83
93
yt = get_ytmusic ()
84
94
85
- id = _ytmusic_create_playlist (yt , title = pl_name , description = pl_name )
95
+ id = _ytmusic_create_playlist (
96
+ yt , title = pl_name , description = pl_name , privacy_status = privacy_status
97
+ )
86
98
print (f"Playlist ID: { id } " )
87
99
88
100
@@ -405,6 +417,7 @@ def copy_playlist(
405
417
track_sleep : float = 0.1 ,
406
418
yt_search_algo : int = 0 ,
407
419
reverse_playlist : bool = True ,
420
+ privacy_status : str = "PRIVATE" ,
408
421
):
409
422
"""
410
423
Copy a Spotify playlist to a YTMusic playlist
@@ -429,9 +442,11 @@ def copy_playlist(
429
442
pl_name = pl ["name" ]
430
443
431
444
ytmusic_playlist_id = _ytmusic_create_playlist (
432
- yt , title = pl_name , description = pl_name
445
+ yt ,
446
+ title = pl_name ,
447
+ description = pl_name ,
448
+ privacy_status = privacy_status ,
433
449
)
434
- time .sleep (1 ) # seems to be needed to avoid missing playlist ID error
435
450
436
451
# create_playlist returns a dict if there was an error
437
452
if isinstance (ytmusic_playlist_id , dict ):
@@ -459,6 +474,7 @@ def copy_all_playlists(
459
474
spotify_playlists_encoding : str = "utf-8" ,
460
475
yt_search_algo : int = 0 ,
461
476
reverse_playlist : bool = True ,
477
+ privacy_status : str = "PRIVATE" ,
462
478
):
463
479
"""
464
480
Copy all Spotify playlists (except Liked Songs) to YTMusic playlists
@@ -477,8 +493,9 @@ def copy_all_playlists(
477
493
dst_pl_id = get_playlist_id_by_name (yt , pl_name )
478
494
print (f"Looking up playlist '{ pl_name } ': id={ dst_pl_id } " )
479
495
if dst_pl_id is None :
480
- dst_pl_id = _ytmusic_create_playlist (yt , title = pl_name , description = pl_name )
481
- time .sleep (1 ) # seems to be needed to avoid missing playlist ID error
496
+ dst_pl_id = _ytmusic_create_playlist (
497
+ yt , title = pl_name , description = pl_name , privacy_status = privacy_status
498
+ )
482
499
483
500
# create_playlist returns a dict if there was an error
484
501
if isinstance (dst_pl_id , dict ):
0 commit comments