Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Canva update #74

Merged
merged 21 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
unistall axios
  • Loading branch information
dientuki committed May 1, 2024
commit 5af430d1f86caf5063ff305035b2eb54dd27f2c0
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"homepage": "https://github.com/Fabrizz/MMM-OnSpotify#readme",
"license": "MIT",
"dependencies": {
"axios": "^1.6.8",
"body-parser": "^1.20.1",
"cookie-parser": "^1.4.6",
"dompurify": "^3.0.6",
Expand Down
43 changes: 24 additions & 19 deletions utils/SpotifyFetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
*/

// Use node fetch as most MM2 installs use older node

const fetch = require("node-fetch");
const axios = require('axios');
const tokenRefreshBase = "https://accounts.spotify.com";
const userBase = "https://api.spotify.com";
const openSpotify = "https://open.spotify.com/";
const canvasesUrl = 'https://spclient.wg.spotify.com/canvaz-cache/v0/canvases';
const canvas = require('./canvas_pb.js');

module.exports = class SpotifyFetcher {
Expand Down Expand Up @@ -44,38 +43,44 @@ module.exports = class SpotifyFetcher {
}

async getCanvas(trackUri) {
let canvasRequest = new canvas.CanvasRequest();
const canvasesUrl = 'https://spclient.wg.spotify.com/canvaz-cache/v0/canvases';
const canvasToken = await this.getCanvasToken();

let canvasRequest = new canvas.CanvasRequest();
let spotifyTrack = new canvas.CanvasRequest.Track();

spotifyTrack.setTrackUri(trackUri);
canvasRequest.addTracks(spotifyTrack);
let requestBytes = canvasRequest.serializeBinary();

const options = {
responseType: 'arraybuffer',
method: 'POST',
headers: {
'accept': 'application/protobuf',
'content-type': 'application/x-www-form-urlencoded',
'accept-language': 'en',
'user-agent': 'Spotify/8.5.49 iOS/Version 13.3.1 (Build 17D50)',
'accept-encoding': 'gzip, deflate, br',
'authorization': `Bearer ${canvasToken.accessToken}`,
},
body: requestBytes
};

try {
const response = await fetch(canvasesUrl, options);
if (!response.ok) {
console.log(`ERROR ${canvasesUrl}: ${response.status} ${response.statusText}`);
const errorData = await response.json();
if (errorData.error) {
console.log(errorData.error);
}
return null; // or handle error according to your needs
}
const responseData = await response.arrayBuffer();
return canvas.CanvasResponse.deserializeBinary(new Uint8Array(responseData)).toObject();
} catch (error) {
console.log(`ERROR ${canvasesUrl}: ${error}`);
return null; // or handle error according to your needs
}
return axios.post(canvasesUrl, requestBytes, options)
.then(response => {
if (response.statusText !== 'OK') {
console.log(`ERROR ${canvasesUrl}: ${response.status} ${response.statusText}`);
if (response.data.error) {
console.log(response.data.error);
}
} else {
return canvas.CanvasResponse.deserializeBinary(response.data).toObject();
}
})
.catch(error => console.log(`ERROR ${canvasesUrl}: ${error}`));

}

formatTime(milliseconds) {
Expand Down