Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
For loop通过R中的唱片集获取spotify曲目_R_Api_For Loop_Apply_Libspotify - Fatal编程技术网

For loop通过R中的唱片集获取spotify曲目

For loop通过R中的唱片集获取spotify曲目,r,api,for-loop,apply,libspotify,R,Api,For Loop,Apply,Libspotify,我正在尝试编写一个for循环来调用多个相册并检索每个相册的曲目id和曲目名称 原始代码适用于一张专辑: 如何为多张相册执行此操作 我尝试创建for循环,但没有成功: spotifyAlbum <- data.frame(c("62AITKgUMplmKKSTsTlDDo","2scB1uhcCI1TSf6b9TCZK3")) colnames(spotifyAlbum)[1]<-"ALBUM" for(i in 1:length(spotifyAlbum[,1])) { album

我正在尝试编写一个for循环来调用多个相册并检索每个相册的曲目id和曲目名称

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

如何为多张相册执行此操作

我尝试创建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
}

任何帮助都会很好,谢谢

像这样的事情可能会让你得到你想要的东西

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)
}

当您尝试在函数中包含艺术家时,是否会出现错误?美术师如果您添加美术师,您需要确保并实现这两个更改。谢谢,我这样做了,但是出现了一个错误,错误是:rbindeparse.level中的错误,…:参数的列数不匹配我用10个不同的相册通过[1]4FZCRCVBA01YTH08HNI3D 4SKCIJHVVSKRDOBTOFBXU 0QqrTxRtjqQmH5XyqwORlU 5M9RXTBF7MHFGGQLFSIERY 3rnhvfgbXmDAcESeGp69Tx[6]6OPAQ3TPMGIGVLPJOSCQP4G 4WFBU3ZTRN7ZGHMWVE 0GMJYISNPHGBCL18HNFSC7O 5T4Y6M7FH7FH7FH7FZHWKZYQTSEENbWK7FF5FFFFYZH7ZH7ZH7ZZYZZZZZZZZZZZYZZZZZZZZZZYZYY
                       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)
}