Skip to content

Instantly share code, notes, and snippets.

@TheMuellenator
Forked from angelabauer/main.py
Last active May 6, 2024 15:46
Show Gist options
  • Save TheMuellenator/c84616c21f0f9ce68c12c357d3e1c794 to your computer and use it in GitHub Desktop.
Save TheMuellenator/c84616c21f0f9ce68c12c357d3e1c794 to your computer and use it in GitHub Desktop.
sp = spotipy.Spotify(
auth_manager=SpotifyOAuth(
scope="playlist-modify-private",
redirect_uri="http://example.com",
client_id=YOUR UNIQUE CLIENT ID,
client_secret= YOUR UNIQUE CLIENT SECRET,
show_dialog=True,
cache_path="token.txt"
)
)
user_id = sp.current_user()["id"]
date = input("Which year do you want to travel to? Type the date in this format YYYY-MM-DD: ")
song_uris = ["The list of", "song URIs", "you got by", "searching Spotify"]
playlist = sp.user_playlist_create(user=user_id, name=f"{date} Billboard 100", public=False)
# print(playlist)
sp.playlist_add_items(playlist_id=playlist["id"], items=song_uris)
@rickycc
Copy link

rickycc commented Apr 18, 2024

# Use this return to check if our OAuth is successful.
# return 'OAuth Successful'
sp = spotipy.Spotify(auth=token_info['access_token'])
user_id = sp.current_user()['id']
current_playlists = sp.current_user_playlists()['items']

song_uris = []
# Song search to add in playlist
for song in song_names:
    result = sp.search(q=f"track:{song}", type="track", market="US")
    # print track: find the
    # print(result['tracks']['items'][0])
    try:
        uri = result["tracks"]["items"][0]["uri"]
        song_uris.append(uri)
    except IndexError:
        print(f"{song} doesn't exist in Spotify. Skipped.")
# print(song_uris)

# Check if the playlist already exist for given date:
billboard_100_playlist_id = None
for playlist in current_playlists:
    if playlist['name'] == f'{date} Billboard 100':
        billboard_100_playlist_id = playlist['id']

if not billboard_100_playlist_id:
    new_playlist = sp.user_playlist_create(user_id, f'{date} Billboard 100', True)
    billboard_100_playlist_id = new_playlist['id']
    # return f"Billboard top 100 playlist id for date {date} is not found."

Awesome work. However, I am having trouble carrying out the playlist check. I cannot retrieve any information running the line below immediately after the authentication steps. I can print out user_id = sp.current_user()["id"] without any problem.
Can you shed some light on what should i do?

current_playlists = sp.current_user_playlists()['items']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment