Last active
May 6, 2016 21:32
-
-
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
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
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