Skip to content

3. APIs

Akshay Chacko edited this page Apr 20, 2021 · 34 revisions

Get New Recommendations

Returns a list of song recommendations and relevant track information based on the Spotify song recommendation API. First we call the Get Audio Features for a Track or Get Audio Features for Several Tracks Spotify endpoints depending on if we are starting with a single track or a playlist. This information is then passed to the Get Recommendations API to get a list of candidate recommendations. Finally, this data is turned into a feature vector and then we use k-nearest neighbors to choose the most suitable songs.

There are two main endpoints that the user can call, depending on if they provide a single track or a playlist. Both of these endpoints return the same output, which is a JSON list of recommendations, to be displayed in the front-end. Our endpoints combine the data from multiple API calls and then run the k-nearest neighbors algorithm on that data, before returning the recommendations.

Endpoint: GET /getsong/ link to source code

Request Parameters

Key Location Type Description
track_id User Input String Spotify ID of user seeded track

Response Codes

Code Description
200 OK Success
400 Bad Request Invalid parameters

Returns

Key Location Type Description
result JSON Song feature data Returns song feature data of user provided song

Endpoint: GET /read_playlist/ link to source code

Request Parameters

Key Location Type Description
playlist_id User Input String Spotify ID of user seeded playlist

Response Codes

Code Description
200 OK Success
400 Bad Request Invalid parameters

Returns

Key Location Type Description
result JSON List of Spotify Tracks Takes all the recommended tracks and returns them with their corresponding metadata

Example

curl -b cookies.txt -c cookies.txt -X GET https://OUR_SERVER/read_playlist/ or https://OUR_SERVER/getsong/

{
    "result": [
        {
            "track_name": "Blow - Original Mix",
            "artist": "Marcelo Vasami",
            "track_id": "3KQne67zgRUZmboJO3LlH6",
            "artist_id": "4bsJBOidwLc1UbwPoKbF44",
            "preview_url": "https://p.scdn.co/mp3-preview/b3b84151abffdfb5eea1bc736d58577a21564f39?cid=7a03c05c75c04958926b5213cda242f3",
            "image_url": "https://i.scdn.co/image/ab67616d0000b2730a049795c739a3ebf2c59586",
            "tempo": 121.995,
            "energy": 0.507,
            "key": 7,
            "danceability": 0.806,
            "valence": 0.361
        },
        {
            "track_name": "I Want Your Love - Vocal Duke Mix",
            "artist": "Gutterstylz",
            "track_id": "41D5mxhp22Fjn7UWc8rYsb",
            "artist_id": "3xO2cljpynygqFjaB4freO",
            "preview_url": "https://p.scdn.co/mp3-preview/388f26c4a72d78272d61c1440c38a29c3988f6f9?cid=7a03c05c75c04958926b5213cda242f3",
            "image_url": "https://i.scdn.co/image/ab67616d0000b273295feb3acb50d64352863125",
            "tempo": 123.001,
            "energy": 0.761,
            "key": 5,
            "danceability": 0.803,
            "valence": 0.236
        }
    ]
}

Third-Party SDKs

Our front end is developed in Kotlin with Android Studio IDE and we will be using Spotify's Web API.

Spotify Web API

We use Spotify's Web API on the front end for numerous purposes that provide the core functionality of the app. The Spotify API is called to get information about the user-seeded song, and then it is used to get information (ie. BPM/key) to determine which songs to recommend. Lastly, the API is used again to save the recommendations to a playlist in the users account. We are making use of the spotipy library for python to act as an interface to the Spotify API.

Authorization

Spotify's Web API requires authorization before we make any requests. We will be following Client Credentials Flow.

Song Info and Audio Features

When a song is being recommended, we show the name of the song, the name of the song's artist, the title of the album containing the song, the image (album cover) for that album, and a 30-second preview of the song with Spotify's Get a Track endpoint. In addition, we will also display the song's audio features such as danceability, energy, and loudness with Spotify's Get Audio Features for a Track endpoint.

Seeding the Recommender

A user provides the link to whichever song they want to use to seed the recommender. We then get information on the provided song's audio features such as danceability, energy, and loudness with Spotify's Get Audio Features for a Track endpoint. We then use this data to determine how the song recommendations will be calculated.

Clone this wiki locally