For-loop通过R中的专辑获取spotify曲目



我正在尝试编写一个for循环来调用多个专辑并检索每个专辑的曲目ID和曲目名称。

原始代码适用于一张专辑:

spotifyKey <- "###########################"
spotifySecret <- "#######################"
library("httr")
library("jsonlite")
response = POST(
'https://accounts.spotify.com/api/token',
accept_json(),
authenticate(spotifyKey, spotifySecret),
body = list(grant_type = 'client_credentials'),
encode = 'form',
verbose()
)
token = content(response)$access_token
HeaderValue = paste0('Bearer ', token)

spotifyAlbum <- "2scB1uhcCI1TSf6b9TCZK3"
albumTracksURL <- paste("https://api.spotify.com/v1/albums/",
spotifyAlbum,"/tracks?limit=50",sep="")
getTracks <- GET(albumTracksURL, add_headers(Authorization = HeaderValue))
albumTracks <- jsonlite::fromJSON(toJSON(content(getTracks)))
ids <- data.frame(matrix(unlist(albumTracks$items$id), 
nrow=albumTracks$total, byrow=T),stringsAsFactors=FALSE)
names <- data.frame(matrix(unlist(albumTracks$items$name), 
nrow=albumTracks$total, byrow=T),stringsAsFactors=FALSE)
result <- cbind(ids, names)

这是输出:

matrix.unlist.albumTracks.items.id...nrow...albumTracks.total.. matrix.unlist.albumTracks.items.name...nrow...albumTracks.total..
1                                           0B1QILlvPWfc9vovFYuUuk                                             FutureSex / LoveSound
2                                           0O45fw2L5vsWpdsOdXwNAR                                                          SexyBack
3                                           5Gq7IN29DbgUO8Ris1TZKl                                                       Sexy Ladies
4                                           1vMuSYlp2Im8Gpj6JxRJfj                              Medley: Let Me Talk to You / My Love
5                                           5iyE2d4U3Vc5cqY9mPTlpy                        LoveStoned / I Think She Knows (Interlude)
6                                           3pD0f7hSJg2XdQ6udw5Tey                   What Goes Around.../...Comes Around (Interlude)
7                                           0LlObwsSjhvPFKPPpSWOeh                                                        Chop Me Up
8                                           4JQtyBEeLOAOnb7n5OQdDd                                                         Damn Girl
9                                           1MBM7CyZbwJpVbbZJnHHRg                                                       Summer Love
10                                          39q69v4D15oSshBmlJrkmW                      Set the Mood Prelude / Until the End of Time
11                                          1Ou4q1gX3QAvKhAFd3xsXT                                                     Losing My Way
12                                          24CBAWq81pQNTftNAxVLYk                                     (Another Song) All Over Again

如何为多个专辑执行此操作?

我尝试创建一个 for 循环,但这不起作用:

spotifyAlbum <- data.frame(c("62AITKgUMplmKKSTsTlDDo","2scB1uhcCI1TSf6b9TCZK3"))
colnames(spotifyAlbum)[1]<-"ALBUM"
for(i in 1:length(spotifyAlbum[,1])) {
albumTracksURL <- paste("https://api.spotify.com/v1/albums/",
spotifyAlbum[i,1],"/tracks?limit=50",sep="")
getTracks <- GET(albumTracksURL, add_headers(Authorization = HeaderValue))
albumTracks <- jsonlite::fromJSON(toJSON(content(getTracks)))
ids <- data.frame(matrix(unlist(albumTracks$items$id), 
nrow=albumTracks$total, byrow=T),stringsAsFactors=FALSE)
names <- data.frame(matrix(unlist(albumTracks$items$name), 
nrow=albumTracks$total, byrow=T),stringsAsFactors=FALSE)
spotifyAlbum[,2] <- ids
spotifyAlbum[,3] <- names
}

额外的一点是还包括专辑名称。

我的目标是让最终输出如下所示:

ID                                            NAME     ALBUM
1  0B1QILlvPWfc9vovFYuUuk                           FutureSex / LoveSound     FutureSex/LoveSounds
2  0O45fw2L5vsWpdsOdXwNAR                                        SexyBack    FutureSex/LoveSounds
3  5Gq7IN29DbgUO8Ris1TZKl                                     Sexy Ladies    FutureSex/LoveSounds
4  1vMuSYlp2Im8Gpj6JxRJfj            Medley: Let Me Talk to You / My Love    FutureSex/LoveSounds
5  5iyE2d4U3Vc5cqY9mPTlpy      LoveStoned / I Think She Knows (Interlude)    FutureSex/LoveSounds
6  3pD0f7hSJg2XdQ6udw5Tey What Goes Around.../...Comes Around (Interlude)    FutureSex/LoveSounds
7  0LlObwsSjhvPFKPPpSWOeh                                      Chop Me Up    FutureSex/LoveSounds
8  4JQtyBEeLOAOnb7n5OQdDd                                       Damn Girl    FutureSex/LoveSounds
9  1MBM7CyZbwJpVbbZJnHHRg                                     Summer Love    FutureSex/LoveSounds
10 39q69v4D15oSshBmlJrkmW    Set the Mood Prelude / Until the End of Time    FutureSex/LoveSounds
11 1Ou4q1gX3QAvKhAFd3xsXT                                   Losing My Way    FutureSex/LoveSounds
12 24CBAWq81pQNTftNAxVLYk                   (Another Song) All Over Again    FutureSex/LoveSounds
13 0KqpOdVQoDfeELTKYc97cO                        Piensas (Dile la Verdad)    Dale
14 2MRv991PzjY2ecCluXoRq9              Como Yo Le Doy - Spanglish Version    Dale
15 501PoxE6Qti2ekECimQeXK                                         El Taxi    Dale
16 2yUu771fYfHQa45VcVvkhS                   Yo Quiero (Si Tu Te Enamoras)    Dale
17 5KsChQslA4yRGInwDfGm9N                                        El Party    Dale
18 3yyXE55qVqIoYS9g3Q5LMM                                       Mami Mami    Dale
19 3NW0zAs5TXnfoyWuTPdyjP                            Baddest Girl in Town    Dale
20 59db0pQBOuB1FgjLo7RlvH                                 Chi Chi Bon Bon    Dale
21 2iRSdAGD6fbY2SdJ2wnTal                                  Haciendo Ruido    Dale
22 4BmLLPmR24ExGQyqhJH8i2                                     Hoy Se Bebe    Dale
23 62NKyNgp1yBp2KYRHIm2ou                                    No Puedo Más    Dale
24 0fafrbr2rolcqdV1aOkI6n                                      Que Lo Que    Dale

任何帮助都会很棒,谢谢!

这样的事情可能会让你得到你所追求的。

spotifyKey <- "###########################"
spotifySecret <- "#######################"
library("httr")
library("jsonlite")
response = POST(
'https://accounts.spotify.com/api/token',
accept_json(),
authenticate(spotifyKey, spotifySecret),
body = list(grant_type = 'client_credentials'),
encode = 'form',
verbose()
)
token = content(response)$access_token
HeaderValue = paste0('Bearer ', token)
spotifyAlbums <- c("62AITKgUMplmKKSTsTlDDo","2scB1uhcCI1TSf6b9TCZK3")
get.tracks <- function(spotifyAlbum){
albumTracksURL <- paste("https://api.spotify.com/v1/albums/", spotifyAlbum, "/tracks?limit=50", sep="")
getTracks <- GET(albumTracksURL, add_headers(Authorization = HeaderValue))
albumTracks <- jsonlite::fromJSON(toJSON(content(getTracks)))
ids <- data.frame(matrix(unlist(albumTracks$items$id), 
nrow=albumTracks$total, byrow=T),stringsAsFactors=FALSE)
names <- data.frame(matrix(unlist(albumTracks$items$name), 
nrow=albumTracks$total, byrow=T),stringsAsFactors=FALSE)
artists <- data.frame(matrix(unlist(albumTracks$items$artists), nrow=albumTracks$total, byrow=T),stringsAsFactors=FALSE)
result <- cbind(ids, names)
colnames(result) <- c("ID", "NAME")
return(result)
}
df <- lapply(spotifyAlbums, get.tracks)
df <- do.call(rbind, df)

修改的功能

get.tracks <- function(spotifyAlbum){
albumTracksURL <- paste("https://api.spotify.com/v1/albums/", spotifyAlbum, "/tracks?limit=50", sep="")
getTracks <- GET(albumTracksURL, add_headers(Authorization = HeaderValue))
albumTracks <- jsonlite::fromJSON(toJSON(content(getTracks)))
ids <- unlist(albumTracks$items$id)
names <- unlist(albumTracks$items$name)
artists <- paste(unlist(albumTracks$items$artists), collapse = "; ")
result <- data.frame(ID = ids, NAME = names, ARTIST = artists, stringsAsFactors = FALSE)
return(result)
}

相关内容

  • 没有找到相关文章

最新更新