Skip to content

Instantly share code, notes, and snippets.

@healthiq-treyHolterman
Last active August 26, 2019 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save healthiq-treyHolterman/fc42a28643dc79c3de1279f3db97dd46 to your computer and use it in GitHub Desktop.
Save healthiq-treyHolterman/fc42a28643dc79c3de1279f3db97dd46 to your computer and use it in GitHub Desktop.
Full boar with prompting and searching
def findTopSong(sp, artist, allTargetSongs):
allTopTracks = sp.artist_top_tracks(artist['id'] , country='US')['tracks']
if len(allTopTracks) <= 3: return
singleTopTrack = allTopTracks[0] # Grabs top Track
allTargetSongs.append(singleTopTrack)
# Takes sp for auth, the current artist being looked at, the current threshold for popularity
# and how far removed (linkLevel) the current artist is from the original favorite artists.
# Ultimately the goal of this function is to take someone's top 10 favorite artists and return
# underground artists with on especially popular song relative to the average of their top 5 songs.
def findUnpopularRelated(sp, artist, popThresh, linkLevel, allTargetSongs):
relatedArtists = sp.artist_related_artists(artist['id'])
if len(relatedArtists) < 1: return
if len(relatedArtists['artists']) < 1: return
artist = relatedArtists['artists'][0]
for artist in relatedArtists['artists']:
if artist['popularity'] < popThresh and popThresh >= 30:
findTopSong(sp, artist, allTargetSongs)
findUnpopularRelated(sp, artist, popThresh - 10, linkLevel + 1, allTargetSongs)
def promptForArtists(sp):
searchedArtists = []
nextArtist = None
while True:
nextArtist = input("Please enter an artist's name: ")
if nextArtist == "": return searchedArtists
result = sp.search(nextArtist, limit=10, offset=0, type='artist', market=None)['artists']['items'][0]
searchedArtists.append(result)
if __name__ == '__main__':
username = "<Your username>"
token = util.prompt_for_user_token(username,scope = 'user-top-read',client_id='<Your id>',client_secret='<Your secret>',redirect_uri='http://localhost/')
if token:
sp = spotipy.Spotify(auth=token)
allTargetSongs = []
startingArtists = promptForArtists(sp)
for artist in startingArtists:
print("Looking at artist: " + artist['name'])
findUnpopularRelated(sp, artist, 60, 1, allTargetSongs)
printFoundSongs(allTargetSongs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment