getting the genre of a song in a playlist

I'm currently using the very cool app Exportify (https://github.com/watsonbox/exportify) to export Spotify playlists to CSV.

My javascript knowledge is terrible but I'm currently trying to include the genre of each track, and I'm having trouble retrieving it.

I'm assuming that:

  • I have to get the genre through the artist.
  • The playlist API returns a simple artist object that can only get me the ID, not the genre (https://developer.spotify.com/web-api/get-playlists-tracks/)
  • That to get the genre I'd have to do a further lookup of the artist using the artist ID (https://developer.spotify.com/web-api/object-model/#artist-object-full)
  • I can get the artist ID by adding "item.track.artists.map(function(artist) { return artist.id }).join(', ')," to the code below in the "exportify.js" file.

          var tracks = responses.map(function(response) {
        return response.items.map(function(item) {
          return [
            item.track.uri,
            item.track.id,
            item.track.name,
            item.track.artists.map(function(artist) { return artist.name }).join(', '),
            item.track.artists.map(function(artist) { return artist.id }).join(', '),
            item.track.album.name,
            item.track.disc_number,
            item.track.track_number,
            item.track.duration_ms,
            item.added_by == null ? '' : item.added_by.uri,
            item.added_at
          ].map(function(track) { return '"' + track + '"'; })
        });
      });
    

    Can anyone tell me how I can then get the artist genre based on the artist ID and add it as another column?

    Thanks

    Chris


    Use the "Get an Artist" endpoint in Spotify:

    https://developer.spotify.com/web-api/get-artist/

    Calling this endpoint will get you the genres of an artist given his artist ID.


    As Potray said, you can connect to the endpoint getArtist. More specifically, the JSON returned will have a genre key. The genre key directly points to an array of genres.

    response.genres will access this array.

    You can test this out at RapidAPI here. I've specifically linked you to the getArtist endpoint. Here you can fill in the artist_id, click test, and see a sample response. RapidAPI will also generate a code snippet so you can just copy and paste the API call directly into your own code.

    Here I'm testing out the getArtist endpoint with the Red Hot Chili Peppers artist_id "0L8ExT028jH3ddEcZwqJJ5":

    在这里输入图像描述

    You'll notice that the genre array contains 5 items: [ 'alternative rock', 'funk metal', 'funk rock', 'permanent wave', 'rock' ]

    If you click CODE directly about the response on the right and sign in, you'll be able to access the code snippet, and paste it directly into your code. An example would be:

    在这里输入图像描述

    链接地址: http://www.djcxy.com/p/55590.html

    上一篇: 重新排序播放列表

    下一篇: 在播放列表中获取歌曲的流派