Skip to content

Commit

Permalink
fix: adapt extensions to new player data structure
Browse files Browse the repository at this point in the history
  • Loading branch information
rxri authored Aug 31, 2023
1 parent a6b43c1 commit 033929c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 49 deletions.
21 changes: 5 additions & 16 deletions Extensions/shuffle+.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,33 +456,22 @@
return array.filter(track => track);
}

async function Queue(list, context = null, type) {
async function Queue(list, type) {
const count = list.length;

list.push("spotify:delimiter");

await Spicetify.Platform.PlayerAPI.clearQueue();

await Spicetify.CosmosAsync.put("sp://player/v2/main/queue", {
queue_revision: Spicetify.Queue?.queueRevision,
next_tracks: list.map(uri => ({
Spicetify.Platform.PlayerAPI.addToQueue(
list.map(uri => ({
uri,
provider: "context",
metadata: {
is_queued: "false"
}
})),
prev_tracks: Spicetify.Queue?.prevTracks
});

if (context) {
await Spicetify.CosmosAsync.post("sp://player/v2/main/update", {
context: {
uri: context,
url: "context://" + context
}
});
}
}))
);

Spicetify.Player.next();

Expand Down
36 changes: 22 additions & 14 deletions Extensions/webnowplaying.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
class WNPReduxWebSocket {
_ws = null;
cache = new Map();
playerCache = null;
reconnectCount = 0;
updateInterval = null;
communicationRevision = null;
Expand All @@ -43,18 +44,24 @@ class WNPReduxWebSocket {

constructor() {
this.init();
Spicetify.CosmosAsync.sub("sp://player/v2/main", this.updateSpicetifyInfo.bind(this));

setInterval(() => {
if (objectsAreEqual(Spicetify.Player.data, playerCache)) return;

playerCache = Spicetify.Player.data;
this.updateSpicetifyInfo(playerCache);
}, 100);
}

updateSpicetifyInfo(data) {
if (!data?.track?.metadata) return;
const meta = data.track.metadata;
if (!data?.item?.metadata) return;
const meta = data.item.metadata;
this.spicetifyInfo.title = meta.title;
this.spicetifyInfo.album = meta.album_title;
this.spicetifyInfo.duration = timeInSecondsToString(Math.round(parseInt(meta.duration) / 1000));
this.spicetifyInfo.state = !data.is_paused ? "PLAYING" : "PAUSED";
this.spicetifyInfo.repeat = data.options.repeating_track ? "ONE" : data.options.repeating_context ? "ALL" : "NONE";
this.spicetifyInfo.shuffle = data.options.shuffling_context;
this.spicetifyInfo.state = !data.isPaused ? "PLAYING" : "PAUSED";
this.spicetifyInfo.repeat = data.repeat === 2 ? "ONE" : data.repeat === 1 ? "ALL" : "NONE";
this.spicetifyInfo.shuffle = data.shuffle;
this.spicetifyInfo.artist = meta.artist_name;
let artistCount = 1;
while (meta["artist_name:" + artistCount]) {
Expand All @@ -63,7 +70,7 @@ class WNPReduxWebSocket {
}
if (!this.spicetifyInfo.artist) this.spicetifyInfo.artist = meta.album_title; // Podcast

Spicetify.Platform.LibraryAPI.contains(data.track.uri).then(([added]) => (this.spicetifyInfo.rating = added ? 5 : 0));
Spicetify.Platform.LibraryAPI.contains(data.item.uri).then(([added]) => (this.spicetifyInfo.rating = added ? 5 : 0));

const cover = meta.image_xlarge_url;
if (cover?.indexOf("localfile") === -1) this.spicetifyInfo.cover = "https://i.scdn.co/image/" + cover.substring(cover.lastIndexOf(":") + 1);
Expand Down Expand Up @@ -99,13 +106,10 @@ class WNPReduxWebSocket {
if (this.isClosed) return;
this.close(true);
// Reconnects once per second for 30 seconds, then with a exponential backoff of (2^reconnectAttempts) up to 60 seconds
this.reconnectTimeout = setTimeout(
() => {
this.init();
this.reconnectAttempts += 1;
},
Math.min(1000 * (this.reconnectAttempts <= 30 ? 1 : 2 ** (this.reconnectAttempts - 30)), 60000)
);
this.reconnectTimeout = setTimeout(() => {
this.init();
this.reconnectAttempts += 1;
}, Math.min(1000 * (this.reconnectAttempts <= 30 ? 1 : 2 ** (this.reconnectAttempts - 30)), 60000));
}

send(data) {
Expand Down Expand Up @@ -172,6 +176,10 @@ class WNPReduxWebSocket {
}
}

function objectsAreEqual(obj1, obj2) {
return JSON.stringify(obj1) === JSON.stringify(obj2);
}

function OnMessageLegacy(self, message) {
// Quite lengthy functions because we optimistically update spicetifyInfo after receiving events.
try {
Expand Down
36 changes: 17 additions & 19 deletions jsHelper/spicetifyWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -581,30 +581,28 @@ window.Spicetify = {
};

setInterval(() => {
playerState.current = Spicetify.Platform.PlayerAPI._state;
if (objectsAreEqual(Spicetify.Platform.PlayerAPI._state, playerState.cache)) return;

if (!objectsAreEqual(playerState.current, playerState.cache)) {
Spicetify.Player.data = Spicetify.Platform.PlayerAPI._state;
playerState.current = Spicetify.Platform.PlayerAPI._state;
Spicetify.Player.data = playerState.current;

// compatibility
// TODO: remove in next few releases
Spicetify.Player.data["track"] = Spicetify.Player.data.item;
playerState.current = Spicetify.Player.data;
// compatibility
// TODO: remove in next few releases
Spicetify.Player.data["track"] = Spicetify.Player.data.item;

if (playerState.cache?.item.uri !== playerState.current.item.uri) {
const event = new Event("songchange");
event.data = playerState.current;
Spicetify.Player.dispatchEvent(event);
}

if (playerState.cache?.isPaused !== playerState.current.isPaused) {
const event = new Event("onplaypause");
event.data = playerState.current;
Spicetify.Player.dispatchEvent(event);
}
if (playerState.cache?.item.uri !== playerState.current?.item?.uri) {
const event = new Event("songchange");
event.data = playerState.current;
Spicetify.Player.dispatchEvent(event);
}

playerState.cache = playerState.current;
if (playerState.cache?.isPaused !== playerState.current?.isPaused) {
const event = new Event("onplaypause");
event.data = playerState.current;
Spicetify.Player.dispatchEvent(event);
}

playerState.cache = playerState.current;
}, 100);

setInterval(() => {
Expand Down

0 comments on commit 033929c

Please sign in to comment.