Improve example JavaScript

* Move querying lyrics into separate module
* Avoid out of service message from being used as lyrics
This commit is contained in:
Martchus 2023-08-05 23:12:39 +02:00
parent a191aebd8a
commit dace19b2bf
2 changed files with 30 additions and 17 deletions

View File

@ -0,0 +1,27 @@
import * as http from "http.js"
function waitFor(signal) {
signal.connect(() => { utility.exit(); });
utility.exec();
}
function queryMakeItPersonal(searchCriteria) {
const lyricsModel = utility.queryMakeItPersonal(searchCriteria);
if (!lyricsModel.areResultsAvailable) {
waitFor(lyricsModel.resultsAvailable);
}
if (!lyricsModel.fetchLyrics(lyricsModel.index(0, 0))) {
waitFor(lyricsModel.lyricsAvailable);
}
const lyrics = lyricsModel.lyricsValue(lyricsModel.index(0, 0));
if (lyrics && lyrics.startsWith("Bots have beat this API")) {
return undefined;
}
return lyrics;
}
export function queryLyrics(searchCriteria) {
return queryMakeItPersonal(searchCriteria);
}

View File

@ -1,5 +1,4 @@
// import another module as an example how imports work import * as metadatasearch from "metadatasearch.js"
import * as http from "http.js"
export function main(file) { export function main(file) {
// iterate though all tags of the file to change fields in all of them // iterate though all tags of the file to change fields in all of them
@ -21,11 +20,6 @@ function isString(value) {
return typeof(value) === "string" || value instanceof String; return typeof(value) === "string" || value instanceof String;
} }
function waitFor(signal) {
signal.connect(() => { utility.exit(); });
utility.exec();
}
function logTagInfo(file, tag) { function logTagInfo(file, tag) {
// log tag type and supported fields // log tag type and supported fields
const fields = tag.fields; const fields = tag.fields;
@ -84,17 +78,9 @@ function addLyrics(file, tag) {
} }
const firstTitle = fields.title?.[0]?.content; const firstTitle = fields.title?.[0]?.content;
const firstArtist = fields.artist?.[0]?.content; const firstArtist = fields.artist?.[0]?.content;
if (!firstTitle || !firstArtist) { if (firstTitle && firstArtist) {
return; fields.lyrics = metadatasearch.queryLyrics({title: firstTitle, artist: firstArtist});
} }
const lyricsModel = utility.queryMakeItPersonal({title: firstTitle, artist: firstArtist});
if (!lyricsModel.areResultsAvailable) {
waitFor(lyricsModel.resultsAvailable);
}
if (!lyricsModel.fetchLyrics(lyricsModel.index(0, 0))) {
waitFor(lyricsModel.lyricsAvailable);
}
fields.lyrics = lyricsModel.lyricsValue(lyricsModel.index(0, 0));
} }
function addMiscFields(file, tag) { function addMiscFields(file, tag) {