Skip to content

Commit

Permalink
Merge pull request #56 from Fabrizz/dev-features
Browse files Browse the repository at this point in the history
V4.0.0 General update
  • Loading branch information
Fabrizz authored Apr 7, 2024
2 parents b547ef3 + ed2db3e commit 8b83353
Show file tree
Hide file tree
Showing 20 changed files with 361 additions and 244 deletions.
Binary file added .github/content/readme/banner-homekit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .github/content/readme/banner-livelyrics.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ test.json
api.json
svgs.html
lyrics.js

vizualizer.html
163 changes: 109 additions & 54 deletions MMM-OnSpotify.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Module.register("MMM-OnSpotify", {
prefersLargeImageSize: false,
// If you selected a high interval, you can hide the progress timestamp
// and animate the seekbar to the timing of the updateInterval, making it look better.
hideTrackLenghtAndAnimateProgress: false,
hideTrackLengthAndAnimateProgress: false,
// Shows the Vibrant output in the console as a palette and color data.
showDebugPalette: false,
// Max age in seconds for personal data. If set to 0 they update when the player changes
Expand All @@ -38,6 +38,8 @@ Module.register("MMM-OnSpotify", {
filterNoticeSubtitle: true,
// Changes the language sent to Spotify
language: config.language,
// Shows the recieved data from the node_helper
debugSpotifyData: false,

updateInterval: {
isPlaying: 1,
Expand Down Expand Up @@ -124,6 +126,10 @@ Module.register("MMM-OnSpotify", {
blurCorrectionInAllSides: false,
alwaysUseDefaultDeviceIcon: false,
showVerticalPipe: true,

// In special use cases where a frontend needs to take over other you can disabl
// the id matching for the frontend, so "multiple" frontends can talk to the module even if not supported
matchBackendUUID: false,
},

start: function () {
Expand All @@ -136,6 +142,10 @@ Module.register("MMM-OnSpotify", {
this.nowDisplaying = null;
this.globalThemeSelected = null;
this.currentStatus = "";
this.backendExpectId = this.config.matchBackendUUID
? Date.now().toString(16)
: "ABC";
this.lastServerId = undefined;
this.retries = 0;
this.lastPrivate = [null, null];
this.lastStatus = "isPlaying";
Expand All @@ -145,7 +155,7 @@ Module.register("MMM-OnSpotify", {
typeof this.config.theming.experimentalCSSOverridesForMM2 === "object";

////////////////////////////////////////////////////////////////////////////////////////////
this.version = "3.3.1";
this.version = "4.0.0";
////////////////////////////////////////////////////////////////////////////////////////////

this.displayUser =
Expand Down Expand Up @@ -195,21 +205,16 @@ Module.register("MMM-OnSpotify", {

this.root = document.querySelector(":root");

this.sendSocketNotification("SET_CREDENTIALS_REFRESH", {
preferences: {
userAffinityUseTracks: this.config.userAffinityUseTracks,
deviceFilter: this.config.deviceFilter,
deviceFilterExclude: this.config.deviceFilterExclude,
},
credentials: {
clientId: this.config.clientID,
clientSecret: this.config.clientSecret,
accessToken: this.config.accessToken,
refreshToken: this.config.refreshToken,
},
language: this.config.language,
});
this.sendCredentialsBackend();
this.updateFetchingLoop(this.config.updateInterval[this.lastStatus]);
if (this.config.debugSpotifyData) console.debug(
`%c· MMM-OnSpotify %c %c DBUG %c`,
"background-color:#84CC16;color:black;border-radius:0.5em",
"",
"background-color:#293f45;color:white;",
"",
this.config,
);
},

getDom: function () {
Expand Down Expand Up @@ -267,9 +272,6 @@ Module.register("MMM-OnSpotify", {
},
getScripts: function () {
let files = [
this.file(
"node_modules/moment-duration-format/lib/moment-duration-format.js",
),
this.file("utils/SpotifyDomBuilder.js"),
// Use a custom build of the "node-vibrant" library that fixes the webworker usage
this.file("vendor/vibrant.worker.min.js"),
Expand All @@ -284,11 +286,6 @@ Module.register("MMM-OnSpotify", {
)
files.push(this.file("node_modules/dompurify/dist/purify.min.js"));

// Only load moment if for some reason MM2 has not loaded it yet, fixes https://github.com/Fabrizz/MMM-OnSpotify/issues/32
// Actually https://github.com/Fabrizz/MMM-OnSpotify/pull/33 fixed it.
if (!("moment" in window)) {
files.push(this.file("node_modules/moment/min/moment.min.js"));
}
return files;
},
getTranslations: function () {
Expand All @@ -302,19 +299,19 @@ Module.register("MMM-OnSpotify", {
this.moduleHidden = true;
this.smartUpdate();
console.info(
`%c· MMM-OnSpotify %c %c[INFO]%c ${this.translate("SUSPEND")}`,
"background-color:#84CC16;color:black;border-radius:0.4em",
`%c· MMM-OnSpotify %c %c INFO %c ${this.translate("SUSPEND")}`,
"background-color:#84CC16;color:black;border-radius:0.5em",
"",
"background-color:darkcyan;color:black;",
"background-color:#02675d;color:white;",
"",
);
},
resume: function () {
console.info(
`%c· MMM-OnSpotify %c %c[INFO]%c ${this.translate("RESUME")}`,
"background-color:#84CC16;color:black;border-radius:0.4em",
`%c· MMM-OnSpotify %c %c INFO %c ${this.translate("RESUME")}`,
"background-color:#84CC16;color:black;border-radius:0.5em",
"",
"background-color:darkcyan;color:black;",
"background-color:#02675d;color:white;",
"",
);
this.moduleHidden = false;
Expand All @@ -324,6 +321,15 @@ Module.register("MMM-OnSpotify", {
socketNotificationReceived: function (notification, payload) {
switch (notification) {
case "PLAYER_DATA":
if (this.config.debugSpotifyData) console.debug(
`%c· MMM-OnSpotify %c %c DBUG %c`,
"background-color:#84CC16;color:black;border-radius:0.5em",
"",
"background-color:#293f45;color:white;",
"",
"PLAYER_DATA",
payload,
);
this.isConnectedToSpotify = true;
this.connectionRetries = 0;
if (!payload.statusPlayerUpdating) this.playerData = payload;
Expand Down Expand Up @@ -364,6 +370,15 @@ Module.register("MMM-OnSpotify", {
});
break;
case "USER_DATA":
if (this.config.debugSpotifyData) console.debug(
`%c· MMM-OnSpotify %c %c DBUG %c`,
"background-color:#84CC16;color:black;border-radius:0.5em",
"",
"background-color:#293f45;color:white;",
"",
"USER_DATA",
payload,
);
this.userData = {
...payload,
subtitleOverride:
Expand All @@ -376,16 +391,25 @@ Module.register("MMM-OnSpotify", {
this.smartUpdate("USER_DATA");
if (this.userData.product !== "premium")
console.warn(
`%c· MMM-OnSpotify %c %c[WARN]%c ${this.translate(
`%c· MMM-OnSpotify %c %c WARN %c ${this.translate(
"PRODUCT_WARNING",
)}`,
"background-color:#84CC16;color:black;border-radius:0.4em",
"background-color:#84CC16;color:black;border-radius:0.5em",
"",
"background-color:orange;color:black;",
"background-color:#754700;color:white;",
"",
);
break;
case "AFFINITY_DATA":
if (this.config.debugSpotifyData) console.debug(
`%c· MMM-OnSpotify %c %c DBUG %c`,
"background-color:#84CC16;color:black;border-radius:0.5em",
"",
"background-color:#293f45;color:white;",
"",
"AFFINITY_DATA",
payload,
);
this.affinityData = payload;
this.affinityData.age = Date.now();
this.requestAffinityData = false;
Expand All @@ -394,18 +418,31 @@ Module.register("MMM-OnSpotify", {
case "CONNECTION_ERRONED":
if (this.isConnectedToSpotify) {
console.info(
`%c· MMM-OnSpotify %c %c[WARN]%c ${this.translate(
`%c· MMM-OnSpotify %c %c INFO %c ${this.translate(
"CONNECTION_WARNING",
)}`,
"background-color:#84CC16;color:black;border-radius:0.4em",
"background-color:#84CC16;color:black;border-radius:0.5em",
"",
"background-color:orange;color:black;",
"background-color:#02675d;color:white;",
"",
);
}
this.isConnectedToSpotify = false;
this.smartUpdate("PLAYER_DATA");
break;
case "REQUEST_REAUTH":
if (this.lastServerId === payload) break;
this.lastServerId = payload;
console.warn(
`%c· MMM-OnSpotify %c %c WARN %c ${this.translate("BACKEND_REAUTH")}`,
"background-color:#84CC16;color:black;border-radius:0.5em",
"",
"background-color:#754700;color:white;",
"",
);
this.sendNotification("SERVERSIDE_RESTART");
this.sendCredentialsBackend();
break;
}
},
notificationReceived: function (notification, payload) {
Expand Down Expand Up @@ -443,44 +480,44 @@ Module.register("MMM-OnSpotify", {
break;
case "LIVELYRICS_NOTICE":
console.info(
`%c· MMM-OnSpotify %c %c[INFO]%c ${this.translate(
`%c· MMM-OnSpotify %c %c INFO %c ${this.translate(
"LIVELYRICS_NOTICE",
)}`,
"background-color:#84CC16;color:black;border-radius:0.4em",
"background-color:#84CC16;color:black;border-radius:0.5em",
"",
"background-color:darkcyan;color:black;",
"background-color:#02675d;color:white;",
"",
);
break;
case "ALL_MODULES_STARTED":
if (this.config.showDebugPalette)
console.info(
`%c· MMM-OnSpotify %c %c[INFO]%c ${this.translate(
`%c· MMM-OnSpotify %c %c INFO %c ${this.translate(
"DEBUG_COLORS",
)}`,
"background-color:#84CC16;color:black;border-radius:0.4em",
"background-color:#84CC16;color:black;border-radius:0.5em",
"",
"background-color:darkcyan;color:black;",
"background-color:#02675d;color:white;",
"",
);
if (this.config.theming.spotifyCodeExperimentalShow)
console.info(
`%c· MMM-OnSpotify %c %c[WARN]%c ${this.translate(
`%c· MMM-OnSpotify %c %c WARN %c ${this.translate(
"SPOTIFYCODE_EXPERIMENTAL",
)}`,
"background-color:#84CC16;color:black;border-radius:0.4em",
"background-color:#84CC16;color:black;border-radius:0.5em",
"",
"background-color:orange;color:black;",
"background-color:#754700;color:white;",
"",
);
if (this.config.theming.experimentalCSSOverridesForMM2)
console.info(
`%c· MMM-OnSpotify %c %c[INFO]%c ${this.translate(
`%c· MMM-OnSpotify %c %c INFO %c ${this.translate(
"CSSOVERRIDE_NOTICE",
)}`,
"background-color:#84CC16;color:black;border-radius:0.4em",
"background-color:#84CC16;color:black;border-radius:0.5em",
"",
"background-color:darkcyan;color:black;",
"background-color:#02675d;color:white;",
"",
);
this.sendNotification("LIVELYRICS_GET");
Expand All @@ -501,12 +538,13 @@ Module.register("MMM-OnSpotify", {
},

instantUpdate: function () {
this.sendSocketNotification("REFRESH_PLAYER");
this.sendSocketNotification("REFRESH_PLAYER", this.backendExpectId);
// To prevent multiple api calls, the call is requested but its only called
// here, following the interval of the player.
if (this.requestUserData) this.sendSocketNotification("REFRESH_USER");
if (this.requestUserData)
this.sendSocketNotification("REFRESH_USER", this.backendExpectId);
if (this.requestAffinityData)
this.sendSocketNotification("REFRESH_AFFINITY");
this.sendSocketNotification("REFRESH_AFFINITY", this.backendExpectId);
},

smartUpdate: function (type) {
Expand Down Expand Up @@ -558,12 +596,12 @@ Module.register("MMM-OnSpotify", {
this.retries = this.retries > 25 ? this.retries : this.retries + 1;
if (this.retries === 25) {
console.error(
`%c· MMM-OnSpotify %c %c[ERRO]%c ${this.translate(
`%c· MMM-OnSpotify %c %c ERRO %c ${this.translate(
"CONNECTION_ERROR",
)}`,
"background-color:#84CC16;color:black;border-radius:0.4em",
"background-color:#84CC16;color:black;border-radius:0.5em",
"",
"background-color:darkred;color:black;",
"background-color:#781919;color:white;",
"",
);
this.playerData = {
Expand Down Expand Up @@ -624,6 +662,23 @@ Module.register("MMM-OnSpotify", {

getImage: (im, prefersLarge) =>
im ? (prefersLarge ? im.large : im.medium) : null,
sendCredentialsBackend() {
this.sendSocketNotification("SET_CREDENTIALS_REFRESH", {
preferences: {
userAffinityUseTracks: this.config.userAffinityUseTracks,
deviceFilter: this.config.deviceFilter,
deviceFilterExclude: this.config.deviceFilterExclude,
},
credentials: {
clientId: this.config.clientID,
clientSecret: this.config.clientSecret,
accessToken: this.config.accessToken,
refreshToken: this.config.refreshToken,
},
language: this.config.language,
backendExpectId: this.backendExpectId,
});
},
logBadge: function () {
console.log(
` ⠖ %c by Fabrizz %c ${this.name}`,
Expand Down
Loading

0 comments on commit 8b83353

Please sign in to comment.