def get_from_echonest(title=None): if config.ECHO_NEST_API_KEY is None: raise Exception('ECHO_NEST_API_KEY variable cannot be None!') title_parts = title_splitter(title) if len(title_parts) == 0 or title_parts[1] is None: title_parts = title.split(' ') for (i, ignored) in enumerate(title_parts): word1 = ' '.join(title_parts[0:i + 1]) word2 = ' '.join(title_parts[i + 1:]) matches = song.search(title=word1, artist=word2) if matches: break matches = song.search(title=word2, artist=word1) if matches: break #matches = song.search(title=title_parts[0], artist=title_parts[1]) #if not matches: # matches = song.search(title=title_parts[1], artist=title_parts[0]) if not matches: return (None, None) else: match = matches[0] return (match.title, match.artist_name)
def getSongInfo(s, case): res7D = [] resEN = [] if case == 1: # string contains name of the song resEN = song.search(title=s) # res7D = py7D.request('track', 'search', q=s, pageSize=1) # figure out the artist elif case == 2: # string contains name of the artist resEN = song.search(artist=s) # figure out the song elif case == 3: # string contains song and artist resEN = song.search(combined=s) if len(resEN) > 0: for i in range(0, len(resEN)): title = resEN[i].title artist_name = resEN[i].artist_name print artist_name + " - " + title return resEN else: raise NameError("song not found")
def song_info(artist, title): if title is u'': print("Searching for '%s'" % artist) result = song.search(combined=artist) else: print("Searching for '%s - %s'" % (artist, title)) result = song.search(artist=artist, title=title) print_search_results(take(3, result))
def get_from_spotify(artist, title): # Getting data from spotify request_url = '{}/search'.format(SPOTIFY_API) query = '{} {}'.format(artist, title) response = requests.get(request_url, params={'q': query, 'type': 'track'}).json() items = response['tracks']['items'] if not items: print "Couldn't find '{} - {}' on Spotify".format(artist, title) return None raw_track_data = items[0] track_data = dict() for attr in ['id', 'name', 'uri', 'preview_url']: track_data[attr] = raw_track_data[attr] track_data['artist_name'] = raw_track_data['artists'][0]['name'] track_data['artist_id'] = raw_track_data['artists'][0]['id'] track_data['album'] = raw_track_data['album']['name'] track_data['image'] = raw_track_data['album']['images'][0]['url'] # EchoNest enrichement songs = echonest_song.search(artist=artist, title=title) if not songs: print "Couldn't find '{} - {}' on EchoNest".format(artist, title) return None song = songs[0] track_data['audio_summary'] = song.audio_summary artist = echonest_artist.search(name=song.artist_name)[0] track_data['genres'] = [t['name'] for t in artist.terms] return Track(**track_data)
def search_songs(**args): """ Use the Search song API, wrapped in our usual error handling try/except. All the args are passed toward songEN.search, if there is an error... good luck! Note that this implies we only look once, so not good for the step param if implemented (or you must call that function again) RETURN list of songs, can be empty """ while True: try: songs = songEN.search(**args) break except (KeyboardInterrupt, NameError): close_creation() raise except (Exception, e): print(type(e), ':', e) print('at time', time.ctime(), 'in search songs [params=' + str(args) + '] (we wait', SLEEPTIME, 'seconds)') time.sleep(SLEEPTIME) continue # done return songs
def get_artist_song_from_names(artistname, songtitle): """ Get an artist and a song from their artist name and title, return the two: artist,song or None,None if problem """ while True: try: songs = songEN.search(artist=artistname, title=songtitle, results=1, buckets=[ 'artist_familiarity', 'artist_hotttnesss', 'artist_location', 'tracks', 'id:musicbrainz', 'id:7digital', 'id:playme' ]) break except (KeyboardInterrupt, NameError, TypeError): close_creation() raise except Exception, e: print type(e), ':', e print 'at time', time.ctime( ), 'in get_artist_song_from_names (we wait', SLEEPTIME, 'seconds)' time.sleep(SLEEPTIME) continue
def get_energy(): all_tracks = userTrack.get_no_energy_tracks() all_tracks = [[one_track.artist, one_track.title, one_track.track_uuid] for one_track in all_tracks] fin_tracks = [] print("The tracks number %s" % (len(all_tracks))) for index in xrange(len(all_tracks)): track = all_tracks[index] try: results = song.search(artist=track[0], title=track[1]) result = results[0] energy = result.get_audio_summary(True)['energy'] energy *= 400 track.append(energy) fin_tracks.append(track) print("energy %s, index%s" % (energy, index)) index += 1 except EchoNestException as e: print(e) # Add it to the db if fin_tracks: userTrack.add_energy_tracks(fin_tracks) print("add to db") print("wait 20 seconds") time.sleep(20) except Exception as e: if e == 'timed out': time.sleep(5) print("Time out") continue print(e) track.append(None) index += 1 fin_tracks.append(track)
def get_tempo(artist, title): "gets the tempo for a song" results = song.search(artist=artist, title=title, results=1, buckets=buckets) if len(results['response']['songs']) > 0: return results['response']['songs'][0] else: return None
def get_echonest_metadata(search_terms, spotify_uris): buckets = [ 'audio_summary', 'artist_discovery', 'artist_familiarity', 'artist_hotttnesss', 'artist_location', 'song_discovery', 'song_hotttnesss', ] try: s = song.search( artist=search_terms['a'], title=search_terms['t'], buckets=buckets, )[0] except IndexError: sys.stdout.write(" Falling back to uri lookup\n") try: s = song.profile( track_ids=spotify_uris, buckets=buckets )[0] except EchoNestAPIError: sys.stdout.write(" Failed to find echonest metadata\n") return [] return json.dumps(s.__dict__)
def _similar_tracks(self, callback, artist, title, threshold): timestamp = now() diff = timestamp - self.get_track_timestamp(artist, title) if diff < threshold: self._logger.debug(u"similar_tracks[%s-%s]: looked up %d seconds ago" % (artist, title, diff)) return self.set_track_timestamp(artist, title, timestamp) try: self._logger.debug(u"similar_tracks[%s-%s]: lookup" % (artist, title)) self._delay() a = en_song.search(title=title, artist=artist) try: p = en_playlist.static(type='song-radio', song_id=a[0].id, results=100) i = 100.0 self._logger.info(u"similar_tracks[%s-%s]: %d result(s)" % (artist, title, len(p))) for song in p: callback(artist, title, song.artist_name.lower(), song.title.lower(), i / 100.0, self._title) i -= 1.0 except IndexError: self._logger.info(u"similar_tracks[%s-%s]: no result" % (artist, title)) return except Exception, e: self._logger.error(e) self._logger.info(u"similar_tracks[%s-%s]: no result" % (artist, title)) return
def get_tempo(artist, title): "gets the tempo for a song" results = song.search(artist=artist, title=title, results=1, buckets=['audio_summary']) if len(results) > 0: return results[0].audio_summary['tempo'] else: return None
def main(): artists = pickle.load(open("artists.pickle")) sys.stdout.write("Building...") sys.stdout.flush() start = len(artists) / 2 + 350 for i, artist in enumerate(artists[start:]): try: songs = song.search(artist_id=artist.id) except pyechonest.util.EchoNestAPIError, e: sys.stdout.write("XXX") sys.stdout.flush() continue for j, sng in enumerate(songs): metadata = get_metadata(sng) try: segments = get_segments(sng) except pyechonest.util.EchoNestAPIError, e: time.sleep(30) continue features = extract_features(segments) metadata.update({"features": features}) col.insert(metadata) sys.stdout.write(".") sys.stdout.flush() if j % 5 == 0: sys.stdout.write("zzz") sys.stdout.flush() time.sleep(2)
def plot(self): """Plot song data""" songs = echosong.search(title=self.title, artist=self.artist, results=1) if songs: # Search for the track through all catalogues for catalog in CATALOGS: # Fails when the API Key can't access a catalogue try: tracks = songs[0].get_tracks(catalog) except EchoNestAPIError: continue # Get track or go to next catalogue if not tracks: continue track = echotrack.track_from_id(tracks[0]['id']) # Adjust start and end self.start = self.start < track.duration and self.start or 0 self.end = self.end < track.duration and self.end or track.duration # Get full aoustic analysis track.get_analysis() # Loudness (from segments) x = np.array([segment.get('start') for segment in track.segments if self._inrange(segment)]) y = np.array([segment.get('loudness_max') for segment in track.segments if self._inrange(segment)]) plt.plot(x, y) plt.xlabel('Duration') plt.ylabel('Loudness') # Use sections as a grid [plt.axvline(section.get('start')+section.get('duration'), color='k', linestyle='--') for section in track.sections] # Add caption and show plot plt.suptitle('%s - %s' %(self.artist, self.title)) plt.show() return
def categorize_tweets_csv(): for tweetsfile in os.listdir(os.getcwd()): excitements = [] happy = 0 exclamations = 0 counter_num = 0 if tweetsfile.endswith(".csv"): print tweetsfile with open(tweetsfile, 'r') as csvfile: csvreader = csv.reader(csvfile) for tweet, sentiment, accuracy in csvreader: counter_num += 1 if sentiment == "positive" and accuracy >= 50: happy += 1 if tweet.count("!") > 1 and tweet.count(".") <= 1: exclamations += 1 exclamation_percentage = exclamations / float(counter_num) # excitement = (sum(excitements) + exclamations) / float(len(excitements)) happy /= float(counter_num) if exclamation_percentage > .15: if happy > .4: mood = "happy" else: mood = "angry" else: if happy > .4: mood = "relaxed" else: mood = "sad" rkp_results = song.search(mood=mood, min_energy=exclamation_percentage, artist_min_familiarity=.6, style="pop", artist_start_year_after="1999") resultant_song = rkp_results[0] print resultant_song.title + " - " + resultant_song.artist_name + " happy " + str(happy) + " excite " + str(exclamation_percentage)
def findTrack(): track = request.args.get('song', '') artist = request.args.get('artist', '') filenames = ['TRBIJES12903CF5B12.h5', \ 'TRBIJFB128F92ED124.h5', \ 'TRBIJFO128F42990C5.h5', \ 'TRBIJIA128F425F57D.h5', \ 'TRBIJIP128F9334953.h5', \ 'TRBIJKN12903CBF11B.h5', \ 'TRBIJLT12903CE7070.h5', \ 'TRBIJMU12903CF892B.h5', \ 'TRBIJNF128F14815A7.h5', \ 'TRBIJNK128F93093EC.h5', \ 'TRBIJRN128F425F3DD.h5', \ 'TRBIJYB128F14AE326.h5'] if track == '' or artist == '': return render_template('tracksearch.html', searchOk = False, filenames = filenames) else: # Find the requested song with echonest API result = song.search(artist = artist, title = track)[0] return render_template('tracksearch.html', searchOk = True, result = result, filenames = filenames)
def dance_songs(artist_id, dance=0.6, maxresults=10): results = song.search(artist_id=artist_id, min_danceability=dance, results=maxresults, sort='danceability-desc', buckets=['audio_summary']) return results
def lookup_track(album, metadata, release, track): tagger = album.tagger upload = tagger.config.setting["echonest_upload"] artist_title_lookup = tagger.config.setting["echonest_artist_title_lookup"] songs = [] try: songs = echosong.profile([u"musicbrainz:song:%s" % metadata['musicbrainz_trackid']], buckets="audio_summary") except EchoNestAPIError: if artist_title_lookup: songs = echosong.search(title=metadata['title'], artist=metadata['artist']) min_diff = tagger.config.setting["echonest_duration_diff"] match = None for song in songs: # try to match based on duration / length len_diff = abs(metadata.length / 1000.0 - song.audio_summary['duration']) if min_diff < 0.0 or len_diff < min_diff: min_diff = len_diff match = song if match: metadata['bpm'] = str(match.audio_summary['tempo']) metadata['comment:Songs-DB_Energy'] = str(match.audio_summary['energy']) metadata['comment:Songs-DB_Danceability'] = str(match.audio_summary['danceability']) elif upload: # FIXME: how do i get the filename for this track? pass req_minus(album)
def fetch_track(track_params): """ Fetch a track from 7digital via the Echo Nest API. Available track parameters are listed at http://developer.echonest.com/docs/v4/song.html#search """ try: search_results = echonest_song.search( buckets=['id:7digital-US', 'tracks'], limit=True, results=app.config['ECHO_NEST_SONG_RESULTS'], **track_params ) except TypeError as exc: raise GweetrError("Received unknown track parameter: %s" % str(exc)) if search_results: song_obj = random.choice(search_results) tracks = song_obj.get_tracks('7digital-US') track_data = tracks[0] track = { 'title': song_obj.title, 'artist': song_obj.artist_name, 'url': track_data['preview_url'] } return track
def dump(file): dictionary = {} failedList = [] with open("parsedHits.csv") as csv_file: i=0; for row in csv.reader(csv_file, delimiter=','): print i i=i+1 sleep(3) #total = int(col) songArtist = row[2] songName = row[1] print "getting hits:%s from %s"%(songName,songArtist) startTime = time.time() record=song.search(artist = songArtist, title=songName) endTime = time.time() print "time for api call was: %s ms"%(endTime - startTime) if record: dictionary[songName]=record[0] #dictionary[songName]=record[0].audio_summary['tempo'] print "song name: %s , %s "%(songName,record[0].audio_summary) else: failedList.append([songArtist,songName]) if(i>60): sleep(60) i=0; #print dictionary pickle.dump(failedList, open(failed.p,"wb")) pickle.dump( dictionary, open( file+".p", "wb" ) ) print "printing failedList..." for i in failedList: print i print "printed failedList..."
def __init__( self, name, artist, file_url=None, echonest_id=None, songsterr_id=None, rdio_id=None, key=None, tempo=None, loudness=None, major_key=None, genre=None, ): en_result = en_song.search(artist=artist.name, title=name, results=1)[0] en_summary = en_result.get_audio_summary() print en_result.title print en_summary self.name = name self.artist = artist self.file_url = file_url self.echonest_id = en_result.id self.songsterr_id = songsterr_id self.rdio_id = rdio_id self.key = en_summary[u"key"] self.tempo = en_summary[u"tempo"] self.loudness = en_summary[u"loudness"] self.major_key = en_summary[u"mode"] self.genre = genre
def querySong(songName, artist): rkp_results = song.search(artist=artist, title=songName) karma_police = rkp_results[0] print karma_police.artist_location print 'tempo:', karma_police.audio_summary[ 'tempo'], 'duration:', karma_police.audio_summary['duration'] print rkp_results[0]
def fetch_features(tracks): features = np.zeros((len(tracks), 125)) found_tracks = list() for k, track in enumerate(tracks): results = song.search(artist=track[0], title=track[1]) if (len(results) == 0): continue s = results[0] found_tracks.append(track) print 'fetching features for track %d: %s - %s' % (k, track[0], track[1]) energy = s.audio_summary['energy'] avg_loudness = s.audio_summary['loudness'] tempo = s.audio_summary['tempo'] url = s.audio_summary['analysis_url'] try: data = urllib2.urlopen(url) j = json.load(data) #fetch features segments = j['segments'] num_segments = len(segments) timbres = np.zeros((12, num_segments)) pitches = np.zeros((12, num_segments)) start_segments = np.zeros((num_segments)) loudness_max = np.zeros((num_segments)) for i in range(num_segments): timbre = segments[i]['timbre'] timbres[:, i] = np.array(timbre).T pitch = segments[i]['pitches'] pitches[:, i] = np.array(pitch).T start_segments[i] = segments[i]['start'] loudness_max[i] = segments[i]['loudness_max'] timbres = timbres.T pitches = pitches.T timbre = (timbres.mean(axis=0), timbres.var(axis=0), np.median(timbres, axis=0), np.min(timbres, axis=0), np.max(timbres, axis=0)) pitch = (pitches.mean(axis=0), pitches.var(axis=0), np.median(pitches, axis=0), np.min(pitches, axis=0), np.max(pitches, axis=0)) idx = np.where((start_segments > j['track']['end_of_fade_in']) & (start_segments < j['track']['start_of_fade_out'])) loudness = ( avg_loudness, np.var(loudness_max[idx]), abs(max(loudness_max[idx]) - abs(min(loudness_max[idx])))) features[k] = np.concatenate( (timbre[0], timbre[1], timbre[2], timbre[3], timbre[4], pitches[0], pitches[1], pitches[2], pitches[3], pitches[4], np.array([tempo]), np.array(loudness), np.array([energy]))) except: print "could not find song" #remove zero rows idx_nonzero = np.unique(features.nonzero()[0]) features = features[idx_nonzero, :] return (found_tracks, features)
def get_song(combined="kreayshawn gucci gucci"): s = song.search(combined=combined,buckets=["id:7digital-US","tracks"],limit=True,results=1) if len(s) > 0: try: url = s[0].get_tracks("7digital-US")[0]["preview_url"] return (url, s[0]) except KeyError: return (None, None)
def get_songs(name): # get 50 hottest songs for each artist to use as chapter headings song_titles = [] songs = en_song.search(artist=name,results=50,sort='song_hotttnesss-desc') for song in songs: if song.title not in song_titles: song_titles.append(song.title) return song_titles
def check_song(artist, title): res = song.search(artist=artist, title=title) if res: meta = res[0].audio_summary # HAAAAAACKS display_metadata("{0} - {1}".format(artist, title), meta['tempo'], meta['key'], meta['mode'])
def search(request): song_title = request.POST['SongTitle'] try: search_results = song.search(title=song_title) except EchoNestAPIError: template = loader.get_template('error.html') context = RequestContext( request, {'Error_Message': 'Please enter a song title.'}) return HttpResponse(template.render(context)) try: search_result_0 = search_results[0] # return HttpResponse("You're looking for song %s. Song Tempo is %d" % (song_title, search_result_0.audio_summary['tempo'],)) template = loader.get_template('melody.html') song_key = get_song_key(search_result_0.audio_summary['key'], search_result_0.audio_summary['mode']) context = RequestContext( request, { 'Song_Name': search_result_0.title, 'Artist': search_result_0.artist_name, 'Tempo': search_result_0.audio_summary['tempo'], 'Key': song_key, 'Energy': search_result_0.audio_summary['energy'], 'Time_signature': search_result_0.audio_summary['time_signature'], #'Liveness': search_result_0.audio_summary['liveness'], #'Acousticness': search_result_0.audio_summary['acousticness'], #'Analysis_url': search_result_0.audio_summary['analysis_url'], #'Audio_md5': search_result_0.audio_summary['audio_md5'], #'Danceability': search_result_0.audio_summary['danceability'], #'Duration': search_result_0.audio_summary['duration'], #'Instrumentalness': search_result_0.audio_summary['instrumentalness'], #'Loudness': search_result_0.audio_summary['loudness'], #'Mode': search_result_0.audio_summary['mode'], #'Speechiness': search_result_0.audio_summary['speechiness'], #'valence': search_result_0.audio_summary['valence'], 'Scale_Notes': get_notes('C-Minor'), 'Scale_Chords': get_chords(search_result_0.audio_summary['key'], search_result_0.audio_summary['mode']), }) return HttpResponse(template.render(context)) except IndexError: template = loader.get_template('error.html') context = RequestContext( request, { 'Error_Message': 'The Song ' + "\"" + song_title + "\"" + ' could not be found. Please enter a new song.' }) return HttpResponse(template.render(context))
def song_search(): songs = song.search(min_tempo=120, max_tempo=160, artist_start_year_after=1980, artist_end_year_before=2000, min_danceability=0.6, artist_min_familiarity=0.5, buckets=["id:spotify-WW", "tracks"]) for s in songs: print s.title, "by", s.artist_name for t in s.get_tracks("spotify-WW"): uri = t["foreign_id"].replace("spotify-WW", "spotify") print " ", uri
def getSong(searchArtist): # Artist search songResult = song.search(artist=searchArtist, buckets=["tracks",catalogId], limit="true") # Select a random track within the collection randIndex = random.randint(0, len(songResult) - 1) return songResult[randIndex]
def get_lyricfind_ids(artist=None, title=None, genre=None, num_songs=1): if genre: res = playlist.static(results=num_songs,type='genre-radio',genres=genre,buckets=['id:lyricfind-US'], limit=True) elif not title: res = playlist.static(results=num_songs,type='artist',artist=artist,buckets=['id:lyricfind-US'], limit=True) else: res = song.search(sort='song_hotttnesss-desc', buckets=['id:lyricfind-US'], limit=True, artist=artist, title=title, results=num_songs) lids = [x.get_foreign_id('lyricfind-US').split(":")[-1] for x in res] print [x for x in lids]
def test_get_song_audio_and_analysis(): buckets = ['id:7digital-US', 'tracks'] the_songs = song.search(artist='The National', title='Slow Show', buckets=buckets, limit=True) assert len(the_songs) > 0 # at least 1 matching song the_song = the_songs[0] assert the_song.title == 'Slow Show' tracks = the_song.get_tracks('7digital-US') assert len(tracks) > 0 # at least 1 track for the song assert tracks[0]['album_name'] == 'Boxer'
def retrieveTempo(artistName, songName): rkp_results = song.search(artist=artistName, title=songName) try: searchResult = rkp_results[0] return searchResult.audio_summary['tempo'] except UnboundLocalError: return 0 except IndexError: return 0
def search(self, q='', special='', sid='', artist='', title='', callback='', _=''): if callback: cherrypy.response.headers['Content-Type']= 'text/javascript' else: cherrypy.response.headers['Content-Type']= 'application/json' print 'total', self.total, 'cached', self.cached, q, callback response = {} if len(special) > 0: results = self.read_from_cache(special) if results: results = callback + "(" + results + ")" return results else: response['status'] = 'not_found' return to_json(response, callback) elif len(sid) > 0: result = song_api.Song(sid, buckets=[rcatalog, 'tracks', 'audio_summary'], limit=True, results=1) results = [result] elif len(artist) > 0: results = song_api.search(artist=artist, title=title,\ buckets=[rcatalog, 'tracks', 'audio_summary'], limit=True, results=1) else: results = song_api.search(combined=q, \ buckets=[rcatalog, 'tracks', 'audio_summary'], limit=True, results=1) if len(results) > 0: track = results[0].get_tracks(catalog)[0] id = track['id'] results = self.read_from_cache(id) if results: print 'cache hit' else: print 'cache miss' response['status'] = 'ok' t = self.get_track(id) response['track'] = t results = to_json(response, None) self.write_to_cache(id, results) results = callback + "(" + results + ")" return results else: response['status'] = 'not_found' return to_json(response, callback)
def process_track(track): filename = track['filename'] artist = track['artist'] title = track['song'] if not filename in backup and track.get('album', None) is not None: results = song.search(artist=artist, title=title, buckets=['id:whosampled', 'id:7digital-US', 'id:spotify-WW', 'tracks', 'audio_summary', 'artist_location', 'artist_familiarity', 'artist_hotttnesss']) if len(results) > 0: result = results[0] backup[filename] = result song_obj = Song() song_obj.title = result.title song_obj.filename = filename artist, new_artist = Artist.objects.get_or_create(echonest_id=result.artist_id, defaults= {'name': result.artist_name, 'familiarity':result.artist_familiarity, 'hotness': result.artist_hotttnesss} ) song_obj.artist = artist artist_location = result.artist_location lat = artist_location['latitude'] lng = artist_location['longitude'] place_name = artist_location['location'] if lat is not None and lng is not None and place_name is not None and new_artist: artist.place = Place.objects.get_or_create(name=place_name, defaults={'latitude': lat, 'longitude': lng})[0] artist.save() album = Album.objects.get_or_create(name=track['album'])[0] song_obj.album = album album.artists.add(artist) audio_summary = result.audio_summary song_obj.key = audio_summary['key'] song_obj.mode = audio_summary['mode'] song_obj.bpm = audio_summary['tempo'] song_obj.echonest_id = result.id song_obj.content = track['lyrics'] whosampled = result.get_tracks('whosampled') sevendigital = result.get_tracks('7digital-US') spotify = result.get_tracks('spotify-WW') if len(whosampled) > 0: song_obj.whosampled_id = whosampled[0]['foreign_id'] if len(sevendigital) > 0: song_obj.sevendigital_id = sevendigital[0]['foreign_id'] if len(spotify) > 0: song_obj.spotify_id = spotify[0]['foreign_id'] song_obj.save() if song_obj.spotify_id is not None: get_date_from_spotify(spotimetadata, song_obj)
def connie(URL=None): mood = texts(request.form['URL']) songlist = song.search(mood=mood, buckets=['tracks','id:spotify-WW'], limit=True, results=20) foreign_ids = [] for item in songlist: for t in item.get_tracks('spotify-WW'): foreign_ids.append(t['foreign_id'][17:]) comma = "," foreign_id_string = comma.join(foreign_ids) return render_template("playlist.html", tracks=foreign_id_string)
def get_song(tweet): text = tweet[5] title = clean_tweet(text) try: results = song.search( title = title, results = 1, buckets = ['audio_summary'] ) return results except: return []
def get_artist_song_from_names(artistname, songtitle): """ Get an artist and a song from their artist name and title, return the two: artist,song or None,None if problem """ while True: try: songs = songEN.search(artist=artistname, title=songtitle, results=1, buckets=[ 'artist_familiarity', 'artist_hotttnesss', 'artist_location', 'tracks', 'id:musicbrainz', 'id:7digital', 'id:playme' ]) break except (KeyboardInterrupt, NameError, TypeError): close_creation() raise except (Exception, e): print(type(e), ':', e) print('at time', time.ctime(), 'in get_artist_song_from_names (we wait', SLEEPTIME, 'seconds)') time.sleep(SLEEPTIME) continue # sanity checks if len(songs) == 0: print('no song found for:', artistname, '(', songtitle, ')') return None, None if CREATION_CLOSED: return None, None # get artist song = songs[0] while True: try: artist = artistEN.Artist(song.artist_id) break except KeyboardInterrupt: close_creation() raise except (pyechonest.util.EchoNestAPIError, e): print('MAJOR ERROR, wrong artist id?', song.artist_id) print(e) # means the ID does not exist return None, None except (Exception, e): print(type(e), ':', e) print('at time', time.ctime(), 'in get_artist_song_from_names, aid=', song.artist_id, '(we wait', SLEEPTIME, 'seconds)') time.sleep(SLEEPTIME) continue # done return artist, song
def getSongs(emots): config.ECHO_NEST_API_KEY = "7UL5JWDT2WWMWSMYS" if u'faceRectangle' not in emots: return "" emots = emots['scores'] happiness = emots['happiness'] anger = emots['anger'] contempt = emots['contempt'] disgust = emots['disgust'] fear = emots['fear'] sadness = emots['sadness'] neutral = emots['neutral'] surprise = emots['surprise'] biggestMood = max(happiness, sadness, anger) if biggestMood == happiness: biggestMood = "happy" if biggestMood == sadness: biggestMood = "sad" if biggestMood == anger: biggestMood = "angry" # max_tempo=None # min_tempo=None # max_loudness=None # min_loudness=None min_energy = sadness max_energy = surprise if (min_energy > max_energy): min_energy = 0.5 max_energy = 0.75 min_danceability = neutral max_danceability = happiness if (min_danceability > max_danceability): min_danceability = 0.5 max_danceability = 0.75 # max_acousticness=None # min_acousticness=None # max_liveness=None # min_liveness=None # max_valence=None # min_valence=None # return song.search(mood = mood, min_energy = min_energy, min_danceability = min_danceability, max_danceability = max_danceability) x = song.search(mood=biggestMood, min_energy=min_energy, max_energy=max_energy, min_danceability=min_danceability, max_danceability=max_danceability) return x[0]
def get_allData(artist, title): # gets the various data for a song # Use help(song.search) for other useful data results = song.search(artist=artist, title=title, results=1, buckets=['audio_summary']) if len(results) > 0: tempo = results[0].audio_summary['tempo'] energy = results[0].audio_summary['energy'] valence = results[0].audio_summary['valence'] acousticness = results[0].audio_summary['acousticness'] return tempo, energy, valence, acousticness else: return None
def getEchonestSongData ( src, artistName, songName ) : # res = song.search( artist="Justin Bieber", title="Baby" ) res = song.search( artist=artistName, title=songName ) # res = song.search( artist='Charlie Parker', title='Now\'s the time') print( res ) for curres in res : print curres.get_audio_summary() if not res: raise Exception( "SongNotFound" )
def getSongs(style,mood,results=15,max_tempo=200.0,min_tempo=80.0,max_danceability=0.8,min_danceability=0.5): start_year = random.randint(1970,2012) print start_year songs = song.search(style=style, mood=mood, results=results,min_tempo=min_tempo,max_tempo=max_tempo,min_danceability=min_danceability,max_danceability=max_danceability,buckets=["id:spotify-WW"],limit='true',max_duration=240, sort='artist_familiarity-asc', artist_start_year_before=start_year) spotify_songs=[] for element in songs: spotify_values = element.get_tracks('spotify-WW') spotify_songs.append(spotify_values[0].values()[1]) return songs
def create_track_files_from_artist(maindir,artist,mbconnect=None,maxsongs=100): """ Get all songs from an artist, for each song call for its creation We assume we already have artist. We can have a connection to musicbrainz as an option. This function should create only one file! GOAL: handles some errors. INPUT maindir - MillionSongDataset root directory artist - pyechonest artist object for that song/track mbconnect - open musicbrainz pg connection maxsongs - maximum number of files retrieved, default=100, should be 500 or 1k RETURN number fo files created, 0<=...<=1000? """ assert maxsongs <= 1000,'dont think we can query for more than 1K songs per artist' if maxsongs==100: #print 'WARNING,create_track_files_from_artist, start param should be implemented' pass # get all the songs we want allsongs = [] while True: try: n_missing = maxsongs - len(allsongs) n_askfor = min(n_missing,100) start = len(allsongs) songs = songEN.search(artist_id=artist.id, buckets=['id:'+CATALOG, 'tracks', 'audio_summary', 'artist_familiarity','artist_hotttnesss', 'artist_location','song_hotttnesss'], limit=True, results=n_askfor) allsongs.extend(songs) if len(allsongs) >= maxsongs or len(songs) < n_askfor: break print ('WARNING tracks from artists, we cant search for more than 100 songs for the moment (pid='+str(os.getpid())+')') break # we have not implemented the start yet except KeyboardInterrupt: close_creation() raise except pyechonest.util.EchoNestAPIError,e: if str(e)[:21] == 'Echo Nest API Error 5': # big hack, wrong artist ID print ('SKIPPING ARTIST',artist.id,'FOR NONEXISTENCE') return 0 else: print type(e),':',e print ('at time',time.ctime(),'in create_track_file_from_artist, aid=',artist.id,'(we wait',SLEEPTIME,'seconds) (pid='+str(os.getpid())+')') time.sleep(SLEEPTIME) continue except Exception,e: print type(e),':',e print ('at time',time.ctime(),'in create_track_files_from_artist, aid=',artist.id,'(we wait',SLEEPTIME,'seconds) (pid='+str(os.getpid())+')') time.sleep(SLEEPTIME) continue
def findSimilarArtists(dataset, name_of_artist, how_many_of_their_songs, euclidean_threshold, features_used): #returning this, this contains INDEXES of similar artists. you must refer to artist_id, artist_name, genre array, etc for it to work. similar_artists = [] #rebuild the data to select our features artist_id, artist_name, genre, features = loadDataset( dataset, features_used) k_means_cluster = KMeans(n_init=5, random_state=1) k_means_cluster.fit(features, genre) #get a single song songs_by_artist = song.search(artist=name_of_artist.lower(), results=how_many_of_their_songs, buckets=['audio_summary']) features_of_artist = [] for i in range(0, len(songs_by_artist)): single_feature_set = [] for g in range(0, len(features_used)): if features_used[g]: single_feature_set.append( songs_by_artist[i].audio_summary[features_names[g]]) features_of_artist.append(single_feature_set) for x in range(0, len(features_of_artist)): clusters_belong = k_means_cluster.predict(features_of_artist[x]) for i in range(0, len(clusters_belong)): for g in range( 0, len( np.where(k_means_cluster.labels_ == clusters_belong[i]) [0])): index = np.where( k_means_cluster.labels_ == clusters_belong[i])[0][g] euc_dist = distance.euclidean(features_of_artist[x], features[index]) if euc_dist <= euclidean_threshold: if (artist_name[index].lower() != name_of_artist.lower()): if artist_name[index] not in [ row[0] for row in similar_artists ]: artist_data = [] artist_data.append(artist_name[np.where( k_means_cluster.labels_ == clusters_belong[i]) [0][g]]) artist_data.append(euc_dist) similar_artists.append(artist_data) #sort the array based on second column. similar_artists = sorted(similar_artists, key=itemgetter(1)) return similar_artists
def search_song(request, query): ss_results = echo_song.search(combined=query, buckets=['id:spotify-WW'], limit=True, results=5) songs = [] if len(ss_results) > 0: songs = [{ "artist": s.artist_name, "title": s.title, "echonest_id": s.id, "spotify_trackid": _get_spotify_trackid(s) } for s in ss_results] return HttpResponse(json.dumps(songs), content_type="application/json")
def getEchonestMetadata(track, search_artist=True): artist_id = None possible_artists = [] possible_tracks = [] try: ''' Get all recordings from echonest with the corresponding artist and track-title ''' recordings = echonest_song.search(artist=track[0], title=track[1]) for recording in recordings: ''' check if echonest found the correct title or if it returned just similar written tracks ''' if utils.is_similar(recording.title, track[1], normalize=True) and utils.is_similar( recording.artist_name.lower(), track[0]): if artist_id is None: artist_id = recording.artist_id ''' there may exist multiple entities for one song at echonest, so I collect all the matching songs ''' possible_tracks.append(recording) ''' if we found more than one track, get the mean metadata for all of them ''' if len(possible_tracks) > 0: getEchonestTrackMetadata(possible_tracks) if search_artist: if artist_id is None: ''' if echonest couldn't find a corresponding match for the given artist and track, there are chances it will still find only the artist by it's name ''' artists = echonest_artist.search(name=track[0]) for artist in artists: if utils.is_similar(artist.name, track[0], normalize=True): possible_artists.append(artist) if len(possible_artists) > 1: pass # TODO let user choose elif len(possible_artists) == 1: getEchonestArtistMetadata(possible_artists[0]) else: getEchonestArtistMetadata(echonest_artist.Artist(artist_id)) except socket.timeout: track_md.error = True print colored("| Socket timeout...", 'red')
def songData(art, ti): """ This function has a dependency you need to install. You'll need to install pyechonest. the easiest way to do this is to use setuptools and run the command "easy_install pyechonest" """ from pyechonest import config, song config.ECHO_NEST_API_KEY="J52LGDHNBUF5VOJDA"; results = song.search(artist = art, title = ti); song_result = results[0]; if song_result is None: print "Song not found."; return -1; return song_result.audio_summary['tempo'];
def search_echonest_for_song(search_term, match_threshold=.75): search_results = song.search(combined=search_term) if search_results: score_results = [] for search_result in search_results: matched_term = search_result.artist_name + ' ' + search_result.title similarity_score = calculate_similarity(search_term, matched_term, match_threshold) score_results.append({ 'similarity_score': similarity_score, 'song_object': search_result }) top_score_result = max(score_results, key=lambda x: x['similarity_score']) if top_score_result['similarity_score'] >= match_threshold: return top_score_result['song_object']
def get_data(self): try: s = song.search(title=self.title, artist=replace_artist(self.artist))[0] self.song_hotness = s.song_hotttnesss self.artist_hotness = s.artist_hotttnesss self.artist_familiarity = s.artist_familiarity self.danceability = s.audio_summary['danceability'] self.duration = s.audio_summary['duration'] self.energy = s.audio_summary['energy'] self.tempo = s.audio_summary['tempo'] self.populated = True except IndexError: print("Song not found: {} - {}".format( self.title.encode('utf-8'), self.artist.encode('utf-8'))) with open("scripts/already_tried.txt", "a") as f: f.write("Song not found: {} - {}".format( self.title.encode('utf-8'), self.artist.encode('utf-8')) + '\n')
def create(self, style, max_tempo=None, min_tempo=None, key=None): print('Starting search') songs = song.search(style=style, max_tempo=max_tempo, min_tempo=min_tempo, key=key, results=1) print('Search returned') start = time.time() song_ids = [] for track in songs: song_ids.append(track.id) # TODO get the real tempo tempo = 120 * 4 print('Collecting data') songs_data = collect_data(song_ids) print('Data collected') print('Ranking songs') rank_songs(songs_data, tempo) print('Songs ranked') print('Training models') (bar_model, bar_alphabet) = BarLearner.train_model(songs_data) print str(bar_model), str(bar_alphabet) (note_models, note_alphabet) = NoteLearner.train_model(songs_data) print str(note_models), str(note_alphabet) (duration_model, duration_alphabet) = DurationLearner.train_model(songs_data, tempo) print str(duration_model), str(duration_alphabet) print('Models trained') print('Composing') compose(bar_model, note_models, duration_model, bar_alphabet, note_alphabet, duration_alphabet) print('Done composing') end = time.time() print('Total time: {}'.format(end - start))
def get_features(songArtist='', songTitle=''): ''' Returns a song's audio features, including: mode, tempo, key, duration, time signature, loudness, danceability, energy Returns the dictionary of song attributes, or an empty dictionary if there was an error ''' if songArtist == '' or songTitle == '': return {} #Searches for the song in pyechonest song_results = song.search(artist=songArtist, title=songTitle) if len(song_results) > 0: desiredSong = song_results[0] if desiredSong: #If song was found, return its attributes return desiredSong.audio_summary return {}
def echonest_search(styles, moods): """ performs the search on echonest with the given styles and moods """ cache_key = 'echo_%s_%s' % (styles, moods) result = cache.get(cache_key) if result: return result try: songs = song.search( style=styles, mood=moods, buckets=['id:%s' % BUCKET, 'tracks'], limit=True, results=100) foreign_ids = [s.cache['tracks'][0]['foreign_id'] for s in songs] keys = [str(f.split(':')[-1]) for f in foreign_ids] except: return [] cache.set(cache_key, keys, 600) return keys
def categorize_tweets_csv(): for tweetsfile in os.listdir(os.getcwd()): excitements = [] happy = 0 exclamations = 0 counter_num = 0 if tweetsfile.endswith(".csv"): print tweetsfile with open(tweetsfile, 'r') as csvfile: csvreader = csv.reader(csvfile) for tweet, sentiment, accuracy in csvreader: counter_num += 1 if sentiment == "positive" and accuracy >= 50: happy += 1 if tweet.count("!") > 1 and tweet.count(".") <= 1: exclamations += 1 exclamation_percentage = exclamations / float(counter_num) # excitement = (sum(excitements) + exclamations) / float(len(excitements)) happy /= float(counter_num) if exclamation_percentage > .15: if happy > .4: mood = "happy" else: mood = "angry" else: if happy > .4: mood = "relaxed" else: mood = "sad" rkp_results = song.search(mood=mood, min_energy=exclamation_percentage, artist_min_familiarity=.6, style="pop", artist_start_year_after="1999") resultant_song = rkp_results[0] print resultant_song.title + " - " + resultant_song.artist_name + " happy " + str( happy) + " excite " + str(exclamation_percentage)
def song_chooser(): from pyechonest import config from pyechonest import song config.ECHO_NEST_API_KEY = "VHLWI61NDUKAGF1AX" artist = input('Select an artist: ') title = input('Select a song: ') rkp_results = song.search(artist=artist, title=title) songs = rkp_results print "Please select a song: " counter = 0 for each in songs: print counter, ": ", each counter = counter + 1 key = input('Choose a number: ') song = songs[key] final_url = song.audio_summary["analysis_url"] print "\nNow Analyzing:" print song.title + " by " + song.artist_name return final_url, song.artist_name, song.title