Skip to content

Instantly share code, notes, and snippets.

@dasevilla
Created January 10, 2014 00:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dasevilla/8344911 to your computer and use it in GitHub Desktop.
Save dasevilla/8344911 to your computer and use it in GitHub Desktop.
Example of using The Echo Nest for bulk catalog matching to Rdio. This example depends on http://echonest.github.io/pyechonest/
import logging
import time
from pyechonest import config, catalog
config.ECHO_NEST_API_KEY = 'YOUR_API_KEY'
ECHO_NEST_SLEEP_LENGTH = 10 # seconds
def update_taste_profile(taste_profile):
tracks = [
('my-id-1', 'weezer', 'in the garage'), # In Rdio catalog
('my-id-2', 'led zepplin', 'whole lota love'), # Not in Rdio catalog
('my-id-3', 'fake artist', 'fake track'), # Unknown track
]
actions = []
for track_id, artist, title in tracks:
actions.append({
'action': 'update',
'item': {
'item_id': track_id,
'artist_name': artist,
'song_name': title,
}
})
ticket = taste_profile.update(actions)
logging.info('Updated catalog and got ticket "%s"', ticket)
return ticket
def check_ticket(taste_profile, ticket):
"""Polls taste profile ticket and blocks until it's finished."""
completed = False
while not completed:
time.sleep(ECHO_NEST_SLEEP_LENGTH)
catalog_status = taste_profile.status(ticket)
completed = catalog_status['ticket_status'] == 'complete'
logging.info('Catalog ticket %s, status %s', ticket,
catalog_status['ticket_status'])
def dump_taste_profile(taste_profile):
found_tracks = []
items = taste_profile.get_item_dicts(buckets=['tracks', 'id:rdio-US', 'id:whosampled'])
for item in items:
status = 'Unable to identify'
rdio_track_keys = []
my_song_id = item['request']['item_id']
my_artist_name = item['request']['artist_name']
my_song_name = item['request']['song_name']
en_song_id = None
en_artist_name = None
en_song_name = None
# If `song_id` exists, Echo Nest Found a match
if 'song_id' in item:
en_song_id = item['song_id']
en_artist_name = item['artist_name']
en_song_name = item['song_name']
if len(item['tracks']):
rdio_track_keys = [extract_rdio_key(track['foreign_id']) for track in item['tracks'] if track['catalog'] == 'rdio-US']
status = 'Found Rdio match'
else:
status = 'Identified, no Rdio match'
print my_song_id, my_artist_name, my_song_name
print status
print 'Rdio keys:', ', '.join(rdio_track_keys)
print
found_tracks.append({
'my_metadata': {
'id': my_song_id,
'artist': my_artist_name,
'song': my_song_name
},
'echo_nest_metadata': {
'id': en_song_id,
'artist': en_artist_name,
'song': en_song_name
},
'rdio_keys': rdio_track_keys,
'status': status,
})
return found_tracks
def extract_rdio_key(foreign_id):
"""Takes a key of the form ``rdio-US:track:t2840616`` and extracts ``t2840616``"""
rdio_catalog, key_type, rdio_key = foreign_id.split(':')
return rdio_key
def main():
logging.basicConfig(level=logging.INFO)
taste_profile = catalog.Catalog('sample-taste-profile', type='song')
ticket = update_taste_profile(taste_profile)
check_ticket(taste_profile, ticket)
found_tracks = dump_taste_profile(taste_profile)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment