MCPcopy Create free account
hub / github.com/Mrinank-Bhowmick/python-beginner-projects / playSong

Function playSong

projects/Spotify Player/main.py:130–159  ·  view source on GitHub ↗
(spotifyObject, searchQuery)

Source from the content-addressed store, hash-verified

128
129
130def playSong(spotifyObject, searchQuery):
131 searchResults = spotifyObject.search(searchQuery, 1, 0, "track")
132 track = searchResults["tracks"]["items"][0]
133
134 openSong = input(
135 "Would you like to open this song in your browser [yes/no]: "
136 ).lower()
137 song = searchResults["tracks"]["items"][0]["external_urls"]["spotify"]
138
139 if openSong in ["y", "yes"]:
140 try:
141 webbrowser.open(song)
142 except:
143 raiseError(f"Could not open: {song}")
144
145 print(
146 f"{fore.GREEN}✅ Playing {track['name']} - {track['artists'][0]['name']}{fore.RESET}"
147 )
148
149 scope = "user-modify-playback-state"
150 token = util.prompt_for_user_token(
151 username,
152 scope,
153 client_id=clientID,
154 client_secret=clientSecret,
155 redirect_uri=redirectURI,
156 )
157
158 sp = spotipy.Spotify(auth=token)
159 sp.start_playback(uris=[song])
160
161
162##############################

Callers 1

mainFunction · 0.85

Calls 2

raiseErrorFunction · 0.85
searchMethod · 0.80

Tested by

no test coverage detected