3

responses in pyen, a thin library for music data, returns dictionaries in this fashion:

{u'id': u'AR6SPRZ1187FB4958B', u'name': u'Wilco'}

I'm looping through and printing artists:

response = en.get('artist/search', artist_location='Chicago')

artists = response['artists']
for artist in artists:
   sys.stdout.write("song by {}\n".format(artist['name']))

but I'd like to pass a list of ids here:

  response = en.get('song/search', artist_ids = ?) //pass a list here?
  for song in response['songs']:
  sys.stdout.write("\t{}\n".format(song['title']))

Is this possible? How?

8-Bit Borges
  • 9,643
  • 29
  • 101
  • 198

2 Answers2

3

pyen is a very thin wrapper, you should always check the EchoNest API docs directly. According to the API documentation, the song/search endpoint does not accept multiple artist_ids.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
2

If you look at the Echo Nest API, you'll see that song search by artist_id doesn't support multiple params.

Thus, that's a restriction on pyen, as well, being a consumer of that API.

Instead, you'll have to print songs in a loop of requests:

artist_ids = ['AR54RGR1187FB51D10', 'AR6SPRZ1187FB4958B', 'AR5KAA01187FB5AEB7']
for artist_id in artist_ids:
    for song in en.get('song/search', artist_id=artist_id).get('songs', []):
        sys.stdout.write("\t{}\n".format(song['title']))
Steven Moseley
  • 15,871
  • 4
  • 39
  • 50
  • 1
    You should use another variable name instead of `id`, as it shadows the built-in function – Francisco Apr 21 '16 at 00:37
  • @Francisco strictly speaking yes, but I generally tend to agree with [this answer](http://stackoverflow.com/a/77925/771848). – alecxe Apr 21 '16 at 00:43
  • I tried it, but it raises an exception: `artist_id - Only one value allowed for parameter.` – 8-Bit Borges Apr 21 '16 at 01:30
  • @data_garden I'm pretty sure you didn't try it as intended. artist_ids should be a simple list of ids, e.g. `artist_ids = [1, 2, 3]` – Steven Moseley Apr 21 '16 at 01:34
  • @Steven Moseley you mean: `artist_ids = ['AR54RGR1187FB51D10', 'AR6SPRZ1187FB4958B', 'AR5KAA01187FB5AEB7']`, so you'd have to output a list of `ids` beforehand. please edit this and I'll accept your answer. – 8-Bit Borges Apr 21 '16 at 01:47