-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotify.py
44 lines (34 loc) · 1.94 KB
/
spotify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import spotipy
from spotipy.oauth2 import SpotifyOAuth
def create_new_playlist_from_liked_songs():
# Set up Spotify API credentials
client_id = 'your_client_id' # <--- Change this to your client ID
client_secret = 'your_client_secret'# <--- Change this to your client secret
redirect_uri = "http://localhost:8888/callback" # <--- Change this to your redirect URI. You can use http://localhost:8888/callback. Make sure to add this to your Spotify app in the developer dashboard.
scope = "playlist-modify-private user-library-read" # <--- Don't change this
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=client_id,
client_secret=client_secret,
redirect_uri=redirect_uri,
scope=scope))
user = sp.current_user()
user_id = user["id"]
tracks = []
offset = 0
limit = 50
while True:
liked_songs = sp.current_user_saved_tracks(limit=limit, offset=offset)
if len(liked_songs["items"]) == 0:
break
tracks.extend([track["track"]["uri"] for track in liked_songs["items"]])
offset += limit
# Create a new playlist
playlist_name = "Playlist name" # <--- Change this to the name of your new playlist
playlist_description = "Playlist description" # <--- Change this to the description of your new playlist
playlist = sp.user_playlist_create(user=user_id, name=playlist_name, public=False, description=playlist_description)
playlist_id = playlist["id"]
track_chunks = [tracks[i:i+100] for i in range(0, len(tracks), 100)]
for chunk in track_chunks:
sp.playlist_add_items(playlist_id=playlist_id, items=chunk)
print(f"New playlist '{playlist_name}' created with {len(tracks)} songs.")
# Call the function to create a new playlist from the "Liked Songs" playlist
create_new_playlist_from_liked_songs()