@@ -32,7 +32,12 @@ get_spotify_track_info <- function(
3232 api_key = get_spotify_access_token()
3333) {
3434 check_character(track_id , allow_na = TRUE )
35- check_character(api_key )
35+ check_character(api_key $ client_id )
36+ check_character(api_key $ client_secret )
37+ api_key <- spotifyr :: get_spotify_access_token(
38+ client_id = api_key $ client_id ,
39+ client_secret = api_key $ client_secret
40+ )
3641
3742 if (is.na(track_id ) || track_id == " " ) {
3843 return (NULL )
@@ -60,19 +65,24 @@ get_spotify_track_info <- function(
6065
6166# ' Spotify API helpers
6267# '
63- # ' This is a wrapper around [spotifyr::get_spotify_access_token()] to create a
64- # ' Spotify access token from stored environment variables.
68+ # ' Set and retrieve Spotify client information.
69+ # '
70+ # ' @param id A Spotify Client ID.
71+ # ' @param secret A Spotify Client Secret.
6572# '
66- # ' @returns The Spotify access token.
73+ # ' @returns
74+ # ' * `get_spotify_api_key()` returns a previously stored Client ID and Secret.
75+ # ' * `set_spotify_api_key()` is called for side effects only.
76+ # ' @name spotify-api
6777# ' @export
6878# '
6979# ' @examplesIf taylor_examples()
7080# ' \donttest{get_spotify_access_token()}
7181get_spotify_access_token <- function () {
7282 client_id <- Sys.getenv(" SPOTIFY_CLIENT_ID" )
7383 client_secret <- Sys.getenv(" SPOTIFY_CLIENT_SECRET" )
74- if (! any(identical (client_id , " " ), identical (client_secret , " " ))) {
75- token <- spotifyr :: get_spotify_access_token (
84+ if (all(nzchar (client_id ), nzchar (client_secret ))) {
85+ token <- list (
7686 client_id = client_id ,
7787 client_secret = client_secret
7888 )
@@ -85,16 +95,39 @@ get_spotify_access_token <- function() {
8595 cli :: cli_abort(
8696 cli :: format_message(
8797 c(
88- " No access token found, please see" ,
98+ " Client ID or Secret not found, please see" ,
8999 " {.fun spotifyr::get_spotify_access_token}"
90100 )
91101 )
92102 )
93103 }
94104}
95105
106+ # ' @export
107+ # ' @rdname spotify-api
108+ set_spotify_api_key <- function (id = NULL , secret = NULL ) {
109+ check_character(id , allow_null = TRUE )
110+ check_character(secret , allow_null = TRUE )
111+
112+ if (is.null(id )) {
113+ id <- askpass :: askpass(" Please enter your Client ID" )
114+ if (is.null(id )) {
115+ cli :: cli_abort(" Client ID not provided" )
116+ }
117+ }
118+ Sys.setenv(" SPOTIFY_CLIENT_ID" = id )
119+
120+ if (is.null(secret )) {
121+ secret <- askpass :: askpass(" Please enter your Client Secret" )
122+ if (is.null(secret )) {
123+ cli :: cli_abort(" Client Secret not provided" )
124+ }
125+ }
126+ Sys.setenv(" SPOTIFY_CLIENT_SECRET" = secret )
127+ }
128+
96129spotify_testing_key <- function () {
97- spotifyr :: get_spotify_access_token (
130+ list (
98131 client_id = httr2 :: secret_decrypt(
99132 " UOF5NVolAFuZUfPsrqB6zRGiuT2U6kZTly16hmop_vkzywAmTyHJaDuWl13gymsI" ,
100133 " TAYLOR_KEY"
@@ -120,10 +153,11 @@ spotify_testing_key <- function() {
120153# '
121154# ' @details
122155# ' Due to differences in algorithms and methodologies, the SoundStat audio
123- # ' features are on a slightly different scale than the Spotify audio features
124- # ' that were originally included in [taylor] prior the [changes to the Spotify
125- # ' API](). We can convert the SoundStat values to the Spotify scale using the
126- # ' formulas in the [SoundStat
156+ # ' features are on a slightly different scale than the audio features that were
157+ # ' originally included in [taylor] prior to the [changes to the Spotify
158+ # ' API](https://developer.spotify.com/blog/2024-11-27-changes-to-the-web-api).
159+ # ' We can convert the SoundStat values to the Spotify scale using the formulas
160+ # ' in the [SoundStat
127161# ' docs](https://soundstat.info/article/Understanding-Audio-Analysis.html):
128162# '
129163# ' ```
0 commit comments