#' Get Album Info
#'
#' Retrieves album information. See \code{\link{parseAlbum}} for details on fields retrieved.
#'
#' @param album.id Character vector, Spotify album id
#'
#' @return A tibble
#' @export
getAlbum <- function(album.id = NULL)
{
if (length(album.id) > 20) stop("Too many album IDs, max 20")
token <- get(".token", envir = .GlobalEnv)
if(is.null(album.id)) {
stop("No album ID provided")
} else if (length(album.id) == 1) {
req_url <- paste0("https://api.spotify.com/v1/albums/", album.id)
} else {
req_url <- paste0("https://api.spotify.com/v1/albums?ids=",
paste(album.id, collapse = ","))
}
req <- httr::GET(req_url, httr::config(token = token))
albums <- httr::content(req)
if (length(albums) == 1) {
to_parse <- albums$albums
purrr::map_df(to_parse, parseAlbum)
} else {
parseAlbum(albums)
}
}
#' Get artist's albums
#'
#' Retrieves albums for a particular \code{artist.id}. See
#' \code{\link{parseAlbum}} for details on fields retrieved.
#'
#'@importFrom dplyr %>%
#'@param id Character string, Spotify artist id.
#'@param market Character string, an ISO 3166-1 alpha-2 country code.
#'@param type Character string, defaults to 'album'. Other options are "single," "appears on," and "compilation."
#'@return A tibble
#'@export
getArtistAlbums <- function(artist.id, type="album", market="us")
{
token <- get(".token", envir = .GlobalEnv)
alb_req <- httr::GET(paste0("https://api.spotify.com/v1/artists/",
artist.id,
"/albums?album_type=",
type,
"&market=",
market),
httr::config(token = token))
alb_items <- httr::content(alb_req)$items
alb_ids <- sapply(alb_items, function(x) x[['id']])
getAlbum(alb_ids)
}
#' Get Featured New Albums
#'
#' Retrieve information on 'Spotify Featured Albums.' See \code{\link{parseAlbum}} for details on fields retrieved.
#'
#' @param country Character string, an ISO 3166-1 alpha-2 country code
#' @param limit Numeric 1-50, number of albums to return
#' @param offset The index of the first item to return. Default: 0 (the first object).
#' Use with \code{limit} to get the next set of items.
#' @return A tibble
#' @export
getNewAlbums <- function(country = "us", limit = 20, offset = 0)
{
token <- get(".token", envir = .GlobalEnv)
stub <- "https://api.spotify.com/v1/browse/new-releases?"
req_url <- paste0(stub, "country=", country, "&limit=", limit, "&offset=", offset)
req <- httr::GET(req_url, httr::config(token = token))
alb_items <- httr::content(req)$albums$items
alb_ids <- sapply(alb_items, function(x) x[['id']])
alb_ids_cut <- chunk(alb_ids, 20)
purrr::map(alb_ids_cut, getAlbum) %>% dplyr::bind_rows()
}
#' Parse album object
#'
#' Helper function to parse useful fields from the album object. Mostly used
#' internally.
#'
#' \itemize{
#' \item artist name
#' \item markets album is available in
#' \item album name
#' \item album's spotify url
#' \item label album was released under
#' \item popularity score, 0-100, generated by Spotify
#' \item release date
#' \item genres
#' \item album id
#' }
#'
#' @param album_obj A json-like list, album object returned by Spotify API
#' @return A tibble
parseAlbum <- function(album_obj)
{
tibble::tibble(
artist = paste(sapply(album_obj$artists, function(x) x[['name']]), collapse = ", "),
markets = paste(album_obj[["available_markets"]], collapse = ", "),
album = album_obj[["name"]],
link = album_obj[["external_urls"]][["spotify"]],
label = album_obj[["label"]],
popularity = album_obj[["popularity"]],
release_date = album_obj[["release_date"]],
genres = paste(album_obj[['genres']], collapse = ", "),
album_id = album_obj[['id']]
)
}
#' Get tracks for an album
#'
#' Function to retrieve tracks for a given album ID. See \link{\code{parseShortTrack}}
#' for details on fields returned.
#'
#' @param album.id Character string, Spotify album id
#'
#' @return A tibble
#' @export
getAlbumTracks <- function(album.id, limit = 50, offset = 0)
{
token <- get(".token", envir = .GlobalEnv)
req <- httr::GET(paste0("https://api.spotify.com/v1/albums/",album.id,"/tracks?",
"limit=", limit,
"&offset=", offset),
httr::config(token = token))
tracks <- httr::content(req)$items
purrr::map_df(tracks, parseShortTrack)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.