-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspotify_service.py
More file actions
54 lines (42 loc) · 1.96 KB
/
spotify_service.py
File metadata and controls
54 lines (42 loc) · 1.96 KB
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
45
46
47
48
49
50
51
52
53
54
from dotenv import load_dotenv
import os
import spotipy
from spotipy.oauth2 import SpotifyOAuth
import pandas as pd
load_dotenv()
client_id=os.getenv('SPOTIPY_CLIENT_ID')
client_secret=os.getenv('SPOTIPY_CLIENT_SECRET')
redirect_uri=os.getenv('SPOTIPY_REDIRECT_URI')
scope=os.getenv('SCOPE')
def get_spotify_client():
return spotipy.Spotify(auth_manager=SpotifyOAuth(
client_id=client_id,
client_secret=client_secret,
redirect_uri=redirect_uri ,
scope=scope,
))
def load_uris_from_csv(filename="spotify_results.csv"):
df = pd.read_csv(filename)
if "spotify_uri" not in df.columns:
raise ValueError(f"Column 'spotify_uri' not found in {filename}.")
uris = [uri for uri in df["spotify_uri"].dropna().tolist() if isinstance(uri, str) and uri.strip()]
if not uris:
raise ValueError("No valid Spotify URIs found in the CSV file.")
print(f"Loaded {len(uris)} valid Spotify URIs from {filename}.")
return uris
def create_playlist_from_uris(uris, playlist_name="Kamilimu Cohort 9 Graduation Playlist 🎓", batch_size=50):
sp = get_spotify_client()
user_id = sp.current_user()['id']
playlist = sp.user_playlist_create(
user_id,
playlist_name,
public=True,
description="A playlist made by us, for us. Curated from song suggestions by Cohort 9 mentees to celebrate our graduation on 22/11/2025. From chill vibes to dance-floor moments, every track tells a story of growth, resilience, and unforgettable memories. This is our soundtrack to the end of one journey and the beginning of another 🔊💃🏽🕺🏽",
collaborative=True
)
playlist_id = playlist["id"]
for batch_index in range(0, len(uris), batch_size):
batch = uris[batch_index:batch_index + batch_size]
sp.playlist_add_items(playlist_id, batch)
print(f"Added batch {(batch_index // batch_size) + 1} with {len(batch)} tracks to playlist {playlist_id}.")
return playlist_id