Skip to content

Instantly share code, notes, and snippets.

@devStepsize
Last active May 6, 2016 21:32
Show Gist options
  • Save devStepsize/a0cc7c68da46adb30e6f5a4fcd57000e to your computer and use it in GitHub Desktop.
Save devStepsize/a0cc7c68da46adb30e6f5a4fcd57000e to your computer and use it in GitHub Desktop.
"I'm feeling lucky" type retrieval of an artist id given a search term
import requests
spotify_endpoint = "https://api.spotify.com/v1/"
def get_artist_id(search_term):
"""
I'm feeling lucky search for artist_id
"""
params = {
'q': search_term,
'type': 'artist'
}
artist_id = None
try:
r = requests.get(spotify_endpoint + 'search', params=params).json()
artists = r.get('artists', {}).get('items')
if artists:
artist_id = artists[0].get('id')
except requests.exceptions.RequestException as e:
print e
return artist_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment