Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

user_playlist_create won't create playlist despite the correct parameters being passed through #1064

Open
JoshPauls94 opened this issue Dec 21, 2023 · 1 comment
Labels

Comments

@JoshPauls94
Copy link

This code was working fine all day for me, then I went for lunch and came back to create a new playlist and now it wont work.
Please help

It keeps running this line of code for half an hour, by which point I interrupt the process
playlist = sp.user_playlist_create(USERNAME, playlist_name, public=False, description=playlist_description)

Entire code is below

import spotipy
from spotipy.oauth2 import SpotifyOAuth
import pandas as pd

df_sorted_2 = pd.read_csv('/Documents/coding_projects/partoo/new_radio_round_up.csv')

Spotify API credentials

CLIENT_ID = client_id
CLIENT_SECRET = client_secret
REDIRECT_URI = 'http://localhost:3000'
USERNAME = username

Set up Spotify authentication

sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
redirect_uri=REDIRECT_URI,
scope='playlist-modify-private'))

Create a new playlist

playlist_name = '4th Do You!! Roundup 2023'
playlist_description = 'Charlie Bones web scrape'
print("Creating playlist.....")
playlist = sp.user_playlist_create(USERNAME, playlist_name, public=False, description=playlist_description)
print("Playlist has been created")

Search for each song in the CSV and add it to the playlist

for index, row in df_sorted_2.iterrows():
track_name = row['Track Title']
artist_name = row['Artist']
# Use the Spotify API to search for the track
try:
# Refine the search query by including the artist name
search_query = f'{track_name} artist:{artist_name}'

    # Use the Spotify API to search for the track
    results = sp.search(q=search_query, type='track', limit=1)
    if results['tracks']['items']:
        track_uri = results['tracks']['items'][0]['uri']
        # Add the track to the playlist
        sp.playlist_add_items(playlist['id'], [track_uri])
    else:
        print(f"Track not found: {track_name} by {artist_name}")
except spotipy.SpotifyException as e:
    print(f"Error searching for {track_name} by {artist_name}: {e}")
    # Continue to the next iteration of the loop if there's an error
    continue

print(f"Playlist '{playlist_name}' created successfully.")

@Kudzmat
Copy link

Kudzmat commented Jan 19, 2024

Are you getting error messages?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants