diff --git a/MMM-LiveLyrics.js b/MMM-LiveLyrics.js index 53c10e7..dd03599 100644 --- a/MMM-LiveLyrics.js +++ b/MMM-LiveLyrics.js @@ -79,6 +79,7 @@ Module.register("MMM-LiveLyrics", { LIVELYRICS_GET: "LIVELYRICS_GET", ALL_MODULES_STARTED: "ALL_MODULES_STARTED", DOM_OBJECTS_CREATED: "DOM_OBJECTS_CREATED", + SERVERSIDE_RESTART: "SERVERSIDE_RESTART", }, }, @@ -115,7 +116,7 @@ Module.register("MMM-LiveLyrics", { if (this.config.hideStrategy === "mm2") this.config.startHidden = true; /////////////////////// - this.version = "1.2.0"; + this.version = "1.3.0"; /////////////////////// this.config.version = this.version; @@ -131,6 +132,7 @@ Module.register("MMM-LiveLyrics", { useFormatter: this.config.useDefaultSearchFormatter, startHidden: this.config.startHidden, hidesAutomatically: this.config.hideStrategy, + lang: this.config.language ? this.config.language : "en-US", }); this.sendSocketNotification("GET_SERVER", { @@ -233,24 +235,29 @@ Module.register("MMM-LiveLyrics", { if (this.config.updateTopModulesCalcOnData) this.builder.getTopModulesHeight(); break; - case "LYRICS_TOGGLE": - this.moduleHidden ? this.show() : this.hide(); - break; case "LYRICS_SHOW": + this.sendNotification("LYRICS_STATUS", { + type: "external", + show: true, + }); this.show(); break; case "LYRICS_HIDE": + this.sendNotification("LYRICS_STATUS", { + type: "external", + show: false, + }); this.hide(); break; case "ONSPOTIFY_NOTICE": this.masterFound = true; this.enable = true; console.info( - "%c· MMM-LiveLyrics %c %c[INFO]%c " + + "%c· MMM-LiveLyrics %c %c INFO %c " + `${this.translate("ONSPOTIFY_FOUND")} | V${payload.version}`, `background-color:${this.moduleColor};color:black;border-radius:0.4em`, "", - "background-color:darkcyan;color:black;border-radius:0.4em", + "background-color:#02675d;color:white;", "", ); this.moduleHidden @@ -274,8 +281,9 @@ Module.register("MMM-LiveLyrics", { if (this.dynamicTheme && !payload.directColorData) { console.warn( - "%c· MMM-LiveLyrics %c %c[WARN]%c " + - this.translate("DYNAMIC_UNKNOWN"), + `%c· MMM-LiveLyrics %c %c[WARN]%c ${this.translate( + "DYNAMIC_UNKNOWN", + )}`, `background-color:${this.moduleColor};color:black;border-radius:0.4em`, "", "background-color:orange;color:black;border-radius:0.4em", @@ -329,6 +337,16 @@ Module.register("MMM-LiveLyrics", { case "DOM_OBJECTS_CREATED": setTimeout(() => this.builder.getTopModulesHeight(), 2000); break; + case "SERVERSIDE_RESTART": + this.hide(); + this.sendSocketNotification("SET_CREDENTIALS", { + apiKey: this.config.accessToken, + useMultipleArtists: this.config.useMultipleArtistInSearch, + useFormatter: this.config.useDefaultSearchFormatter, + startHidden: this.config.startHidden, + hidesAutomatically: this.config.hideStrategy, + }); + break; default: break; } @@ -366,12 +384,32 @@ Module.register("MMM-LiveLyrics", { case "SET": switch (payload) { case "TOGGLE": - this.moduleHidden ? this.userShow() : this.userHide(); + if (this.moduleHidden) { + this.sendNotification("LYRICS_STATUS", { + type: "internal", + show: true, + }); + this.userShow(); + } else { + this.sendNotification("LYRICS_STATUS", { + type: "internal", + show: false, + }); + this.userHide(); + } break; case "SHOW": + this.sendNotification("LYRICS_STATUS", { + type: "internal", + show: true, + }); this.userShow(); break; case "HIDE": + this.sendNotification("LYRICS_STATUS", { + type: "internal", + show: false, + }); this.userHide(); break; default: @@ -396,20 +434,21 @@ Module.register("MMM-LiveLyrics", { if (this.config.logSuspendResume && !this.firstSuspend) console.info( - "%c· MMM-LiveLyrics %c %c[INFO]%c " + this.translate("SUSPEND"), + `%c· MMM-LiveLyrics %c %c INFO %c ${this.translate("SUSPEND")}`, `background-color:${this.moduleColor};color:black;border-radius:0.4em`, "", - "background-color:darkcyan;color:black;border-radius:0.4em", + "background-color:#02675d;color:white;", "", ); if (this.firstSuspend) { console.info( - "%c· MMM-LiveLyrics %c %c[INFO]%c " + - this.translate("STARTS_SUSPENDED"), + `%c· MMM-LiveLyrics %c %c INFO %c ${this.translate( + "STARTS_SUSPENDED", + )}`, `background-color:${this.moduleColor};color:black;border-radius:0.4em`, "", - "background-color:darkcyan;color:black;border-radius:0.4em", + "background-color:#02675d;color:white;", "", ); this.firstSuspend = false; @@ -428,10 +467,10 @@ Module.register("MMM-LiveLyrics", { if (this.config.logSuspendResume) console.info( - "%c· MMM-LiveLyrics %c %c[INFO]%c " + this.translate("RESUME"), + `%c· MMM-LiveLyrics %c %c INFO %c ${this.translate("RESUME")}`, `background-color:${this.moduleColor};color:black;border-radius:0.4em`, "", - "background-color:darkcyan;color:black;border-radius:0.4em", + "background-color:#02675d;color:white;", "", ); }, @@ -452,6 +491,7 @@ Module.register("MMM-LiveLyrics", { }, selectScrollType(name) { + // eslint-disable-next-line no-param-reassign if (typeof name !== "string") name = ""; switch (name.toLowerCase()) { case "byanimationframe": diff --git a/node_helper.js b/node_helper.js index 59202d6..72e57dc 100644 --- a/node_helper.js +++ b/node_helper.js @@ -14,7 +14,9 @@ const os = require("os"); module.exports = NodeHelper.create({ start: function () { - console.log("\x1b[46m%s\x1b[0m", "[Node Helper] Init >> " + this.name); + console.log( + "[\x1b[35mMMM-LiveLyrics\x1b[0m] by Fabrizz >> Node helper loaded.", + ); this.fetcher = null; this.frontendAvailable = false; this.serverData = null; diff --git a/utils/LyricsFetcher.js b/utils/LyricsFetcher.js index 57970e9..4b3d5df 100644 --- a/utils/LyricsFetcher.js +++ b/utils/LyricsFetcher.js @@ -5,11 +5,13 @@ * By Fabrizz <3 | https://github.com/Fabrizz/MMM-LiveLyrics * * Inspired by Faisal Arshed https://github.com/farshed + * + * This is not the best code! This module is old and needs a refactor. + * Also web scrapping is not the best but hey, it still works after 2 years. */ -// Use node fetch as most MM2 installs use older node -const fetch = require("node-fetch"); const cheerio = require("cheerio"); +const fetch = require("node-fetch"); const LILYREGEX = require("../LILYREGEX"); const baseApiURL = "https://api.genius.com"; @@ -18,9 +20,10 @@ const queryRegex = /* TODO: Update regex to take in mind letter only titles (512) */ -module.exports = class SpotifyFetcher { +module.exports = class LyricsFetcher { constructor(payload) { this.apiKey = payload.apiKey; + this.lang = payload.lang; this.regex = LILYREGEX; this.userRegex = @@ -34,12 +37,12 @@ module.exports = class SpotifyFetcher { if (payload.userRegex && !(this.userRegex instanceof RegExp)) console.error( "\x1b[41m%s\x1b[0m", - "[MMM-LiveLyrics] [Node Helper] LyricsFetcher >> Malformed RegExp user input.", + "[MMM-LiveLyrics] [Node Helper] RegexChecker >> Malformed RegExp user input.", ); if (payload.userRegexlyrics && !(this.userRegexlyrics instanceof RegExp)) console.error( "\x1b[41m%s\x1b[0m", - "[MMM-LiveLyrics] [Node Helper] LyricsFetcher >> Malformed RegExp user input.", + "[MMM-LiveLyrics] [Node Helper] RegexChecker >> Malformed RegExp user input.", ); } @@ -100,20 +103,39 @@ module.exports = class SpotifyFetcher { const ly = await this.fetchLyrics(final.result.url); const $ = cheerio.load(ly); - let lyrics = $('div[class="lyrics"]').text().trim(); - if (!lyrics) { - lyrics = ""; - $('div[class^="Lyrics__Container"]').each((i, elem) => { - if ($(elem).text().length !== 0) { - let snippet = $(elem) - .html() - .replace(/
/g, "\n") - .replace(/<(?!\s*br\s*\/?)[^>]+>/gi, ""); - lyrics += $("