def create_entries_by_unique_artist(billboard_df, start_year, end_year): billboard_df = billboard_df[(billboard_df["Year"] >= start_year) & (billboard_df["Year"] <= end_year)] billboard_df_temp = pd.DataFrame.copy(billboard_df) billboard_unique_artists = [] billboard_songs = [] billboard_years = [] billboard_rank = [] for index_artist, row in billboard_df_temp.iterrows(): artist = row["Artist(s)"] title = row["Title"] year = row["Year"] rank = row["Num"] if artist == "Earth, Wind & Fire & The Emotions": billboard_unique_artists.append("Earth, Wind & Fire") billboard_rank.append(rank) billboard_songs.append(title) billboard_years.append(year) billboard_unique_artists.append("The Emotions") billboard_rank.append(rank) billboard_songs.append(title) billboard_years.append(year) continue elif artist == "Grover Washington, Jr. & Bill Withers": billboard_unique_artists.append("Grover Washington, Jr.") billboard_rank.append(rank) billboard_songs.append(title) billboard_years.append(year) billboard_unique_artists.append("Bill Withers") billboard_rank.append(rank) billboard_songs.append(title) billboard_years.append(year) continue elif artist == "Dionne and Friends (Dionne Warwick, Gladys Knight, Elton John and Stevie Wonder)": billboard_unique_artists.append("Dionne Warwick") billboard_rank.append(rank) billboard_songs.append(title) billboard_years.append(year) billboard_unique_artists.append("Gladys Knight") billboard_rank.append(rank) billboard_songs.append(title) billboard_years.append(year) billboard_unique_artists.append("Elton John") billboard_rank.append(rank) billboard_songs.append(title) billboard_years.append(year) billboard_unique_artists.append("Stevie Wonder") billboard_rank.append(rank) billboard_songs.append(title) billboard_years.append(year) continue elif artist == "Allen, KrisKris Allen": billboard_unique_artists.append("Kris Allen") billboard_rank.append(rank) billboard_songs.append(title) billboard_years.append(year) continue elif artist == "Crosby, Stills, Nash & Young": billboard_unique_artists.append("Crosby, Stills & Nash") billboard_rank.append(rank) billboard_songs.append(title) billboard_years.append(year) continue elif artist == "Neil Sedaka & Elton John": billboard_unique_artists.append("Neil Sedaka") billboard_rank.append(rank) billboard_songs.append(title) billboard_years.append(year) billboard_unique_artists.append("Elton John") billboard_rank.append(rank) billboard_songs.append(title) billboard_years.append(year) continue elif artist == "Neil & Dara Sedaka": billboard_unique_artists.append("Neil Sedaka") billboard_rank.append(rank) billboard_songs.append(title) billboard_years.append(year) billboard_unique_artists.append("Dara Sedaka") billboard_rank.append(rank) billboard_songs.append(title) billboard_years.append(year) elif artist == "Donna Summer and Brooklyn Dreams": billboard_unique_artists.append("Donna Summer") billboard_rank.append(rank) billboard_songs.append(title) billboard_years.append(year) billboard_unique_artists.append("Brooklyn Dreams") billboard_rank.append(rank) billboard_songs.append(title) billboard_years.append(year) elif artist == "Donny and Marie Osmond": billboard_unique_artists.append("Donny Osmond") billboard_rank.append(rank) billboard_songs.append(title) billboard_years.append(year) billboard_unique_artists.append("Marie Osmond") billboard_rank.append(rank) billboard_songs.append(title) billboard_years.append(year) elif artist in MULTIPLE_ARTIST_LIST: billboard_unique_artists.append(artist) billboard_rank.append(rank) billboard_songs.append(title) billboard_years.append(year) continue else: artist_comma_splitted = artist.split(", ") for item in artist_comma_splitted: if (item == "") or (item == " "): continue elif item in MULTIPLE_ARTIST_LIST: billboard_unique_artists.append(item) billboard_rank.append(rank) billboard_songs.append(title) billboard_years.append(year) continue item_featuring_splitted = item.split(" featuring ") for item2 in item_featuring_splitted: if (item2 == "") or (item2 == " "): continue if item2 in MULTIPLE_ARTIST_LIST: billboard_unique_artists.append(item2) billboard_rank.append(rank) billboard_songs.append(title) billboard_years.append(year) continue elif (year >= 1982) & ((" and " in item2) or (item2.startswith("and "))): if item2.startswith("and "): item_and_splitted = item2.split("and ") else: item_and_splitted = item2.split(" and ") for item3 in item_and_splitted: if (item3 == "") or (item3 == " "): continue if item3 in ARTIST_DICTIONARY: billboard_unique_artists.append(ARTIST_DICTIONARY[item3]) else: billboard_unique_artists.append(item3) billboard_rank.append(rank) billboard_songs.append(title) billboard_years.append(year) else: if item2 in ARTIST_DICTIONARY: billboard_unique_artists.append(ARTIST_DICTIONARY[item2]) else: billboard_unique_artists.append(item2) billboard_rank.append(rank) billboard_songs.append(title) billboard_years.append(year) data = {"Rank": billboard_rank, "Artist(s)": billboard_unique_artists, "Title": billboard_songs, "Year": billboard_years} unique_artist_df = pd.DataFrame(data, columns = ["Rank", "Artist(s)", "Title", "Year"]) return unique_artist_df
def getArtists(): songs = request.json['songs'] ### API Key Configuration from pyechonest import config config.ECHO_NEST_API_KEY="YZZS9XI0IMOLQRKQ6" ###################### Getting Artists + Song Info ######################## ## Basic info on the artists and song from the Echonest API based on a spotify ID sent to the ## app. This part also collects any available information on tempo, danceability and energy ## from the Echonest API for use in subsetting the recommended playlist from pyechonest import track artists = [] EchoNestSongId = [] EchoNestArtistId = [] energy = [] dance = [] tempo = [] for i in range(len(songs)): songs[i] = songs[i].replace("spotify:track:", "spotify-WW:track:") try: t = track.track_from_id(songs[i]) EchoNestSongId.append(t.song_id) EchoNestArtistId.append(t.id) artists.append(t.artist) if "energy" in vars(t): energy.append(t.energy) if "danceability" in vars(t): dance.append(t.danceability) if "tempo" in vars(t): tempo.append(t.tempo) except: continue ###################### Terms Per Artist // All Terms ######################## ### Building Terms from Artists artist_dict = {} terms = [] from pyechonest import artist ##Creates a dictionary with artists and terms associated with that artist, also creates a vector of all terms for i in range(len(artists)): artistTerm = artist.Artist(artists[i]).terms find_term = [] limitTerms = 4 for artistT in artistTerm: find_term.append(artistT['name']) limitTerms -= 1 if limitTerms == 0: break artist_dict[artists[i]] = find_term; terms.extend(find_term) ###################### Weights for Terms ######################## ##Creates weights for each term based on the number of times it appeared in artist list terms_dict = {} for i in range(len(terms)): if terms[i] not in terms_dict.items(): find_count = terms.count(terms[i]) terms_dict[terms[i]] = find_count ###################### Weights for Artists ######################## ## Adds all of the weights per term per artist to get the overall weight for an artist ## 5 subtracted from total weight (number of terms used per artist, each guarenteed a count of 5) weights = {} for key in artist_dict: getWeight=0 for value in artist_dict[key]: if value in terms: getWeight = getWeight+terms_dict[value] weights[key] = getWeight #checking weights #for key in weights: # print key +":" + str(weights[key]) #NOTE::: If we ever want to add more than 5 songs, we have to order the dictionary and select top 5 artists of terms. ###################### Finding Similar Artists w/Weights ######################## createURL = "http://developer.echonest.com/api/v4/artist/similar?api_key=YZZS9XI0IMOLQRKQ6" for key in weights: createURL += "&id=" +artist.Artist(key).id +"^" + str(weights[key]) simMake = urllib2.urlopen(createURL); echonestSim = simMake.read(); findSimilarArtist = [] for artist in echonestSim.split(":"): if "name" in artist: artistShrink = artist.split(",")[0] artistShrink= artistShrink.translate(None, string.punctuation) artistShrink = artistShrink.strip() findSimilarArtist.append(artistShrink) findSimilarArtists = findSimilarArtist[1:30] ###################### Setting Limits on Audio for Song Rec ######################## ### Used to find songs in range of input songs on danceability, enerygy, and tempo, and return full range ### values if there is not enough data to calculate an adjusted range ### NOTE: Currently - tempo and energy not used if len(dance) > 1: min_danceability = dance[dance.index(min(dance))] max_danceability = dance[dance.index(max(dance))] else: min_danceability = 0 max_danceability = 1 if len(tempo) > 1: min_tempo = tempo[tempo.index(min(tempo))] max_tempo = tempo[tempo.index(max(tempo))] else: min_tempo = 0 max_tempo = 500 if len(energy) > 1: min_energy = energy[energy.index(min(energy))] max_energy = energy[energy.index(max(energy))] else: min_energy = 0 max_energy = 1 ###################### Selecting Top Songs for Recommended Artists + Creates Playlist ######################## playlist = [] for i in findSimilarArtists: createURL="http://developer.echonest.com/api/v4/song/search?api_key=YZZS9XI0IMOLQRKQ6&artist_id=" +i+ "&bucket=id:spotify-WW&bucket=tracks&sort=song_hotttnesss-desc&min_danceability=" + str(min_danceability) + "&max_danceability=" + str(max_danceability) + "&results=5" getURL = urllib2.urlopen(createURL); clean_page = getURL.read(); if "spotify-WW:track" in clean_page: get_track = clean_page[clean_page.index("spotify-WW:track")-1:clean_page.index("spotify-WW:track")+60].replace(":", ",").split(",")[2].translate(None, string.punctuation) playlist.append("spotify:track:" + get_track) playlist = jsonify( { 'playlist': playlist} ) return(playlist)