Skip to content

Commit

Permalink
feat: support import from songbook.fun
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Chodur <[email protected]>
  • Loading branch information
FUSAKLA committed Feb 16, 2024
1 parent 2fa2e26 commit 4f351fc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
6 changes: 4 additions & 2 deletions appsscript.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {}
Expand All @@ -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/"
]
}
7 changes: 6 additions & 1 deletion src/import/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/,
Expand Down Expand Up @@ -75,7 +80,7 @@ function writeChordsToDocument(
emptyLines = 0;
}
body.appendParagraph(lineText);
beginning = false
beginning = false;
}

if (chords.videoLink !== "") {
Expand Down
37 changes: 37 additions & 0 deletions src/import/sites/songbookFun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
function getSongbookFunChords(url: string) {
const html = UrlFetchApp.fetch(url).getContentText();
const chordsDivRegexp = /<section class="Col2">\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 class="Song-chord" data-chord="([^"]+)"><\/span>/g,
function (a: string, b: string) {
return "%" + b.replace(/,/g, "").replace(//g, "") + "%";
}
)
.replace(/<\/p>/g, " \n")
.replace(/<br>/g, " \n")
.replace(/&#x27;/g, "'")
.replace(/<[^>]+>/g, "");
const metadataRegexp =
/<title>(.+) Chords &amp; 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")),
};
}

0 comments on commit 4f351fc

Please sign in to comment.