Commit 67f40819 authored by Urs Fleisch's avatar Urs Fleisch
Browse files

Support lyrics.wikia.com in EmbedLyrics.qml.

parent 092033c1
......@@ -25,6 +25,52 @@ import Kid3 1.0
Kid3Script {
onRun: {
var lyricFetchers = {
"makeitpersonal": {
getUrl: function(artist, title) {
return "http://makeitpersonal.co/lyrics?artist=%1&title=%2".
arg(artist).arg(title)
},
parseResponse: function(resp) {
return (resp && resp.substring(0, 5) !== "Sorry") ? resp : ""
}
},
"lyrics.wikia.com": {
getUrl: function(artist, title) {
return "http://lyricwiki.org/" + encodeURIComponent(artist) +
":" + encodeURIComponent(title)
},
parseResponse: function(resp) {
function replaceHtmlEntities(str) {
str = str.replace(/<br[ \/]*>/gmi, "\n");
str = str.replace(/<\/?\w(?:[^"'>]|"[^"]*"|'[^']*')*>/gmi, "");
str = str.replace(/&#\d+;/gm, function(s) {
return String.fromCharCode(parseInt(s.substring(
2, s.length - 1)))
})
return str
}
var begin = resp.indexOf("<div class='lyricbox'>")
if (begin !== -1) {
begin = resp.indexOf("</script>", begin)
if (begin !== -1) {
begin += 9
var end = resp.indexOf("<!--", begin)
var txt = resp.substring(begin, end)
if (txt.substring(0, 2) === "&#") {
txt = replaceHtmlEntities(txt)
return txt
}
}
}
return ""
}
}
}
var usedFetchers = ["makeitpersonal", "lyrics.wikia.com"]
var currentFetcherIdx = 0
function toNextFile() {
if (!app.nextFile()) {
if (isStandalone()) {
......@@ -33,7 +79,16 @@ Kid3Script {
}
Qt.quit()
} else {
currentFetcherIdx = 0
setTimeout(doWork, 1)
}
}
function toNextFetcher() {
if (++currentFetcherIdx < usedFetchers.length) {
setTimeout(doWork, 1)
} else {
toNextFile()
}
}
......@@ -42,26 +97,33 @@ Kid3Script {
var artist = app.getFrame(tagv2, "artist") || app.getFrame(tagv1, "artist")
var title = app.getFrame(tagv2, "title") || app.getFrame(tagv1, "title")
var doc = new XMLHttpRequest();
var name = usedFetchers[currentFetcherIdx]
var fetcher = lyricFetchers[name]
doc.onreadystatechange = function() {
if (doc.readyState === XMLHttpRequest.DONE) {
if (doc.status === 200) {
var txt = doc.responseText.trim()
if (txt && txt.substring(0, 5) !== "Sorry") {
txt = fetcher.parseResponse(txt)
if (txt) {
app.setFrame(tagv2, "lyrics", txt)
console.log("Set lyrics for %1 - %2".arg(artist).arg(title))
console.log("Set lyrics for %1 - %2 from %3".
arg(artist).arg(title).arg(name))
toNextFile()
} else {
console.log("No lyrics for %1 - %2".arg(artist).arg(title))
console.log("No lyrics for %1 - %2 from %3".
arg(artist).arg(title).arg(name))
toNextFetcher()
}
} else {
console.log("Request failed for %1 - %2".arg(artist).arg(title))
console.log("Request failed for %1 - %2 from %3".
arg(artist).arg(title).arg(name))
toNextFetcher()
}
toNextFile()
}
}
doc.open("GET", "http://makeitpersonal.co/lyrics?artist=%1&title=%2".
arg(artist).arg(title));
doc.send();
doc.open("GET", fetcher.getUrl(artist, title))
doc.send()
} else {
toNextFile()
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment