Description
Barbara Ericson
SI 206—005
Final Project—Spotify API
Goals:
Goals Achieved:
Problems/Issues Encountered:
Another issue I encountered was when I was trying to authorize my client key and secret client key. Initially I was using Spotify’s own documentation on authorizing and the authorization flow. They even had a GitHub repository that included some examples on how to authorize. However, the code and examples that they offered were in Javascript or HTML, so while it was useful to gain an idea of what I was supposed to do, it did not help me actually authorize my keys. To solve the problem, I was able to find some documentation on a Python library called Spotipy which included instructions on how to authorize the client keys and conduct an album search.
Report:
The report can be viewed in the file named avgLengths.csv and it includes two columns (one with the album names and the other with the average length of the tracks for each album).
From the report, we can see that the album, “Home For Christmas”, by *NSYNC has the longest average track length of 4.01 minutes. The albums with the shortest average track length is “A Jolly Christmas From Frank Sinatra” by Frank Sinatra and “Elvis’ Christmas Album” by Elvis Presley—both albums have an average track length of 2.56 minutes.
From the visualization I created using Matplotlib, the viewer is able to easily see the albums’ average track lengths compared to each other. Again, the album, “Home For Christmas”, by *NSYNC has the longest average track length, while “A Jolly Christmas From Frank Sinatra” and “Elvis’ Christmas Album” are tied for the shortest average track length. Each album has a different color in order to differentiate them from each other which allows for an easier understood visualization.
Running the Code:
1. Download and save the file folder tianny_lu_final to your preferred directory and then open the folder.
2. Obtain your own Spotify Client Key and Client Secret Key. Open the python file spotify_info.py and fill in the the variables SPOTIFY_CLIENT_ID and SPOTIFY_CLIENT_SECRET with the appropriate strings.
3. On lines 129 and 150, please edit and change the directory path to match where the folder tianny_lu_final is on your device.
• For example, this is the code on the both lines 129 and 150: conn =
sqlite3.connect(‘/Users/tiannyylu/Desktop/206_programs/finalproject-tiannyylu/albums.sqlite’)
• Please edit change where it says “/Users/tiannyylu/Desktop/206_programs/ final-project-tiannyylu/“ to the correct directory on your device.
4. In Terminal, navigate to the directory where the folder tianny_lu_final is located and run the Python program final_project.py by using the command python final_project.py.
Code Documentation:
In the file final_project.py, documentation can be found in the form of docstrings for each function. Overall, the code takes in a list of album IDs and calls the function getSpotifyList in order to iterate through the list of album IDs and return a list of dictionaries. Within the function getSpotifyList, the function get_album is also called. The function get_album takes in the parameter, id, and calls a search for album using the album ID number. After receiving the results from the API search, it creates a new dictionary that includes the album name and a list of dictionaries for each track on the album.
The function addtoTable takes in the parameters spotifyList, conn, and cur.
The function checks to see if there is a table in the SQLite database called Albums, and if not, then it creates a table with the columns album, artists_name, track, track_number, and length_of_track. It will then iterate through the list of dictionaries, spotifyList, and adds each element to the appropriate column.
The function getTrackLengths takes in the parameters cur and spotifyList. Using cur, the function selects the length_of_track column for each album in spotifyList. Then, it converts the track length for every track in the album from milliseconds to minutes and adds the tracks length to a list, minLst. Using minLst, the function then finds the average length of the tracks for each album and adds to the dictionary avgDict with the album name as the key and the average track length as the value. From there, the function uses the avgDict to write to a CSV file that includes the album name in one column and the average track length for each album in another column.
Lastly, the function drawBarChart takes in the parameter avgDict and uses the keys and values of the dictionary to create a bar chart. The bar chart compares the average track length of every album and each album is represented by a different bar color. Resources:
12/4/18 – 12/6/18 Knowing what is https://
included in the objects of developer.spotify.com/ the various search documentation/web-api/ options reference/object-model/
From the page, I was able to see what information was available from each search method and decide what I wanted to include in my program.
12/4/18 – 12/10/18 Understanding what information is included in the album object and how to pull the exact data I wanted https://
developer.spotify.com/ documentation/web-api/ reference/albums/getalbum/
I found what objects I wanted to use and highlight in the program. In addition, I was able to see how the information was embedded and how
I can pull the exact information I wanted.
12/4/18 Understanding how to https:// I was able to find an easy
authorize the client key spotipy.readthedocs.io/ way to authorize my and search for the album en/latest/ client information and an
easy way to search for the albums using the .album( ) method.
12/3/18 Understanding how to receive a client key and how to authorize it https://
developer.spotify.com/ documentation/general/ guides/authorizationguide/
I was able to find how to create my own client key and tried to use their authorization flows in order to authorize my keys. However, the examples were in JavaScript and I decided to authorize the keys using Spotipy.
12/8/18 How to select only a https:// I was able to only select
specific column given a www.w3schools.com/ the length_of_track conditional sql/sql_where.asp column for every track in
the list of albums I had searched.
12/9/18-/12/12/18 How to wrap the axis https://
labels because they were stackoverflow.com/ running into each other questions/15740682/ wrapping-long-y-labelsin-matplotlib-tight-layoutusing-setp
This allowed me to understand how I can manipulate the x-axis labels by wrapping the words and make sure that the whole label is seen.
12/9/18-/12/12/18 How to wrap the axis https://matplotlib.org/ labels because they were gallery/ running into each other text_labels_and_annotati
ons/autowrap.html
12/10/18 The labels were being cut off because the size of the figure was not correct https://
stackoverflow.com/ questions/17109608/ change-figure-size-andfigure-format-inmatplotlib/17109830
I was able to increase the figure size to the correct portions so every label is easily seen and read.
Along with the link above, I was able to not only wrap the x-axis labels in order to ensure that they would not be cut off, but I was also able to angle the labels to ensure that they fit the figure size




Reviews
There are no reviews yet.