def print_results(): """ This function prints out the recommended song (name,artist) with their hotttnesss ranking according to the EchoNest API """ with open("result.txt", 'r') as file: for line in file: list = line.strip().split() for song_id in list: try: s = song.Song(str(song_id)) print("%s | %s - %s" % (s.song_hotttnesss, s, s.artist_name)) except Exception as exception: print( "Exception : {}. Can't get information about song {}". format(exception, song_id)) continue
def get_track_features(start, stop): for num, track_info in enumerate(track_list[start:stop]): if num % 20 == 0: time.sleep(15) trk_id = track_info[0] year, song_id, artist, track_name = track_info[1] info_dict = dict() info_dict['year'] = year info_dict['artist'] = artist info_dict['track_name'] = track_name info_dict['track_id'] = trk_id info_dict['song_id'] = song_id tester = 0 try: trk = track.track_from_id(trk_id) info_dict['track_acousticness'] = trk.acousticness info_dict['track_danceability'] = trk.danceability info_dict['track_duration'] = trk.duration info_dict['track_energy'] = trk.energy info_dict['track_instrumentalness'] = trk.instrumentalness info_dict['track_key'] = trk.key info_dict['track_liveness'] = trk.liveness info_dict['track_loudness'] = trk.loudness info_dict['track_mode'] = trk.mode info_dict['track_speechiness'] = trk.speechiness info_dict['track_tempo'] = trk.tempo info_dict['track_time_signature'] = trk.time_signature info_dict['track_valence'] = trk.valence except: print num, ' - track fail' tester += 1 try: sawng = song.Song(song_id) location = sawng.get_artist_location() summary = sawng.get_audio_summary() # location info_dict['latitude'] = location['latitude'] info_dict['longitude'] = location['longitude'] info_dict['city'] = location['location'] # audio summary info_dict['song_danceability'] = summary['danceability'] info_dict['song_duration'] = summary['duration'] info_dict['song_acousticness'] = summary['acousticness'] info_dict['analysis_url'] = summary['analysis_url'] info_dict['song_energy'] = summary['energy'] info_dict['song_instrumentalness'] = summary['instrumentalness'] info_dict['song_key'] = summary['key'] info_dict['song_liveness'] = summary['liveness'] info_dict['song_loudness'] = summary['loudness'] info_dict['song_mode'] = summary['mode'] info_dict['speechiness'] = summary['speechiness'] info_dict['song_tempo'] = summary['tempo'] info_dict['song_time_signature'] = summary['time_signature'] info_dict['song_valence'] = summary['valence'] # hotness, familiarity, and type info_dict['song_type'] = sawng.get_song_type() info_dict['artist_familiarity'] = sawng.get_artist_familiarity() info_dict['artist_hotttnesss'] = sawng.get_artist_hotttnesss() info_dict['song_currency'] = sawng.get_song_currency() info_dict['song_hotttnesss'] = sawng.get_song_hotttnesss() info_dict['artist_id'] = str(sawng.artist_id) except: print num, ' - song fail' tester += 1 if tester < 2: trackDict[trk_id] = info_dict else: pass
# Uncomment to set the API key explicitly. Otherwise Pyechonest will # look in the ECHO_NEST_API_KEY environment variable for the key. #from pyechonest import config #config.ECHO_NEST_API_KEY='YOUR API KEY' from pyechonest import playlist, song kwstr = song.Song('SOSGSJB12A8C13B2D4') dptec = song.Song('SOOSADY12A6701F119') jwcb = song.Song('SOCEGKM12A58A7F30D') a_playlist = playlist.static(type='song-radio', song_id=[kwstr.id, dptec.id, jwcb.id], results=5) other_playlist = playlist.static(type='song-radio', song_id=[kwstr.id, dptec.id, jwcb.id], results=5) print 'Songs in one playlist for seed songs "%s":' % ('," "'.join( a_song.title for a_song in [kwstr, dptec, jwcb]), ) for a_song in a_playlist: print '"%s" by %s' % (a_song.title, a_song.artist_name) print '' print 'Songs in another playlist for seed songs "%s":' % ('," "'.join( a_song.title for a_song in [kwstr, dptec, jwcb]), ) for a_song in other_playlist: print '"%s" by %s' % (a_song.title, a_song.artist_name)
def convert_one_song(audiofile, output, mbconnect=None, verbose=0, DESTROYAUDIO=False): """ PRINCIPAL FUNCTION Converts one given audio file to hdf5 format (saved in 'output') by uploading it to The Echo Nest API INPUT audiofile - path to a typical audio file (wav, mp3, ...) output - nonexisting hdf5 path mbconnect - if not None, open connection to musicbrainz server verbose - if >0 display more information DESTROYAUDIO - Careful! deletes audio file if everything went well RETURN 1 if we think a song is created, 0 otherwise """ # inputs + sanity checks if not os.path.exists(audiofile): print(('ERROR: song file does not exist:', songfile)) return 0 if os.path.exists(output): print(('ERROR: hdf5 output file already exist:', output, ', delete or choose new path')) return 0 # get EN track / song / artist for that song if verbose > 0: print(('get analysis for file:', audiofile)) track = trackEN.track_from_filename(audiofile) song_id = track.song_id song = songEN.Song(song_id) if verbose > 0: print(('found song:', song.title, '(', song_id, ')')) artist_id = song.artist_id artist = artistEN.Artist(artist_id) if verbose > 0: print(('found artist:', artist.name, '(', artist_id, ')')) # hack to fill missing values try: track.foreign_id except AttributeError: track.__setattr__('foreign_id', '') if verbose > 0: print('no track foreign_id found') try: track.foreign_release_id except AttributeError: track.__setattr__('foreign_release_id', '') if verbose > 0: print('no track foreign_release_id found') # create HDF5 file if verbose > 0: print(('create HDF5 file:', output)) HDF5.create_song_file(output, force=False) # fill hdf5 file from track if verbose > 0: if mbconnect is None: print('fill HDF5 file with info from track/song/artist') else: print( 'fill HDF5 file with info from track/song/artist/musicbrainz') h5 = HDF5.open_h5_file_append(output) HDF5.fill_hdf5_from_artist(h5, artist) HDF5.fill_hdf5_from_song(h5, song) HDF5.fill_hdf5_from_track(h5, track) if not mbconnect is None: HDF5.fill_hdf5_from_musicbrainz(h5, mbconnect) h5.close() # done if DESTROYAUDIO: if verbose > 0: print(('We remove audio file:', audiofile)) os.remove(audiofile) return 1