Last active
March 27, 2024 16:59
-
-
Save iannase/38427b791a860a1f791b5fbba1791592 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ian Annase | |
# 4/16/18 | |
import os | |
import sys | |
import json | |
import spotipy | |
import webbrowser | |
import spotipy.util as util | |
from json.decoder import JSONDecodeError | |
# Get the username from terminal | |
username = sys.argv[1] | |
scope = 'user-read-private user-read-playback-state user-modify-playback-state' | |
# Erase cache and prompt for user permission | |
try: | |
token = util.prompt_for_user_token(username, scope) # add scope | |
except (AttributeError, JSONDecodeError): | |
os.remove(f".cache-{username}") | |
token = util.prompt_for_user_token(username, scope) # add scope | |
# Create our spotify object with permissions | |
spotifyObject = spotipy.Spotify(auth=token) | |
# Get current device | |
devices = spotifyObject.devices() | |
deviceID = devices['devices'][0]['id'] | |
# Current track information | |
track = spotifyObject.current_user_playing_track() | |
artist = track['item']['artists'][0]['name'] | |
track = track['item']['name'] | |
if artist != "": | |
print("Currently playing " + artist + " - " + track) | |
# User information | |
user = spotifyObject.current_user() | |
displayName = user['display_name'] | |
followers = user['followers']['total'] | |
# Loop | |
while True: | |
# Main Menu | |
print() | |
print(">>> Welcome to Spotipy " + displayName + "!") | |
print(">>> You have " + str(followers) + " followers.") | |
print() | |
print("0 - Search for an artist") | |
print("1 - exit") | |
print() | |
choice = input("Your choice: ") | |
if choice == "0": | |
print() | |
searchQuery = input("Ok, what's their name?: ") | |
print() | |
# Get search results | |
searchResults = spotifyObject.search(searchQuery,1,0,"artist") | |
# Artist details | |
artist = searchResults['artists']['items'][0] | |
print(artist['name']) | |
print(str(artist['followers']['total']) + " followers") | |
print(artist['genres'][0]) | |
print() | |
webbrowser.open(artist['images'][0]['url']) | |
artistID = artist['id'] | |
# Album and track details | |
trackURIs = [] | |
trackArt = [] | |
z = 0 | |
# Extract album data | |
albumResults = spotifyObject.artist_albums(artistID) | |
albumResults = albumResults['items'] | |
for item in albumResults: | |
print("ALBUM: " + item['name']) | |
albumID = item['id'] | |
albumArt = item['images'][0]['url'] | |
# Extract track data | |
trackResults = spotifyObject.album_tracks(albumID) | |
trackResults = trackResults['items'] | |
for item in trackResults: | |
print(str(z) + ": " + item['name']) | |
trackURIs.append(item['uri']) | |
trackArt.append(albumArt) | |
z+=1 | |
print() | |
# See album art | |
while True: | |
songSelection = input("Enter a song number to see album art and play the song (x to exit): ") # and play the song | |
if songSelection == "x": | |
break | |
trackSelectionList = [] | |
trackSelectionList.append(trackURIs[int(songSelection)]) | |
spotifyObject.start_playback(deviceID, None, trackSelectionList) # added | |
webbrowser.open(trackArt[int(songSelection)]) | |
if choice == "1": | |
break | |
# print(json.dumps(trackResults, sort_keys=True, indent=4)) |
Facing this error
Enter a song number to see album art and play the song (x to exit): 188
Traceback (most recent call last):
File "spotifyxx.py", line 115, in <module>
spotifyObject.start_playback(deviceID, None, trackSelectionList) # added
AttributeError: 'Spotify' object has no attribute 'start_playback'
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you!