Created
January 30, 2024 15:17
-
-
Save JunkMeal/7192382a70d7d88b9090962afcf1e53e to your computer and use it in GitHub Desktop.
Get audio from Forvo
This file contains hidden or 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
function selectin(str, startstr, endstr) { | |
const starti = str.indexOf(startstr) + startstr.length | |
return str.substring(starti, str.indexOf(endstr, starti)) | |
} | |
function selectinall(str, startstr, endstr) { | |
let result = [] | |
let starti = 0 | |
while (starti < str.length) { | |
const startIndex = str.indexOf(startstr, starti) | |
if (startIndex == -1) break | |
const endIndex = str.indexOf(endstr, startIndex + startstr.length) | |
if (endIndex == -1) break | |
const selectedText = str.substring(startIndex + startstr.length, endIndex) | |
result.push(selectedText) | |
starti = endIndex + endstr.length | |
} | |
return result | |
} | |
async function forvo(word, lang) { | |
const html = await (await fetch(`https://forvo.com/word/${word}/`)).text() | |
const pack = selectin(html, `id="pronunciations-list-${lang}">`, "</ul>") | |
const urls = [] | |
for (const args of selectinall(pack, "Play(", ")")) { | |
if (args.split(",")[4] != "''") { | |
urls.push("https://audio12.forvo.com/audios/mp3/" + atob(args.split(",")[4].slice(1, -1))) | |
continue | |
} | |
urls.push("https://audio12.forvo.com/mp3/" + atob(args.split(",")[1].slice(1, -1))) | |
} | |
return urls | |
} | |
// Example | |
console.log(await forvo("Koch", "de")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sadly doesn't work because cloudflare detects fetch calls! It will work if you swap fetch for something else and make it request with browser headers