[GH-ISSUE #382] Trying to pull artist of a specific track based on search query #224

Closed
opened 2026-02-27 23:21:28 +03:00 by kerem · 1 comment
Owner

Originally created by @innovatetom on GitHub (Jun 10, 2019).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/382

Hi, I am new to Python and I'm trying to make a tool that will tell me if a track is explicit based on the search query. It is working as I'd hoped but I want to also print the artist of the track.

At the moment, It prints the name of the track, and if the track is explicit. It also prints '[{'external_urls': {'spotify': 'https://open.spotify.com/artist/7dGJo4pcD2V6oG8kP0tJRR'}, 'href': 'https://api.spotify.com/v1/artists/7dGJo4pcD2V6oG8kP0tJRR', 'id': '7dGJo4pcD2V6oG8kP0tJRR', 'name': 'Eminem', 'type': 'artist', 'uri': 'spotify🧑‍🎨7dGJo4pcD2V6oG8kP0tJRR'}]'
For the artist.

I'd like to pull the artist name out of this if possible without the full json result.

My code is below:

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]

#User ID: 

# Erase cache and prompt for user permission

try:
    token = util.prompt_for_user_token(username)
except:
    os.remove(f".cache-{username}")
    token = util.prompt_for_user_token(username)

# Create spotifyObject
spotifyObject = spotipy.Spotify(auth=token)

user = spotifyObject.current_user()



while True:

    print()
    print("0 - Search for a track to see if it's explicit: ")
    print("1 - Exit")
    print()
    choice = input ("Your choice: ")

    # Search for an artist
    if choice == "0":
        print("0")
        searchQuery = input("What's the track name?: ")
        print()

        # Get search results
        searchResults = spotifyObject.search(searchQuery, 1, 0, "track")

        print(json.dumps(searchResults, sort_keys=True, indent=4))

        track = searchResults['tracks']['items'][0]
        print(track['name'])
        print(track['artists'])
        print("Is the track explicit?")
        if (track['explicit']) == True:
            print("Yes")
        elif (track['explicit']) == False:
            print("No")

    # End the program
    if choice == "1":
        break
Originally created by @innovatetom on GitHub (Jun 10, 2019). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/382 Hi, I am new to Python and I'm trying to make a tool that will tell me if a track is explicit based on the search query. It is working as I'd hoped but I want to also print the artist of the track. At the moment, It prints the name of the track, and if the track is explicit. It also prints '[{'external_urls': {'spotify': 'https://open.spotify.com/artist/7dGJo4pcD2V6oG8kP0tJRR'}, 'href': 'https://api.spotify.com/v1/artists/7dGJo4pcD2V6oG8kP0tJRR', 'id': '7dGJo4pcD2V6oG8kP0tJRR', 'name': 'Eminem', 'type': 'artist', 'uri': 'spotify:artist:7dGJo4pcD2V6oG8kP0tJRR'}]' For the artist. I'd like to pull the artist name out of this if possible without the full json result. My code is below: ``` 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] #User ID: # Erase cache and prompt for user permission try: token = util.prompt_for_user_token(username) except: os.remove(f".cache-{username}") token = util.prompt_for_user_token(username) # Create spotifyObject spotifyObject = spotipy.Spotify(auth=token) user = spotifyObject.current_user() while True: print() print("0 - Search for a track to see if it's explicit: ") print("1 - Exit") print() choice = input ("Your choice: ") # Search for an artist if choice == "0": print("0") searchQuery = input("What's the track name?: ") print() # Get search results searchResults = spotifyObject.search(searchQuery, 1, 0, "track") print(json.dumps(searchResults, sort_keys=True, indent=4)) track = searchResults['tracks']['items'][0] print(track['name']) print(track['artists']) print("Is the track explicit?") if (track['explicit']) == True: print("Yes") elif (track['explicit']) == False: print("No") # End the program if choice == "1": break ```
kerem closed this issue 2026-02-27 23:21:28 +03:00
Author
Owner

@stephanebruckert commented on GitHub (Jan 11, 2020):

There can be multiple artists, so you need to select the one at index 0:

track['artists'][0]['name']
<!-- gh-comment-id:573347698 --> @stephanebruckert commented on GitHub (Jan 11, 2020): There can be multiple artists, so you need to select the one at index 0: track['artists'][0]['name']
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotipy#224
No description provided.