Created
September 19, 2019 12:27
-
-
Save sillasgonzaga/1df4be9bf1e1a1afb39d4fb1918ea7c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(spotifyr) | |
library(tidyverse) | |
Sys.setenv(SPOTIFY_CLIENT_ID = '') | |
Sys.setenv(SPOTIFY_CLIENT_SECRET = '') | |
xml <- read_html("https://developer.spotify.com/documentation/general/guides/scopes/") | |
scopes <- xml %>% html_nodes("code") %>% html_text() %>% unique() | |
auth <- get_spotify_authorization_code(scope = scopes) | |
#auth <- get_spotify_access_token() | |
#get_spotify_authorization_token() | |
#access_token <- get_spotify_access_token() | |
#get_spotify_authorization_code() | |
get_my_recently_played(limit = 5, authorization = auth) | |
### get all albums from artist | |
input_artist_id <- "7AGSJihqYPhYy5QzMcwcQT" | |
#artist_name <- "Headhunterz" | |
# get artist name | |
input_artist_name <- get_artist(id = input_artist_id)$name | |
pgs <- 49 * c(0:10) | |
sp_albums <- pgs %>% | |
purrr::map(~ get_artist_albums(id = input_artist_id, offset = ., limit = 50)) | |
sp_albums_df <- sp_albums %>% | |
bind_rows() %>% | |
as_tibble() %>% | |
distinct(id, name, release_date) %>% | |
arrange(release_date) %>% | |
rename_all(~ str_c("album_", .x)) | |
albums_ids <- unique(sp_albums_df$album_id) | |
### get all tracks from albums | |
get_album_tracks_include_album <- function(...){ | |
args <- list(...) | |
out <- get_album_tracks(...) | |
if (inherits(out, "data.frame")){ | |
if (nrow(out) > 0){ | |
out$album_id <- args[[1]] | |
} | |
} | |
out | |
} | |
sp_tracks <- albums_ids %>% | |
map(get_album_tracks_include_album, limit = 50) | |
sp_tracks_df <- sp_tracks %>% | |
discard(is.null) %>% | |
bind_rows() %>% | |
as_tibble() %>% | |
mutate(artists = map(artists, ~ rename_all(.x, ~ str_c("artist_", .x)))) %>% | |
unnest(artists) | |
sp_tracks_df_clean <- sp_tracks_df %>% | |
filter(artist_id == input_artist_id) %>% | |
select(track_id = id, track_name = name, track_uri = uri, album_id) %>% | |
# clean track names | |
mutate(track_name_cleaned = str_squish(str_to_lower(track_name))) %>% | |
# filter out tracks with same name | |
distinct(track_name_cleaned, .keep_all = TRUE) %>% | |
select(-track_name_cleaned) %>% | |
left_join(sp_albums_df, by = "album_id") %>% | |
arrange(album_release_date) | |
track_uris <- unique(sp_tracks_df_clean$track_uri) | |
### create playlist | |
plist_name <- str_glue("{input_artist_name} - The Complete Collection") | |
new_plist <- create_playlist(user_id = "12139910710", | |
name = plist_name, | |
public = TRUE, | |
collaborative = FALSE, | |
description = "Made with programming using R by github.com/sillasgonzaga") | |
### add tracks to playlist | |
# quebrar as musicas em grupos de cem | |
tracks_to_add <- tibble(track_uris) %>% | |
mutate(batch = seq_along(track_uris) %/% 100, | |
position = row_number() - 1) %>% | |
split(.$batch) | |
for (i in seq_along(tracks_to_add)){ | |
add_tracks_to_playlist( | |
playlist_id = new_plist$id, | |
uris = tracks_to_add[[i]]$track_uris, | |
position = min(tracks_to_add[[i]]$position) | |
) | |
} | |
cat(new_plist$external_urls$spotify) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment