Skip to content

Commit

Permalink
add interactive prompt for get_artist_audio_features
Browse files Browse the repository at this point in the history
  • Loading branch information
charlie86 committed Nov 3, 2017
1 parent 1746d0c commit efcd893
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 28 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ Imports:
License: MIT License
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.0.1
RoxygenNote: 6.0.1
Suggests: testthat
44 changes: 31 additions & 13 deletions R/get_artist_audio_features.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,42 @@
#' radiohead_features <- get_artist_audio_features('radiohead')

get_artist_audio_features <- function(artist_name, access_token = get_spotify_access_token()) {

### grabs first result using spotifyr::get_artists()
artist <- get_artists(artist_name) %>%
slice(1) %>%
select(artist_uri) %>%
.[[1]]

albums <- get_albums(artist) %>% select(-c(base_album_name, base_album, num_albums, num_base_albums, album_rank))
artist <- get_artists(artist_name)

if (nrow(artist) > 0) {

if (interactive()) {
cat(paste0('We found the following artists on Spotify matching "', artist_name, '":\n\n\t', paste(artist$artist_name, collapse = '\n\t'), '\n\nPlease type the name of the artist you would like:'), sep = '')
selected_artist <- readline()
} else {
selected_artist <- artist$artist_uri[1]
}

artist_uri <- artist$artist_uri[artist$artist_name == selected_artist]
} else {
stop(paste0('Cannot find any artists on Spotify matching "', artist_name, '"'))
}

albums <- get_albums(artist_uri)

if (nrow(albums) > 0) {
albums <- select(albums, -c(base_album_name, base_album, num_albums, num_base_albums, album_rank))
} else {
stop(paste0('Cannot find any albums for "', selected_artist, '" on Spotify'))
}

album_popularity <- get_album_popularity(albums)
tracks <- get_album_tracks(albums)
track_features <- get_track_audio_features(tracks)
track_popularity <- get_track_popularity(tracks)
tots <- albums %>%
left_join(album_popularity, by = 'album_uri') %>%
left_join(tracks, by = 'album_name') %>%
left_join(track_features, by = 'track_uri') %>%

tots <- albums %>%
left_join(album_popularity, by = 'album_uri') %>%
left_join(tracks, by = 'album_name') %>%
left_join(track_features, by = 'track_uri') %>%
left_join(track_popularity, by = 'track_uri')

return(tots)
}
26 changes: 13 additions & 13 deletions R/get_user_playlists.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' Get user playlists
#'
#' This function returns a dataframe of playlists for a given Spotify username
#' @param artist_name String of Spotify username. Can be found within the Spotify App
#' @param username String of Spotify username. Can be found within the Spotify App
#' @param access_token Spotify Web API token. Defaults to spotifyr::get_spotify_access_token()
#' @keywords username
#' @export
Expand All @@ -10,15 +10,15 @@

## helper functions
parse_playlist_list_to_df <- function(playlist_list) {
playlists_df <- map_df(1:length(playlist_list), function(x) {
tmp <- playlist_list[[x]]
playlists_df <- map_df(1:length(playlist_list), function(this_playlist) {
tmp <- playlist_list[[this_playlist]]
map_df(1:length(tmp), function(y) {
tmp2 <- tmp[[y]]

if (!is.null(tmp2)) {
name <- ifelse(is.null(tmp2$name), NA, tmp2$name)
uri <- ifelse(is.null(tmp2$id), NA, tmp2$id)

list(
playlist_name = name,
playlist_uri = uri,
Expand All @@ -37,33 +37,33 @@ parse_playlist_list_to_df <- function(playlist_list) {
get_user_playlist_count <- function(username, access_token = get_spotify_access_token(), echo = F) {
endpoint <- paste0('https://api.spotify.com/v1/users/', username, '/playlists')
total <- GET(endpoint, query = list(access_token = access_token, limit = 1)) %>% content %>% .$total

if (echo) {
print(paste0('Found ', total, ' playlists from ', username))
}

return(total)
}


get_user_playlists <- function(username, access_token = get_spotify_access_token()) {

playlist_count <- get_user_playlist_count(username)
num_loops <- ceiling(playlist_count / 50)
offset <- 0

pb <- txtProgressBar(min = 0, max = num_loops, style = 3)

playlists_list <- map(1:ceiling(num_loops), function(x) {
endpoint <- paste0('https://api.spotify.com/v1/users/', username, '/playlists')
res <- GET(endpoint, query = list(access_token = access_token, offset = offset, limit = 50)) %>% content %>% .$items
offset <<- offset + 50
setTxtProgressBar(pb, x)
return(res)
})

playlists_df <- parse_playlist_list_to_df(playlists_list)

return(playlists_df)

}
2 changes: 1 addition & 1 deletion man/parse_playlist_list_to_df.Rd

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

5 changes: 5 additions & 0 deletions spotifyr.Rproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ SaveWorkspace: No
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 4
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

AutoAppendNewline: Yes
StripTrailingWhitespace: Yes

Expand Down
4 changes: 4 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
library(testthat)
library(spotifyr)

test_check("spotifyr")
6 changes: 6 additions & 0 deletions tests/testthat/test-get_artists.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
library(spotifyr)
context('get_artists')

test_that('get_artists returns a dataframe', {
expect_true(is.data.frame(get_artists('')))
})

0 comments on commit efcd893

Please sign in to comment.