Last active
March 28, 2017 10:01
-
-
Save perezp44/89e353f68715c46ce62750ab0ad5f063 to your computer and use it in GitHub Desktop.
function that find lyrics in chartlyrics API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(httr) | |
library(RCurl) | |
library(XML) | |
df <- data.frame(artist = c('Led Zeppellin', 'Adele'), song = c('Rock´n roll', 'Hello'), stringsAsFactors = F) | |
make.querrye <- function(xx) { | |
names_ok <- gsub(" ", "&", xx) | |
names_ok2 <- paste("\'", names_ok, "\'", sep = '') | |
querrye <- paste("http://api.chartlyrics.com/apiv1.asmx/SearchLyricDirect?artist=", names_ok[1],"&song=", names_ok[2], sep='') | |
data <- GET(querrye) | |
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.querrye(names) #- it works | |
names <- c(df$artist[2], df$song[2]) | |
make.querrye(names) #- it also works |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment