# Code by "unhappy".
# Find me at: https://www.grepper.com/profile/unhappy
# Import spotipy and spotify auth
import spotipy
from spotipy.oauth2 import SpotifyOAuth
# Set up the client id and client secret (you can find the client secret
# from the spotify dev page)
CLIENT_ID = "[YOUR_ID]"
CLIENT_ID = "[YOUR_SECRET]"
# Set the scope (what you are requesting Spotify to be able to do)
Scope = "user-modify-playback-state"
# Create a spotify object passing the client id, client secret,
# redirct url (which doesn't matter, just set it as your local host
# as shown below), and scope
SpotifyObject = spotipy.Spotify(
auth_manager=SpotifyOAuth(
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
redirect_uri="http://localhost:8888/callback",
scope=Scope))
# Set song name and artist (artist not required, but it can help your results)
SongName = "Penny Lane"
ArtistName = "The Beatles"
# Search for the song and return the results (the "type" param controls weather
# it would return tracks, albums, playlists, etc.)
Results = SpotifyObject.search(
q=f'artist:"{ArtistName}" track:"{SongName}"',
type='track')
# Set the track uri to where it is in the returned json
TrackUri = Results['tracks']['items'][0]['uri']
# Start the playback while passing the uris (there is only one in this case, but
# it has to be passed in a list)
SpotifyObject.start_playback(uris=[TrackUri])