knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) if ( file.exists("not_included/daniel_env.R") ) { message("Loading environment.") source ("not_included/daniel_env.R") listen_local_artists <- readRDS("not_included/listen_local_artists.rds") artist_distances <- readRDS("not_included/artist_distances.rds") local_genre_table <- readRDS("not_included/local_genre_table.rds") artist_genre_table <- readRDS("not_included/artist_genre_table.rds") } else { source ("../not_included/daniel_env.R") message("Loading environment.") listen_local_artists <- readRDS("../not_included/listen_local_artists.rds") artist_distances <- readRDS("../not_included/artist_distances.rds") local_genre_table <- readRDS("../not_included/local_genre_table.rds") artist_genre_table <- readRDS("../not_included/artist_genre_table.rds") }
library(spotifyrecommendations) slovakia_indie_playlist_id <- "1E8vQ9Bt4bRKWm0NTzSMt2" slovakia_top_50_playlist_id <- "6WrpeNyqtoq6PEDXyuamGt" sound_of_dutch_indie_playlist_id <- "2pSeH1YQCSICs2knjs7e5o" token <- spotifyr::get_spotify_access_token()
data ("listen_local_artists") listen_local_artists %>% dplyr::sample_n(12)
The ?get_playlist
information uses ?get_release_country
slovakia_indie_playlist_info <- get_playlist_information( playlist_id = slovakia_indie_playlist_id, authorization = token ) slovakia_top_50_info <- get_playlist_information( playlist_id = slovakia_top_50_playlist_id, authorization = token ) dutch_indie_info <- get_playlist_information( playlist_id = sound_of_dutch_indie_playlist_id, authorization = token ) slovakia_indie_playlist_info$user_playlist_artists %>% head () slovakia_indie_playlist_info$user_playlist_tracks %>% head ()
The function initial_recommendations()
returns a tibble of recommendations. recommendations_by_tracks$target_artists
contains a logical vector, identifying the tracks that are among target_ids
.
sk_artist_ids <- get_national_artist_ids("sk") initial_slovak_indie <- initial_recommendations( playlist_information = slovakia_indie_playlist_info, target_ids = sk_artist_ids, authorization = token ) initial_slovak_hits <- initial_recommendations( playlist_information = slovakia_top_50_info, target_ids = sk_artist_ids, authorization = token ) initial_dutch_indie <- initial_recommendations( playlist_information = dutch_indie_info, target_ids = sk_artist_ids, authorization = token )
sum(initial_slovak_indie$release_country_code == "sk") sum(initial_slovak_indie$target_artists)
sum(initial_slovak_hits$release_country_code == "sk") sum(initial_slovak_hits$target_artists)
sum(initial_dutch_indie$release_country_code == "sk") sum(initial_dutch_indie$target_artists)
recommended_by_artist_similarity <- get_local_artist_recommendations( user_playlist_artists = slovakia_indie_playlist_info$user_playlist_artists$id, target_ids = sk_artist_ids, n_rec = 5 )
Behind the scenes, first of all the similar artists are defined.
target_ids <- listen_local_artists %>% filter ( .data$national_identity == "sk") %>% select ( .data$spotify_artist_id ) %>% unlist () %>% as.character() new_artists_spotify_artist_id <- get_local_artist_recommendations( user_playlist_artists = slovakia_indie_playlist_info$user_playlist_artists$id, target_ids = target_ids, n = 5, authorization = NULL )
Then the new recommended artists' nearest tracks are defined:
tracks_by_artist_similarity <- lapply ( recommended_by_artist_similarity, FUN = purrr::possibly(get_artist_audio_features, NULL)) if (is.null(tracks_by_artist_similarity )) { warning("Artist by similarity was not successful.") } tracks_by_artist_similarity <- do.call( rbind, tracks_by_artist_similarity ) local_recommendations_by_artist <- get_nearest_tracks( user_playlist_info = slovakia_indie_playlist_info, new_tracks = tracks_by_artist_similarity )
dutch_artists_by_genre <- get_artist_genre( user_playlist_artists = dutch_indie_info$user_playlist_artists ) user_artists_by_genre <- get_artist_genre( user_playlist_artists = dutch_indie_info$user_playlist_artists ) ## recommend artists by genre first recommended_by_genre <- get_artist_recommendations_genre( artists_by_genre = user_artists_by_genre, target_nationality = 'sk' ) get_artist_audio_features(artist ="5Sdehxs56D9zXmPdGXKWEF") tracks_by_genre_similarity <- lapply ( recommended_by_genre, purrr::possibly(get_artist_audio_features, NULL)) %>% do.call( rbind, .) get_nearest_tracks( user_playlist_info = dutch_indie_info$user_playlist_tracks, new_tracks = tracks_by_genre_similarity, n_rec = 6 )
vars_to_select <- c( "id", "name", "popularity", "uri", "external_ids.isrc", "release_country_code", "target_artists") get_local_recommendations( playlist_id = sound_of_dutch_indie_playlist_id, target_nationality = "sk", recommendation_type = "artists", limit = 20, n = 4, silent = TRUE, authorization = NULL )
start_time <- Sys.time() slovak_indie_based <- get_local_recommendations( playlist_id = "1E8vQ9Bt4bRKWm0NTzSMt2", recommendation_type = "release" ) Sys.time()-start_time
start_time <- Sys.time() global_top_50 <- get_local_recommendations( playlist_id = "1KNl4AYfgZtOVm9KHkhPTF", recommendation_type = 'artists' ) Sys.time()-start_time
start_time <- Sys.time() french_post_rock <- get_local_recommendations( user_playlist_id = "66GxbkUX9OZikMgn338pz6", authorization = token ) Sys.time()-start_time
start_time <- Sys.time() french_rock <- get_local_recommendations( user_playlist_id = "6uL4zN0tryTmOsrhcknaIS" ) Sys.time()-start_time
start_time <- Sys.time() french_rock <- get_local_recommendations( user_playlist_id = "0qJTEzbjENk61TtYeqFxfq" ) Sys.time()-start_time
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.