Skip to content

Instantly share code, notes, and snippets.

@hsarret
Last active August 29, 2015 13:57
Show Gist options
  • Save hsarret/9509322 to your computer and use it in GitHub Desktop.
Save hsarret/9509322 to your computer and use it in GitHub Desktop.
Echo Nest sample: Create a catalog and get a playlist restricted to this catalog
#!/usr/bin/env python
# encoding: utf-8
from pyechonest import config, catalog, playlist
import hashlib
import time
def main():
# use your Echo Nest API key here
config.ECHO_NEST_API_KEY = ''
if config.ECHO_NEST_API_KEY != '':
# create your catalog
myCatalog = catalog.Catalog( 'MyCatalog', 'song' )
# populate catalog
myCatalogDefinition = [
{ 'action': 'update', 'item':{ 'artist_name': 'L7', 'release': 'Hungry For Stink', 'song_name': 'Andres', 'item_id': hashlib.md5( 'L7-HungryForStink-Andres' ).hexdigest() } },
{ 'action': 'update', 'item':{ 'artist_name': 'The Cure', 'release': 'Seventeen Seconds', 'song_name': 'A Forest', 'item_id': hashlib.md5( 'TheCure-SeventeenSeconds-AForest' ).hexdigest() } },
]
# update catalog
ticket = myCatalog.update( myCatalogDefinition )
# wait for completion
status = 'incomplete'
while status != 'complete' and status != 'error':
status = myCatalog.status( ticket )[ 'ticket_status' ]
time.sleep(3)
songs = myCatalog.get_item_dicts( results = myCatalog.profile[ 'total' ] )
print 'Catalog "%s" contains %d song(s)' % ( myCatalog.name, len( songs ) )
for currentSong in songs:
print '\t%s' % currentSong
if len( songs ) > 0:
myPlaylist = playlist.basic( type = 'song-radio', song_id = songs[ 0 ][ 'song_id' ], buckets = 'id:%s' % myCatalog.id, limit = True )
print "Playlist"
for currentSong in myPlaylist:
print "\t%s" % currentSong
else:
print 'Use your Echo Nest API key'
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment