Spotify API - What a person is listening to?

Discussion in 'Programming' started by Aderm, Sep 30, 2017.

Thread Status:
Not open for further replies.
  1. So I've seen @JamesJ's hot website (https://jamesj.me/listening)

    I'm wondering how this is done (James maybe you can explain?), as I cannot find this in the spotify api anywhere.

    Anyone had experience with the API?

    Thanks.
     
  2. JamesJ

    Supporter

    ;)


    So using some NodeJS, I've got an API at https://jamesj.me/api/listening, which does a background API call to the Spotify API server's, which parses it into the names, links, formatting, etc.

    So this is used to get an access token, Spotify uses an OAuth system, so you're given a "refresh" token, which is then used to generate an access token, which will last for 2 hours (I believe).
    [​IMG]
    1. This is the refresh token
    2. This is the application secret (you get this from the Spotify developers website)
    Here I am calling the authorization route, and getting a token back.
    ('access_token' in the JSON response).



    Then, requesting to "https://api.spotify.com/v1/me/player/currently-playing" with your access token (as a Bearer token) you get a response like,
    Code (Text):

    {   "timestamp" : 1506791822478,   "progress_ms" : 5785,   "is_playing" : true,   "item" : {     "album" : { "album_type" : "album", "artists" : [ { "external_urls" : { "spotify" : "https://open.spotify.com/artist/64M6ah0SkkRsnPGtGiRAbb" }, "href" : "https://api.spotify.com/v1/artists/64M6ah0SkkRsnPGtGiRAbb", "id" : "64M6ah0SkkRsnPGtGiRAbb", "name" : "Bebe Rexha", "type" : "artist", "uri" : "spotify:artist:64M6ah0SkkRsnPGtGiRAbb" } ], "available_markets" : [ "AD", "AR", "AT", "AU", "BE", "BG", "BO", "BR", "CA", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "EC", "EE", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HU", "ID", "IE", "IS", "IT", "JP", "LI", "LT", "LU", "LV", "MC", "MT", "MX", "MY", "NI", "NL", "NO", "NZ", "PA", "PE", "PH", "PL", "PT", "PY", "SE", "SG", "SK", "SV", "TR", "TW", "US", "UY" ], "external_urls" : { "spotify" : "https://open.spotify.com/album/6t5D6LEgHxqUVOxJItkzfb" }, "href" : "https://api.spotify.com/v1/albums/6t5D6LEgHxqUVOxJItkzfb", "id" : "6t5D6LEgHxqUVOxJItkzfb", "images" : [ { "height" : 640, "url" : "https://i.scdn.co/image/ba99c03404941dc54d0340da28e368fc7457e22a", "width" : 640 }, { "height" : 300, "url" : "https://i.scdn.co/image/c89ea26344e4eb73c641e9520d1df684c30722de", "width" : 300 }, { "height" : 64, "url" : "https://i.scdn.co/image/d0a9b0370891fe68f68467ea32cb60a35e346bf5", "width" : 64 } ], "name" : "All Your Fault: Pt. 2", "type" : "album", "uri" : "spotify:album:6t5D6LEgHxqUVOxJItkzfb" }, "artists" : [ { "external_urls" : { "spotify" : "https://open.spotify.com/artist/64M6ah0SkkRsnPGtGiRAbb" }, "href" : "https://api.spotify.com/v1/artists/64M6ah0SkkRsnPGtGiRAbb", "id" : "64M6ah0SkkRsnPGtGiRAbb", "name" : "Bebe Rexha", "type" : "artist", "uri" : "spotify:artist:64M6ah0SkkRsnPGtGiRAbb" } ], "available_markets" : [ "AD", "AR", "AT", "AU", "BE", "BG", "BO", "BR", "CA", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "EC", "EE", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HU", "ID", "IE", "IS", "IT", "JP", "LI", "LT", "LU", "LV", "MC", "MT", "MX", "MY", "NI", "NL", "NO", "NZ", "PA", "PE", "PH", "PL", "PT", "PY", "SE", "SG", "SK", "SV", "TR", "TW", "US", "UY" ], "disc_number" : 1, "duration_ms" : 181674, "explicit" : false, "external_ids" : { "isrc" : "USWB11701179" }, "external_urls" : { "spotify" : "https://open.spotify.com/track/22BH6K8RlpwNzIINvWS4Jj" }, "href" : "https://api.spotify.com/v1/tracks/22BH6K8RlpwNzIINvWS4Jj", "id" : "22BH6K8RlpwNzIINvWS4Jj", "name" : "(Not) The One", "popularity" : 79, "preview_url" : "https://p.scdn.co/mp3-preview/bc8d1512f6958c22430d6bbcb233ce3f6b9267cc?cid=8897482848704f2a8f8d7c79726a70d4", "track_number" : 4, "type" : "track", "uri" : "spotify:track:22BH6K8RlpwNzIINvWS4Jj" },   "context" : {     "external_urls" : { "spotify" : "http://open.spotify.com/user/itsjamesj/playlist/5KWNqswgNU37mZm0HJKsFc" }, "href" : "https://api.spotify.com/v1/users/itsjamesj/playlists/5KWNqswgNU37mZm0HJKsFc", "type" : "playlist", "uri" : "spotify:user:itsjamesj:playlist:5KWNqswgNU37mZm0HJKsFc" } }
     
    I'd recommend slapping that into http://unminify.com to make that readable.
    Then, parse the JSON, get the
    Code (Text):
    item
    root, then:
    artists - an array of artist objects
    - artists.name = name
    - artists.external_urls.spotify = their Spotify link (open.spotify.com)
    album - the album object
    - album.images.url - the url (
    Code (Text):
    scdn.co
    ) to the cover photo
    - album.images.height/width - the image sizes

    id - the song URI (will be in the form of a ID, without the open.spotify.com/track/

    And that's it.

    There's some documentation on getting the user's current track here: https://developer.spotify.com/web-api/get-the-users-currently-playing-track/
    Authorization is here: https://developer.spotify.com/web-api/authorization-guide/
    Your applications: https://developer.spotify.com/my-applications/
    General user guide: https://developer.spotify.com/web-api/user-guide/

    And most importantly, for playing around with at first, the API Console - https://developer.spotify.com/web-api/console/

    There may be some wrappers online somewhere, when I made it I wasn't too fussed so just used some Node. The Spotify API is quite powerful, so if you get the chance to play around with it, I'd certainly recommend it :)
     
    • Informative Informative x 2
    • Like Like x 1
  3. @JamesJ you're a babe.

    Locked.
     
Thread Status:
Not open for further replies.

Share This Page