Skip to content

Repository files navigation

all-ears

Access the Spotify API to rate and save music.

In my application, I worked with the Spotify API to create a serverless program where users can rate music, search and browse artists, genres, and albums, and create music folders / playlists. I created a lambda function (spotify_api_connect) which holds my Spotify client id and client secret as environment variables. When accessing the Spotify API, I opted not to have clients sign in to Spotify as for the functions I wanted to perform, this information was not necessary. Therefore, I followed Spotify’s client credentials flow used in server-server authentication, where my program cannot access user information. When users sign in the client side application, they are directed to an /auth POST path within my REST API Gateway. However, they are also directed to an /access_token GET path where a spotify API access token is retrieved for the user. This token is necessary in all calls to the Spotify API. My functions are built in lambda and the client accesses them through. The API Gateway acts as a web server through which a client can send requests. As an example, the function “search” allows the user to input a parameter like a “genre”, “artist”, or “track” and a search query like “rock”, “Rihanna”, or “Cigarettes and Coffee.” The API Gateway directs this call to my search_music lambda function where I use the user authenticated spotify access_token to access the Spotify API given the search query. Some search parameters like genre requires a different kind of API call, so the lambda function handles calling the API correctly to get x amount of tracks of albums back and format them consistently between the different kinds of calls. I did not utilize S3 in my program, but it might be useful if I were to expand my program as S3 could hold txt files with album content, user profile pictures, and more. In my functions, I mainly use POST and GET methods in the API Gateway. My Lambda functions either interact with the Spotify API, RDS database, or both in order to extract or manipulate the data necessary. I originally built my RDS database with 10 tables: users, tokens, ratings, comments, folders, like_comments, like_ratings, like_folders, followers, folder_music. Due to time constraints, I wasn't able to implement the functions that would utilize all of these tables. My program ended up using 5 tables: users to hold user information including email, first and last name, username, and password; tokens to hold user authentication tokens to keep some data private; ratings to store user ratings including user id, a number out of 5 stars, a comment, and the corresponding Spotify API track id; folders to store the folder’s name and user id of the owner of a folder, while the songs within the folder were held in folder_music which held the folder id and the Spotify API track id of a song within the folder. I hold the Spotify API track id in some databases as I can later access it in lambda functions like: “open_folder”, “get_ratings”, and “user_stats_allears”, in order to call back to the Spotify API to get and display relevant information like track names and artists. My main computational functions are these “open_folder”, “get_ratings”, and “user_stats_all_ears” functions in lambda. The open folder function retrieves all of a user’s folders and displays them (using a separate get_folders function), where a user can then choose a folder based on its index in the list. The folder id is retrieved from the get_folders function and inputted as a path parameter along with the spotify access token in a get request in a lambda function. The lambda function accesses the corresponding music ids in the folder from the folder_music table and then sends each music id to the Spotify API to get relevant information to later display to the client. Similarly, the get ratings function uses the user token as a header and the spotify access token as a path parameter to authorize the user and issue a get request to the Spotify API for each of the user’s ratings’ music ids from the ratings table. Relevant information like comments, number of stars, artists, and tracks is then processed and returned. Finally, the user stats function retrieves the user token as a header and the spotify access token as a path parameter to authorize the user and issue a get request to the Spotify API for each of the user’s ratings from the ratings table. This information is processed to compute the user’s top genres, artists, albums, and tracks and their average rating which is then returned to the client.

About

Access the Spotify API to rate and save music.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages