Last active
May 14, 2017 03:27
-
-
Save divisiondeariza/ba8be2605c34f56cfb1c349dc6313947 to your computer and use it in GitHub Desktop.
How get top tracks (cover, audio url and name) from a song using spotipy
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
""" | |
Yeah, I know its awful and should be a class, Maybe latter. | |
Anyway, it works like a charm :3 | |
Gets the first result in query for artist (I ain't no will improve spotify algorithms) and from it gets the top tracks from this artist | |
If doesn't find the artist throws an empty list [] | |
""" | |
import spotipy | |
spotify = spotipy.Spotify() | |
def getTopTracksFromArtistByName(name): | |
uri = getArtistURIByName(name) | |
try: | |
response = spotify.artist_top_tracks(uri) | |
return [getTrackInfo(track) for track in response["tracks"]] | |
except spotipy.SpotifyException: | |
print "not found" | |
return [] | |
def getTrackInfo(track): | |
return {"track":track['name'], | |
"audio":track['preview_url'], | |
"cover":track['album']['images'][0]['url']} | |
def getArtistURIByName(name): | |
return 'spotify:artist:' + getArtistIDByName(name) | |
def getArtistIDByName(name): | |
response = spotify.search(q=name, type='artist') | |
if response["artists"]["items"] == []: | |
return "" | |
return response["artists"]["items"][0]["id"] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment