#' Parse artist object
#'
#' Helper function to parse useful fields from the artist object.
#'
#' \itemize{
#' \item artist name
#' \item arist popularity score, 0-100, generated by Spotify
#' \item genres
#' \item total followers
#' \item artist id
#' \item popularity score, 0-100, generated by Spotify
#' \item release date
#' \item genres
#' \item album id
#' }
#' @param art_obj A json-like list, artist object returned by Spotify API
#'
#' @return
#' @export
#'
#' @examples
parseArtist <- function(art_obj)
{
tibble::tibble(
name = art_obj[["name"]],
popularity = art_obj[["popularity"]],
genres = paste(art_obj[["genres"]], collapse = ", "),
followers = art_obj$followers$total,
id = art_obj[["id"]],
url = art_obj[['external_urls']][['spotify']]
)
}
#' Get Spotify catalog information for a single artist
#'
#'
#'Function to get Spotify catalog information for a single artist identified by their unique Spotify ID.
#'@param id The Spotify ID for the artist.
#'@return Get Spotify catalog information for a single track identified by its unique Spotify ID.
#'@export
#'@examples \dontrun{
#' ## Example
#'
#'
#'foofighters<-getArtist("7jy3rLJdDQY21OgRLCZ9sD")
#'
#'}
#'
getArtist <- function(artist.id = NULL)
{
if (length(artist.id) > 20) stop("Too many album IDs, max 20")
token <- get(".token", envir = .GlobalEnv)
if(is.null(artist.id)) {
stop("No album ID provided")
} else if (length(artist.id) == 1) {
req_url <- paste0("https://api.spotify.com/v1/artists/", artist.id)
} else {
req_url <- paste0("https://api.spotify.com/v1/artists?ids=",
paste(artist.id, collapse = ","))
}
req <- httr::GET(req_url, httr::config(token = token))
artists <- httr::content(req)
if (length(artists) == 1) {
to_parse <- artists$artists
purrr::map_df(to_parse, parseArtist)
} else {
parseArtist(artists)
}
}
#'Get top tracks of an artist
#'
#' function to get top tracks of an Artist by artist ID and country
#'
#' @param country Country of interest
#' @param artist.id
#'
#' @export
getTopTracks<-function(artist.id, country = "us"){
token <- get(".token", envir = .GlobalEnv)
req <- httr::GET(paste0("https://api.spotify.com/v1/artists/",artist.id,
"/top-tracks?country=",country),
httr::config(token = token))
top_tracks <- httr::content(req)$tracks
purrr::map_df(top_tracks, parseFullTrack)
}
#' Get related artists
#'
#' @param arist.id Character string of artist
#'
#' @return
#' @export
#'
#' @examples
getRelatedArtists <- function(arist.id)
{
token <- get(".token", envir = .GlobalEnv)
req <- httr::GET(paste0("https://api.spotify.com/v1/artists/", artist.id, "/related-artists"),
httr::config(token = token))
artists <- httr::content(req)$artists
purrr::map_df(artists, parseArtist)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.