-
-
Save treyholt/b8e8d706f56e7b2b2f812a9e48926887 to your computer and use it in GitHub Desktop.
Full boar with prompting and searching
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 sys | |
import spotipy | |
import spotipy.util as util | |
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