/
artists.R
219 lines (179 loc) · 9.44 KB
/
artists.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#' Get Spotify catalog information for a single artist identified by their unique Spotify ID.
#'
#' @param id The \href{https://developer.spotify.com/documentation/web-api/#spotify-uris-and-ids}{Spotify ID} for the artist.
#' @param authorization Required. A valid access token from the Spotify Accounts service. See the \href{https://developer.spotify.com/documentation/general/guides/authorization-guide/}{Web API authorization Guide} for more details.
#' Defaults to \code{spotifyr::get_spotify_access_token()}
#' @return
#' Returns a data frame of results containing album data.
#' See \url{https://developer.spotify.com/documentation/web-api/reference/albums/get-album/} for more information.
#' @family artist functions
#' @export
get_artist <- function(id,
authorization = get_spotify_access_token()
) {
base_url <- 'https://api.spotify.com/v1/artists'
params <- list(
access_token = authorization
)
url <- str_glue('{base_url}/{id}')
res <- RETRY(verb = 'GET', url,
query = params,
encode = 'json',
terminate_on = c(401, 403, 404)
)
stop_for_status(res)
res <- fromJSON(content(res, as = 'text', encoding = 'UTF-8'), flatten = TRUE)
res
}
#' Get Spotify catalog information for multiple artists identified by their Spotify IDs.
#'
#' @param ids Required. A character vector of the
#' \href{https://developer.spotify.com/documentation/web-api/#spotify-uris-and-ids}{Spotify IDs} for the artists. Maximum: 50 IDs.
#' @param authorization Required. A valid access token from the Spotify Accounts service.
#' See the \href{https://developer.spotify.com/documentation/general/guides/authorization-guide/}{Web API authorization guide} for more details. Defaults to \code{spotifyr::get_spotify_access_token()}
#' @param include_meta_info Optional. Boolean indicating whether to include full result,
#' with meta information such as \code{"total"}, and \code{"limit"}. Defaults to \code{FALSE}.
#' @return
#' Returns a data frame of results containing artist data.
#' See \url{https://developer.spotify.com/documentation/web-api/reference/artists/get-several-artists/} for more information.
#' @family artist functions
#' @export
get_artists <- function(ids,
authorization = get_spotify_access_token(),
include_meta_info = FALSE) {
base_url <- 'https://api.spotify.com/v1/artists'
params <- list(
ids = paste(ids, collapse = ','),
access_token = authorization
)
res <- RETRY(verb = 'GET', base_url, query = params, encode = 'json', terminate_on = c(401, 403, 404))
stop_for_status(res)
res <- fromJSON(content(res, as = 'text', encoding = 'UTF-8'), flatten = TRUE)
if (!include_meta_info) {
res <- res$artists
}
res
}
#' Get Spotify catalog information for multiple artists identified by their Spotify IDs.
#'
#' @param id The \href{https://developer.spotify.com/documentation/web-api/#spotify-uris-and-ids}{Spotify ID} for the artist.
#' @param include_groups Optional. A character vector of keywords that will be used to filter the response. If not supplied, all album types will be returned. Valid values are: \cr
#' \code{"album"} \cr
#' \code{"single"} \cr
#' \code{"appears_on"} \cr
#' \code{"compilation"} \cr
#' For example: \code{include_groups = c("album", "single")}
#' @param market Optional. \cr
#' An ISO 3166-1 alpha-2 country code or the string \code{"from_token"}. \cr
#' Supply this parameter to limit the response to one particular geographical market. For example, for albums available in Sweden: \code{market = "SE"}. \cr
#' If not given, results will be returned for all markets and you are likely to get duplicate results per album, one for each market in which the album is available!
#' @param limit Optional. \cr
#' Maximum number of results to return. \cr
#' Default: 20 \cr
#' Minimum: 1 \cr
#' Maximum: 50 \cr
#' @param offset Optional. \cr
#' The index of the first album to return. \cr
#' Default: 0 (i.e., the first album). \cr
#' Use with limit to get the next set of albums.
#' @param authorization Required. A valid access token from the Spotify Accounts service. See the \href{https://developer.spotify.com/documentation/general/guides/authorization-guide/}{Web API authorization guide} for more details. Defaults to \code{spotifyr::get_spotify_access_token()}
#' @param include_meta_info Optional. Boolean indicating whether to include full result, with meta information such as \code{"total"}, and \code{"limit"}. Defaults to \code{FALSE}.
#' @return
#' Returns a data frame of results containing artist data. See \url{https://developer.spotify.com/documentation/web-api/reference/artists/get-several-artists/} for more information.
#' @family artist functions
#' @export
get_artist_albums <- function(id,
include_groups = c('album', 'single', 'appears_on',
'compilation'),
market = NULL,
limit = 20, offset = 0,
authorization = get_spotify_access_token(),
include_meta_info = FALSE) {
base_url <- 'https://api.spotify.com/v1/artists'
if (!is.null(market)) {
if (!str_detect(market, '^[[:alpha:]]{2}$')) {
stop('"market" must be an ISO 3166-1 alpha-2 country code')
}
}
params <- list(
include_groups = paste(include_groups, collapse = ','),
market = market,
limit = limit,
offset = offset,
access_token = authorization
)
url <- str_glue('{base_url}/{id}/albums')
res <- RETRY(verb = 'GET', url,
query = params,
encode = 'json', terminate_on = c(401, 403, 404))
stop_for_status(res)
res <- fromJSON(
content(res, as = 'text', encoding = 'UTF-8'),
flatten = TRUE
)
if (!include_meta_info) {
res <- res$items
}
res
}
#' @title Get Spotify catalog information about an artist’s top tracks by country.
#'
#' @param id The \href{https://developer.spotify.com/documentation/web-api/#spotify-uris-and-ids}{Spotify ID} for the artist.
#' @param market Required. \cr
#' An ISO 3166-1 alpha-2 country code or the string \code{"from_token"}.
#' Defaults to \code{"US"}.
#' @param authorization Required. A valid access token from the Spotify Accounts service. See the \href{https://developer.spotify.com/documentation/general/guides/authorization-guide/}{Web API authorization guide} for more details. Defaults to \code{spotifyr::get_spotify_access_token()}
#' @param include_meta_info Optional. Boolean indicating whether to include full result, with meta information such as \code{"total"}, and \code{"limit"}. Defaults to \code{FALSE}.
#' @return
#' Returns a data frame of results containing artist data. See \url{https://developer.spotify.com/documentation/web-api/reference/artists/get-several-artists/} for more information.
#' @family artist functions
#' @export
get_artist_top_tracks <- function(id,
market = 'US',
authorization = get_spotify_access_token(),
include_meta_info = FALSE) {
base_url <- 'https://api.spotify.com/v1/artists'
if (!is.null(market)) {
if (!str_detect(market, '^[[:alpha:]]{2}$')) {
stop('"market" must be an ISO 3166-1 alpha-2 country code')
}
}
params <- list(
market = market,
access_token = authorization
)
url <- str_glue('{base_url}/{id}/top-tracks')
res <- RETRY(verb = 'GET', url, query = params, encode = 'json', terminate_on = c(401, 403, 404))
stop_for_status(res)
res <- fromJSON(content(res, as = 'text', encoding = 'UTF-8'), flatten = TRUE)
if (!include_meta_info) {
res <- res$tracks
}
res
}
#' Get Spotify catalog information about artists similar to a given artist.
#' Similarity is based on analysis of the Spotify community’s listening history.
#'
#' @param id The \href{https://developer.spotify.com/documentation/web-api/#spotify-uris-and-ids}{Spotify ID} for the artist.
#' @param authorization Required. A valid access token from the Spotify Accounts service. See the \href{https://developer.spotify.com/documentation/general/guides/authorization-guide/}{Web API authorization guide} for more details. Defaults to \code{spotifyr::get_spotify_access_token()}
#' @param include_meta_info Optional. Boolean indicating whether to include full result, with meta information such as \code{"total"}, and \code{"limit"}. Defaults to \code{FALSE}.
#' @return
#' Returns a data frame of results containing artist data. See \url{https://developer.spotify.com/documentation/web-api/reference/artists/get-several-artists/} for more information.
#' @family artist functions
#' @export
get_related_artists <- function(id,
authorization = get_spotify_access_token(),
include_meta_info = FALSE) {
base_url <- 'https://api.spotify.com/v1/artists'
params <- list(
access_token = authorization
)
url <- str_glue('{base_url}/{id}/related-artists')
res <- RETRY(verb = 'GET', url, query = params, encode = 'json', terminate_on = c(401, 403, 404))
stop_for_status(res)
res <- fromJSON(content(res, as = 'text', encoding = 'UTF-8'), flatten = TRUE)
if (!include_meta_info) {
res <- res$artists
}
res
}