wcii
wcii

Reputation: 11

How to search for track based on track name and artist in spotipy

I need to retrieve a song for the user based on song name and artist name (so I get the correct one). The end goal is to get the track id. Here is the code I thought would work:

searchResults = spotifyObject.search(q="artist:" + artist + "track:" + searchQuery, type="track")

Where artist is the name of the artist/band and searchQuery is the name of the track. This should return a JSON block for the specific track but instead returns:

{
"tracks": {
    "href": "https://api.spotify.com/v1/search?query=artist%3AHarry+Stylestrack%3AWatermelon+Sugar&type=track&offset=0&limit=10",
    "items": [],
    "limit": 10,
    "next": null,
    "offset": 0,
    "previous": null,
    "total": 0
}

}

Upvotes: 1

Views: 7366

Answers (3)

Miguel Ángel
Miguel Ángel

Reputation: 323

I don't know if this is of any help, but in spotipy's definition of the function, 'track' is the type by default, so if you pass both artist and song name into the query, there are 99% chances you get what you are searching for. You can try something like this:

searchQuery = track + ' ' + artist
searchResults = spotifyObject.search(q=searchQuery)

Getting this as a result using track="New Rules" and artist="Dua Lipa" (I included parameters market="US" and limit=1 for the purpose of this example):

{
  'tracks': {
    'href': 'https://api.spotify.com/v1/search?query=New+Rules+Dua+Lipa&type=track&market=US&offset=0&limit=1',
    'items': [{
      'album': {
        'album_type': 'album',
        'artists': [{
          'external_urls': {
            'spotify': 'https://open.spotify.com/artist/6M2wZ9GZgrQXHCFfjv46we'
          },
          'href': 'https://api.spotify.com/v1/artists/6M2wZ9GZgrQXHCFfjv46we',
          'id': '6M2wZ9GZgrQXHCFfjv46we',
          'name': 'Dua Lipa',
          'type': 'artist',
          'uri': 'spotify:artist:6M2wZ9GZgrQXHCFfjv46we'
        }],
        'external_urls': {
          'spotify': 'https://open.spotify.com/album/01sfgrNbnnPUEyz6GZYlt9'
        },
        'href': 'https://api.spotify.com/v1/albums/01sfgrNbnnPUEyz6GZYlt9',
        'id': '01sfgrNbnnPUEyz6GZYlt9',
        'images': [{
          'height': 640,
          'url': 'https://i.scdn.co/image/ab67616d0000b2736b915e407b70e121e06fe979',
          'width': 640
        }, {
          'height': 300,
          'url': 'https://i.scdn.co/image/ab67616d00001e026b915e407b70e121e06fe979',
          'width': 300
        }, {
          'height': 64,
          'url': 'https://i.scdn.co/image/ab67616d000048516b915e407b70e121e06fe979',
          'width': 64
        }],
        'name': 'Dua Lipa (Deluxe)',
        'release_date': '2017-06-02',
        'release_date_precision': 'day',
        'total_tracks': 17,
        'type': 'album',
        'uri': 'spotify:album:01sfgrNbnnPUEyz6GZYlt9'
      },
      'artists': [{
        'external_urls': {
          'spotify': 'https://open.spotify.com/artist/6M2wZ9GZgrQXHCFfjv46we'
        },
        'href': 'https://api.spotify.com/v1/artists/6M2wZ9GZgrQXHCFfjv46we',
        'id': '6M2wZ9GZgrQXHCFfjv46we',
        'name': 'Dua Lipa',
        'type': 'artist',
        'uri': 'spotify:artist:6M2wZ9GZgrQXHCFfjv46we'
      }],
      'disc_number': 1,
      'duration_ms': 209320,
      'explicit': False,
      'external_ids': {
        'isrc': 'GBAHT1600310'
      },
      'external_urls': {
        'spotify': 'https://open.spotify.com/track/2ekn2ttSfGqwhhate0LSR0'
      },
      'href': 'https://api.spotify.com/v1/tracks/2ekn2ttSfGqwhhate0LSR0',
      'id': '2ekn2ttSfGqwhhate0LSR0',
      'is_local': False,
      'is_playable': True,
      'name': 'New Rules',
      'popularity': 81,
      'preview_url': 'https://p.scdn.co/mp3-preview/75a1b521de23958a2db9acf4fc8151999ee54bd7?cid=aba114e12c4b474895556922ce1a572d',
      'track_number': 10,
      'type': 'track',
      'uri': 'spotify:track:2ekn2ttSfGqwhhate0LSR0'
    }],
    'limit': 1,
    'next': 'https://api.spotify.com/v1/search?query=New+Rules+Dua+Lipa&type=track&market=US&offset=1&limit=1',
    'offset': 0,
    'previous': None,
    'total': 53
  }
}

Upvotes: 1

Rusty Widebottom
Rusty Widebottom

Reputation: 984

You are missing a space between the artist and the track: tag.

BEFORE

searchResults = spotifyObject.search(q="artist:" + artist + "track:" + searchQuery, type="track")

AFTER

searchResults = spotifyObject.search(q="artist:" + artist + " track:" + searchQuery, type="track")

Upvotes: 6

Maxim
Maxim

Reputation: 286

I would recommend trying to use the API call links provided by Spotify, look through the following documentation in Spotify Developers (https://developer.spotify.com/documentation/web-api/reference/search/search/). Don't forget to gain an access token after you log into your account

import request
import pprint

url = "#link_created_through_spotify_api"
r = request.get(url)
dictionary = r.json() # Turn call info into dictionary
print("Status code:", r.status_code) # Print status code to see if the call was successful.

pprint(dictionary)

The pprint module will allow to print the dictionary in a very formatted and organized way. A status code of 200 would mean the call was successful. Hope this somewhat helps.

Upvotes: 0

Related Questions