Skip to content

Commit

Permalink
add get related artists
Browse files Browse the repository at this point in the history
  • Loading branch information
charlie86 committed Jan 4, 2018
1 parent 5807f3c commit 0451dad
Show file tree
Hide file tree
Showing 20 changed files with 592 additions and 468 deletions.
50 changes: 25 additions & 25 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
Package: spotifyr
Title: Pull Track Audio Features from the 'Spotify' Web API
Version: 1.0.0
Authors@R: person("Charlie", "Thompson", email = "charles.thompson@barcelonagse.eu", role = c("aut", "cre"))
Description: A wrapper for pulling track audio features from the
'Spotify' Web API <http://developer.spotify.com/web-api> in bulk.
By automatically batching API requests, it allows you to enter an artist's
name and retrieve their entire discography in seconds, along with audio
features and track/album popularity metrics. You can also pull song and
playlist information for a given 'Spotify' user (including yourself!).
Depends: R (>= 3.3.3)
Imports:
dplyr,
purrr,
tidyr,
httr,
stringdist,
lubridate
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.0.1
Suggests: testthat
URL: http://github.com/charlie86/spotifyr
BugReports: http://github.com/charlie86/spotifyr/issues
Package: spotifyr
Title: Pull Track Audio Features from the 'Spotify' Web API
Version: 1.0.0
Authors@R: person("Charlie", "Thompson", email = "charles.thompson@barcelonagse.eu", role = c("aut", "cre"))
Description: A wrapper for pulling track audio features from the
'Spotify' Web API <http://developer.spotify.com/web-api> in bulk.
By automatically batching API requests, it allows you to enter an artist's
name and retrieve their entire discography in seconds, along with audio
features and track/album popularity metrics. You can also pull song and
playlist information for a given 'Spotify' user (including yourself!).
Depends: R (>= 3.3.3)
Imports:
dplyr,
purrr,
tidyr,
httr,
stringdist,
lubridate
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.0.1
Suggests: testthat
URL: http://github.com/charlie86/spotifyr
BugReports: http://github.com/charlie86/spotifyr/issues
47 changes: 24 additions & 23 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
# Generated by roxygen2: do not edit by hand

export(get_album_popularity)
export(get_album_tracks)
export(get_albums)
export(get_artist_audio_features)
export(get_artists)
export(get_playlist_tracks)
export(get_spotify_access_token)
export(get_track_audio_features)
export(get_track_popularity)
export(get_user_audio_features)
export(get_user_playlist_count)
export(get_user_playlists)
export(parse_playlist_list_to_df)
import(dplyr)
import(stringdist)
import(httr)
import(purrr)
import(tidyr)
importFrom(lubridate,year)
importFrom(utils,setTxtProgressBar)
importFrom(utils,txtProgressBar)
# Generated by roxygen2: do not edit by hand

export(get_album_popularity)
export(get_album_tracks)
export(get_albums)
export(get_artist_audio_features)
export(get_artists)
export(get_playlist_tracks)
export(get_related_artists)
export(get_spotify_access_token)
export(get_track_audio_features)
export(get_track_popularity)
export(get_user_audio_features)
export(get_user_playlist_count)
export(get_user_playlists)
export(parse_playlist_list_to_df)
import(dplyr)
import(httr)
import(purrr)
import(stringdist)
import(tidyr)
importFrom(lubridate,year)
importFrom(utils,setTxtProgressBar)
importFrom(utils,txtProgressBar)
74 changes: 74 additions & 0 deletions R/get_related_artists.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#' Get Related Artists
#'
#' This function searches Spotify's library for artists by name or Spotify URI and returns related artists using Spotify's "Related Artists" API endpoint.
#' @param artist_name String of artist name
#' @param artist_uri String of Spotify artist URI. Will only be applied if \code{use_arist_uri} is set to \code{TRUE}. This is useful for pulling related artists in bulk and allows for more accurate matching since Spotify URIs are unique.
#' @param use_artist_uri Boolean determining whether to search by Spotify URI instead of an artist name. If \code{TRUE}, you must also enter an \code{artist_uri}. Defaults to \code{FALSE}.
#' @param return_closest_artist Boolean determining whether to use string distance automatically return the closest match for \code{artist_name}. Only applies if \code{use_artist_uri} is set to \code{FALSE}.
#' @param access_token Spotify Web API token. Defaults to \code{spotifyr::get_spotify_access_token()}.
#' @keywords artists related
#' @export
#' @examples
#' \dontrun{
#' get_related_artists('radiohead')
#'
#' ## If you know the Spotify URI for the artist (or more likely, artists) you're looking for,
#' set use_artist_uri to TRUE and use artist_uri.
#' purrr::map_df(bunch_of_artist_uris, function(this_artist_uri) {
#' get_related_artists(artist_uri = this_artist_uri, use_artist_uri = TRUE)
#' })
#' }

get_related_artists <- function(artist_name = NULL, artist_uri = NULL, use_artist_uri = FALSE, return_closest_artist = TRUE, access_token = get_spotify_access_token()) {

if (use_artist_uri == FALSE) {

if (is.null(artist_name)) {
stop('You must enter an artist name if use_artist_uri = FALSE.')
}

artists <- get_artists(artist_name, access_token = access_token)

if (nrow(artists) > 0) {
if (return_closest_artist == TRUE) {
string_distances <- stringdist(artist_name, artists$artist_name, method = 'cosine')
min_distance_index <- which(string_distances == min(string_distances))
selected_artist <- artists$artist_name[min_distance_index]
message(paste0('Selecting artist "', selected_artist, '". Choose return_closest_artist = FALSE to interactively choose from all the artist matches on Spotify.'))
} else {
cat(paste0('We found the following artists on Spotify matching "', artist_name, '":\n\n\t', paste(artists$artist_name, collapse = "\n\t"), '\n\nPlease type the name of the artist you would like:'), sep = '')
selected_artist <- readline()
}

artist_uri <- artists$artist_uri[artists$artist_name == selected_artist]
} else {
stop(paste0('Cannot find any artists on Spotify matching "', artist_name, '"'))
}
} else {
if (!is.null(artist_uri)) {
artist_uri <- artist_uri
} else {
stop('You must enter an artist_uri if use_artist_uri = TRUE.')
}
}

res <- GET(paste0('https://api.spotify.com/v1/artists/', artist_uri, '/related-artists'), query = list(access_token = access_token)) %>% content

if (!is.null(res$error)) {
stop(paste0(res$error$message, ' (', res$error$status, ')'))
}

content <- res$artists

related_artists <- purrr::map_df(1:length(content), function(this_artist) {
this_artist_info <- content[[this_artist]]
list(
artist_name = this_artist_info$name,
artist_uri = this_artist_info$id,
popularity = this_artist_info$popularity,
num_followers = this_artist_info$followers$total
)
})

return(related_artists)
}
81 changes: 41 additions & 40 deletions R/spotifyr.R
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
#' \code{spotifyr} package
#'
#' A Quick and Easy Wrapper for Pulling Track Audio Features from Spotify's Web API in Bulk
#'
#' See the README on
#' \href{https://github.com/charlie86/spotifyr#readme}{GitHub}
#'
#' @docType package
#' @name spotifyr
#' @import purrr
#' @import dplyr
#' @import tidyr
#' @import httr
#' @import stringdist
#' @importFrom lubridate year
#' @importFrom utils setTxtProgressBar txtProgressBar
NULL

globalVars <- c("album_name",
"album_rank",
"album_release_date",
"album_release_year",
"album_uri",
"analysis_url",
"base_album",
"base_album_name",
"key",
"num_albums",
"num_base_albums",
"playlist_img",
"playlist_name",
"playlist_uri",
"track_href",
"track_uri",
"type",
"uri",
".")

## quiets concerns of R CMD check re: the .'s that appear in pipelines
if(getRversion() >= "2.15.1") utils::globalVariables(globalVars)
#' \code{spotifyr} package
#'
#' A Quick and Easy Wrapper for Pulling Track Audio Features from Spotify's Web API in Bulk
#'
#' See the README on
#' \href{https://github.com/charlie86/spotifyr#readme}{GitHub}
#'
#' @docType package
#' @name spotifyr
#' @import purrr
#' @import dplyr
#' @import tidyr
#' @import httr
#' @import stringdist
#' @importFrom lubridate year
#' @importFrom utils setTxtProgressBar txtProgressBar
NULL

globalVars <- c(
"album_name",
"album_rank",
"album_release_date",
"album_release_year",
"album_uri",
"analysis_url",
"base_album",
"base_album_name",
"key",
"num_albums",
"num_base_albums",
"playlist_img",
"playlist_name",
"playlist_uri",
"track_href",
"track_uri",
"type",
"uri",
".")

## quiets concerns of R CMD check re: the .'s that appear in pipelines
if(getRversion() >= "2.15.1") utils::globalVariables(globalVars)
48 changes: 24 additions & 24 deletions man/get_album_popularity.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 25 additions & 25 deletions man/get_album_tracks.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0451dad

Please sign in to comment.