knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(wrappify)
Spotify has a ID code associated with each object in its database which acts as a unique identifier. Human beings, however, know their favourite artists, tracks, and podcasts by name, not abstract IDs. As such, functionality is needed to convert names into IDs for use in other functions.
Artist and track names do not need to be formatted or spelled correctly to yield valid search results. Spotify will make an educated guess at the best match.
# One artist, mispelled getArtistID('sanTanana') # Multiple artists, seamlessly artists_of_interest <- c('alt j', 'vulfpeck', 'herbie hancock0') do.call(rbind, lapply(artists_of_interest, getArtistID))
When searching for tracks, artist names can be included to hone in on a specific track.
# Song limit defaults to 5 getTrackID('Love') # User can increase limits getTrackID('Love', limit = 10) # User can search with artist name in the string for more precise results getTrackID('Love whitney houston')
Information on an artist can be found using their Spotify ID.
getArtistInfo("1Qp56T7n950O3EGMsSl81D", dataframe = T)
If the ID is not known, the artist name can be used as a search query. This will return the closest matches, out of which the desired artist can be chosen.
getArtistInfo("Ghost", byName = TRUE, dataframe = T, lim = 7)
Ghost is the second artist in this list. The ID can be pulled out of the dataframe for the desired artist and used to query other functions. The artist's top songs can be found using getTopSongs.
ghostid <- getArtistInfo("Ghost", byName = TRUE, dataframe = T, lim = 7)[2,]$id topsongs <- getTopSongs(ghostid) topsongs
This can also be shown in a graphical format.
getTopSongs("1Qp56T7n950O3EGMsSl81D", output = "graph")
If more information is desired for a specific song, the getSongInfo function can be used.
getSongInfo(topsongs[1,]$id, byName = F, dataframe = T)
Similar artists can be found as well.
similar_to_ghost <- getRelatedArtists(artistId = ghostid, dataframe = TRUE) similar_to_ghost
Finally, information on the audio features for specific tracks can be found as well. Definitions of the audio features can be found here.
getAudioFeatures(topsongs[1,]$id, output = "graph")
New music can be searched for based on audio features, artist styles, genre tags, or other songs. This is done using the getTrackRecommendations()
function.
Artist to ID conversion is built into the getTrackRecommendations
function, which can be queried directly using artist names.
Track to ID conversion is not yet implemented, but genre seeds and track IDs can be used as queries. There are some issues with track seeds as vectors so only single tracks can be used as a seed (for now).
Multiple genres can be seeded at once. Spotify has a massive collection of available genre tags, and most genres a user could imagine will be viable tags. If no recommendations are returned, the user may need to use less restrictive queries.
# Search for songs similar to 'Higher Love' by Kygo and Whitney Houston getTrackRecommendations(seed_artists = c('kygo', 'whitney houston'), seed_genres = c('tropical house', 'edm'), seed_tracks = '6oJ6le65B3SEqPwMRNXWjY')
If this list is not satisfactory, we can use other parameters to better guide Spotify's recommendation API.
getTrackRecommendations(seed_artists = c('kygo', 'whitney houston'), seed_genres = c('tropical house', 'edm'), seed_tracks = '6oJ6le65B3SEqPwMRNXWjY', limit = 12, market = 'US', min_popularity = 70, target_valence = 1)
getTrackRecommendations(seed_artists = c('kygo', 'whitney houston'), seed_genres = c('tropical house', 'edm'), seed_tracks = '6oJ6le65B3SEqPwMRNXWjY', limit = 12, market = 'US', target_energy = 0.8, target_danceability = 1, target_valence = 1)
Podcasts can be searched for by querying the getPodcastID function with show names, authors, or studios. Misspelled words are handled directly in the function.
getPodcastID('philosophise this, stephen west')
The shows ID can be used to check out its latest release using getRecentEpisodes. We can also check out a description of each episode using getEpisodeInformation.
getRecentEpisodes('2Shpxw7dPoxRJCdfFXTWLE') getEpisodeInformation('5fE0bPigcmwSrrYxjJD4Sv')
New podcasts can be discovered using searchForPodcast. Key words and genre tags can be used in the query, and a parameter can be set to filter out explicit shows.
searchForPodcast('history', explicit = FALSE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.