From 4f351fc9d3bddc823886cc658c9e55a4e3118f7b Mon Sep 17 00:00:00 2001 From: Martin Chodur Date: Fri, 16 Feb 2024 22:26:17 +0100 Subject: [PATCH] feat: support import from songbook.fun Signed-off-by: Martin Chodur --- appsscript.json | 6 ++++-- src/import/import.ts | 7 ++++++- src/import/sites/songbookFun.ts | 37 +++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 src/import/sites/songbookFun.ts diff --git a/appsscript.json b/appsscript.json index bfac12e3..46ecd2a6 100644 --- a/appsscript.json +++ b/appsscript.json @@ -27,7 +27,8 @@ "https://akordy.kytary.cz/", "https://pdfmerge.w69b.com/", "https://api.qrserver.com/", - "https://acordes.lacuerda.net/" + "https://acordes.lacuerda.net/", + "https://www.songbook.fun/" ] }, "docs": {} @@ -43,6 +44,7 @@ "https://akordy.kytary.cz/", "https://api.qrserver.com/", "https://acordes.lacuerda.net/", - "https://velky-zpevnik.cz/" + "https://velky-zpevnik.cz/", + "https://www.songbook.fun/" ] } diff --git a/src/import/import.ts b/src/import/import.ts index dcdd638c..b4f11c2e 100644 --- a/src/import/import.ts +++ b/src/import/import.ts @@ -18,6 +18,11 @@ function getSupportedChordsImportSites() { regexp: /tabs\.ultimate-guitar\.com/, processor: getUltimateGuitarChords, }, + { + domain: "songbook.fun", + regexp: /songbook\.fun/, + processor: getSongbookFunChords, + }, { domain: "lacuerda.net", regexp: /acordes\.lacuerda\.net/, @@ -75,7 +80,7 @@ function writeChordsToDocument( emptyLines = 0; } body.appendParagraph(lineText); - beginning = false + beginning = false; } if (chords.videoLink !== "") { diff --git a/src/import/sites/songbookFun.ts b/src/import/sites/songbookFun.ts new file mode 100644 index 00000000..e9d01629 --- /dev/null +++ b/src/import/sites/songbookFun.ts @@ -0,0 +1,37 @@ +function getSongbookFunChords(url: string) { + const html = UrlFetchApp.fetch(url).getContentText(); + const chordsDivRegexp = /
\s*(\S[\s\S]*)<\/section>/g; + const chordsDivMatch = html.match(chordsDivRegexp); + if (chordsDivMatch === null) { + throw Error("failed to find the songtext div."); + } + const chordsText = chordsDivMatch[0] + .replace( + /<\/span>/g, + function (a: string, b: string) { + return "%" + b.replace(/,/g, "").replace(/↓/g, "") + "%"; + } + ) + .replace(/<\/p>/g, " \n") + .replace(/
/g, " \n") + .replace(/'/g, "'") + .replace(/<[^>]+>/g, ""); + const metadataRegexp = + /(.+) Chords & Tabs by (.+) for Ukulele.*<\/title>/; + const metadataMatch = html.match(metadataRegexp); + if (metadataMatch === null || metadataMatch.length !== 3) { + throw Error("failed to find the chords metadata."); + } + let youtubeLink = ""; + const youtubeLinkRegexp = /href="([^"]+)".*title="YouTube"/; + const youtubeMatch = html.match(youtubeLinkRegexp); + if (youtubeMatch && youtubeMatch.length === 2) { + youtubeLink = youtubeMatch[1]; + } + return { + artist: metadataMatch[1], + songName: metadataMatch[2], + videoLink: youtubeLink, + rows: extractInlineChords(chordsText.split("\n")), + }; +}