10

I'm trying to get lyrics from the chartlyrics API. I write an R function that works but not inside a loop. My script is:

library(httr)
library(RCurl)
library(XML)

df <- data.frame(artist = c('Led Zeppellin', 'Adele'), song = c('Rock´n roll', 'Hello'), stringsAsFactors = F)

make.querye <- function(xx) {
  names_ok <- gsub(" ", "&", xx)
  names_ok2 <- paste("\'", names_ok, "\'", sep = '')
 querye <- paste("http://api.chartlyrics.com/apiv1.asmx/SearchLyricDirect?artist=", names_ok[1],"&song=",  names_ok[2], sep='')
 data <- GET(querye)
 aa <- content(data, "text")   
 doc <- htmlParse(aa, asText=TRUE)  
 plain.text <- xpathSApply(doc, "//lyric//text()[not(ancestor::script)][not(ancestor::style)][not(ancestor::noscript)][not(ancestor::form)]", xmlValue)  
 if (length(plain.text)==0) {
   plain.text2 <- 'Lyrics not found'
 } else {
   plain.text2 <- iconv(plain.text, from = "UTF-8", to = "latin1", sub = NA, mark = TRUE, toRaw = FALSE)  
 }
 return(plain.text2)
 }


names <- c(df$artist[1], df$song[1])
make.querye(names) #- it works

names <- c(df$artist[2], df$song[2])
make.querye(names) #- it also works

But my function doesn't work inside a loop

for (ii in  1:2){
  names <- c(df$artist[ii], df$song[ii])
  print(names)
  make.querye(names)
}

I get the following error:

Error in curl::curl_fetch_memory(url, handle = handle) : Failure when receiving data from the peer

2
  • are you behind a proxy? Commented Sep 7, 2017 at 13:00
  • Sorry, I have seen today your comment. In fact I'm not sure about your question. Is a really old question and now I'm not working on this, then I'm going to close the question (if I can because never did it before). In this long period I have seen other doing the a similar thing and I thoght that the solution is to put some time between request, but I'm not sure about this. Anyway, thanks for your answer Commented Jan 23, 2018 at 14:57

1 Answer 1

6

The RETRY function was introduced in June 2016 and allows you to retry a request multiple times until it succeeds.

Use it with the parameter verb = "GET" instead of directly using GET, i.e.,

data <- RETRY("GET", query)

You can also define the maximum number of attempts with the times parameter.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.