knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
Note: All OS tests will sometimes pass or fail when CI is run and we can't figure out why. We believe that sometimes tests will run on a remote server in a location where the API is region locked, causing the tests to fail.
Wrappify is an API wrapper for the Spotify API in R. Currently, there is functionality only for the API functions which do not require access to an individuals data.
You can install the released version of wrappify from CRAN with:
install.packages("wrappify")
And the development version from GitHub with:
# install.packages("devtools") devtools::install_github("lukavuko/wrappify")
library(wrappify)
This is a basic example that shows the intended workflow of wrappify.
First, we have an artist we wish to know more about. That artist is the essential alternative metal band "Ghost". We query getArtistInfo.
ghost <- getArtistInfo("Ghost", byName = TRUE) ghost
We see that Ghost is the second artist in the list. We pull out the Spotify ID and use it to get Ghost's top songs.
id <- ghost[2,]$id ghostsongs <- getTopSongs(id) ghostsongs
We are interested in knowing more about the song "Dance Macabre", so we pull out the song ID and use it to generate a graph of audio features.
dance <- ghostsongs[1,]$id getAudioFeatures(dance, output = "graph")
We learn that we really like Ghost, and want to get some similar artists.
getRelatedArtists(id)
And now we can choose one of these artists that look interesting and learn more about them as well.
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.
Note that artist and track names do not need to be formatted or spelled correctly to yield valid search results. We have a list of artists we want to use in other functions:
# 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 you can also include artist names to narrow down what track you're looking for!
# 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')
We may also be interested in searching for new music based on certain song metrics (danceability, energy, valence, etc.), artist styles, genre tags, and even songs themselves. For this we have the getTrackRecommendations()
function.
Artist to ID conversion is built into the getTrackRecommendations
function
so we can directly type names into the function.
Track to ID conversion is not yet implemented so we will use track seeds from earlier to find new song recommendations. There seem to be some issues with track seeds as vectors so we'll use only single tracks as our seed.
Genres can be types as a vector if wanting multiple genres. Most genre tags you can think of exist in the Spotify API search, but in case there's no return try using other genre names.
# I want more songs like 'Higher Love' by Kygo and Whitney Houston getTrackRecommendations(seed_artists = c('kygo', 'whitney houston'), seed_genres = c('tropical house', 'edm'), seed_tracks = '6oJ6le65B3SEqPwMRNXWjY')
Say we aren't satisfied with our list. We can use other parameters to better guide Spotify's recommendation API like so:
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)
For user interest, a pie chart is provided to view what proportion of returned content is explicit. In the future we would like to integrate optional plot parameters, and other optional plots that can highlight song popularity and general genre music metrics such as their energy, acousticness, liveness, and so forth.
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)
If we would prefer information on podcasts, we can use wrappify as well.
Podcasts, like songs and artists, are uniquely identified by their Spotify ID. We can convert key words into this ID using getPodcastID.
getPodcastID('conspiracy theory')
A list of recent episodes can be generated using this ID.
getRecentEpisodes('5RdShpOtxKO3ZWohR2M6Sv', limit=5)
If we only have 30 minutes to spare, the duration filter can be used.
getRecentEpisodes('5RdShpOtxKO3ZWohR2M6Sv', duration = 30, limit=5)
We can also check out some basic stats. Right now, duration of episodes can be plotted over time. However, more graphical functionality is planned in the future.
getBasicStats('5RdShpOtxKO3ZWohR2M6Sv')
searchForPodcast
can be used to find a new show. How about a child-friendly Spanish podcast on history? Type in history
, change the language to es
and change explicit
to FALSE
.
searchForPodcast('history', language = 'es', market = 'ES', explicit = FALSE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.