From 2f0a85d58eaaca4822bea861ca4c65cd893cd011 Mon Sep 17 00:00:00 2001 From: Fabrizio <65259076+Fabrizz@users.noreply.github.com> Date: Sun, 25 Feb 2024 01:48:23 -0300 Subject: [PATCH 1/4] Add reauth on server restart --- MMM-LiveLyrics.js | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/MMM-LiveLyrics.js b/MMM-LiveLyrics.js index 53c10e7..eb32064 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.2.1"; /////////////////////// this.config.version = this.version; @@ -274,8 +275,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 +331,15 @@ Module.register("MMM-LiveLyrics", { case "DOM_OBJECTS_CREATED": setTimeout(() => this.builder.getTopModulesHeight(), 2000); break; + case "SERVERSIDE_RESTART": + 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; } @@ -396,7 +407,7 @@ 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", @@ -405,8 +416,9 @@ Module.register("MMM-LiveLyrics", { 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", @@ -428,7 +440,7 @@ 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", @@ -452,6 +464,7 @@ Module.register("MMM-LiveLyrics", { }, selectScrollType(name) { + // eslint-disable-next-line no-param-reassign if (typeof name !== "string") name = ""; switch (name.toLowerCase()) { case "byanimationframe": From 9236bcfdb61c4dd11f4857c95ebf2ea95c80813d Mon Sep 17 00:00:00 2001 From: Fabrizio <65259076+Fabrizz@users.noreply.github.com> Date: Sun, 25 Feb 2024 01:57:33 -0300 Subject: [PATCH 2/4] Add hide on server error --- MMM-LiveLyrics.js | 1 + 1 file changed, 1 insertion(+) diff --git a/MMM-LiveLyrics.js b/MMM-LiveLyrics.js index eb32064..8154a83 100644 --- a/MMM-LiveLyrics.js +++ b/MMM-LiveLyrics.js @@ -332,6 +332,7 @@ Module.register("MMM-LiveLyrics", { setTimeout(() => this.builder.getTopModulesHeight(), 2000); break; case "SERVERSIDE_RESTART": + this.hide(); this.sendSocketNotification("SET_CREDENTIALS", { apiKey: this.config.accessToken, useMultipleArtists: this.config.useMultipleArtistInSearch, From 77d293913a8647933f58a586b1f3dac509292bfc Mon Sep 17 00:00:00 2001 From: Fabrizio <65259076+Fabrizz@users.noreply.github.com> Date: Fri, 27 Dec 2024 19:30:19 -0300 Subject: [PATCH 3/4] Fixed Genius scrapping --- MMM-LiveLyrics.js | 50 +++++++++++++++++------ node_helper.js | 4 +- utils/LyricsFetcher.js | 93 ++++++++++++++++++++++++++++++------------ 3 files changed, 108 insertions(+), 39 deletions(-) diff --git a/MMM-LiveLyrics.js b/MMM-LiveLyrics.js index 8154a83..6a6e772 100644 --- a/MMM-LiveLyrics.js +++ b/MMM-LiveLyrics.js @@ -132,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", { @@ -234,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 @@ -378,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: @@ -408,21 +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( + `%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; @@ -441,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;", "", ); }, 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 += $("