Skip to content

Commit

Permalink
Merge pull request #11 from Fabrizz/dev
Browse files Browse the repository at this point in the history
Fixed Genius data fetching
  • Loading branch information
Fabrizz authored Dec 27, 2024
2 parents af5bb26 + 1de74d7 commit 8bfb523
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 43 deletions.
72 changes: 56 additions & 16 deletions MMM-LiveLyrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},

Expand Down Expand Up @@ -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;
Expand All @@ -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", {
Expand Down Expand Up @@ -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
Expand All @@ -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",
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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:
Expand All @@ -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;
Expand All @@ -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;",
"",
);
},
Expand All @@ -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":
Expand Down
4 changes: 3 additions & 1 deletion node_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
93 changes: 67 additions & 26 deletions utils/LyricsFetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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 =
Expand All @@ -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.",
);
}

Expand Down Expand Up @@ -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(/<br>/g, "\n")
.replace(/<(?!\s*br\s*\/?)[^>]+>/gi, "");
lyrics += $("<textarea/>").html(snippet).text().trim() + "\n\n";
}

let lyrics = "";
let $lyrics = $('div[id="lyrics-root"]').text().trim();

if ($lyrics) {
$('div[data-lyrics-container="true"]').each((i, e) => {
const lyn =
$('<div>')
.html($(e)
.html()
.replace(/<br\s*\/?>/g, "\n")
.replace(/<(?!\s*br\s*\/?)[^>]+>/gi, "")
)
.text()
.trim();

lyrics += lyn + "\n\n";
});

// Genius changed their layout
// lyrics = "";
// $('div[class^="Lyrics__Container"]').each((i, elem) => {
// if ($(elem).text().length !== 0) {
// let snippet = $(elem)
// .html()
// .replace(/<br>/g, "\n")
// .replace(/<(?!\s*br\s*\/?)[^>]+>/gi, "");
// lyrics += $("<textarea/>").html(snippet).text().trim() + "\n\n";
// }
// });

}
lyrics ? (lyrics = lyrics.trim()) : (lyrics = null);
lyrics.length > 1 ? (lyrics = lyrics.trim()) : (lyrics = null);

if (this.userRegexlyrics && lyrics)
lyrics = lyrics.replace(this.userRegexlyrics, "");
Expand All @@ -132,12 +154,17 @@ module.exports = class SpotifyFetcher {
} catch (error) {
console.error(
"\x1b[41m%s\x1b[0m",
"[MMM-LiveLyrics] [Node Helper] LyricsFetcher >> Malformed RegExp user input.",
"[MMM-LiveLyrics] [Node Helper] LyricsProcess >> Could be: Malformed RegExp user input.",
error,
);
}
}

/**
* @param {string} title
* @param {string} artists
* @returns
* */
getQuery(title, artists) {
if (!title && !artists) return null;
const arts = artists.split(",").map((a) => a.trim());
Expand All @@ -158,6 +185,10 @@ module.exports = class SpotifyFetcher {
return query;
}

/**
* @param {string} query
* @returns
* */
searchSong(query) {
return fetch(
new URL(
Expand All @@ -175,14 +206,24 @@ module.exports = class SpotifyFetcher {
});
}

fetchLyrics(url) {
return fetch(url, {
method: "GET",
referrerPolicy: "no-referrer",
})
.then((response) => response.text())
.catch((error) => {
console.log(error);
/**
* @param {string} url
* @returns
* */
async fetchLyrics(url) {
try {
const response = await fetch(url, {
method: "GET",
headers: {
"Accept-Language": `${this.lang};q=0.9,en-US,en;q=0.8`,
},
});
return await await response.text();
} catch (error) {
return console.error(
"\x1b[41m%s\x1b[0m",
"[MMM-LiveLyrics] [Node Helper] LyricsFetcher >> ",
error);
}
}
};

0 comments on commit 8bfb523

Please sign in to comment.