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

Fix multiple songInfo calls #439

Closed
wants to merge 10 commits into from
36 changes: 17 additions & 19 deletions plugins/tuna-obs/back.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
const { ipcRenderer } = require("electron");
const fetch = require('node-fetch');

const registerCallback = require("../../providers/song-info");

const post = (data) => {
const post = async (data) => {
const port = 1608;
headers = {'Content-Type': 'application/json',
'Accept': 'application/json',
'Access-Control-Allow-Headers': '*',
'Access-Control-Allow-Origin': '*'}
const url = `http://localhost:${port}/`;
fetch(url, {method: 'POST', headers, body:JSON.stringify({data})});
await fetch(url, {method: 'POST', headers, body:JSON.stringify({data})});
}

module.exports = async (win) => {
registerCallback((songInfo) => {
module.exports = async () => {
registerCallback(async (songInfo) => {

if (songInfo.title?.length === 0 && songInfo.artist?.length === 0) {
return;
}

// Register the callback
if (songInfo.title.length === 0 && songInfo.artist.length === 0) {
return;
}

const duration = Number(songInfo.songDuration)*1000
const progress = Number(songInfo.elapsedSeconds)*1000
const cover_url = songInfo.imageSrc
const album_url = songInfo.imageSrc
const title = songInfo.title
const artists = [songInfo.artist]
const status = !songInfo.isPaused ? 'Playing': 'Paused'
post({ cover_url, title, artists, status, progress, duration, album_url});
})
const duration = Number(songInfo.songDuration) * 1000
const progress = Number(songInfo.elapsedSeconds) * 1000
const cover_url = songInfo.imageSrc
const album_url = songInfo.imageSrc
const title = songInfo.title
const artists = [songInfo.artist]
const status = !songInfo.isPaused ? 'Playing' : 'Paused'
await post({cover_url, title, artists, status, progress, duration, album_url});
})
}
74 changes: 54 additions & 20 deletions providers/song-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,26 @@ const getProgress = async (win) => {
);
};

const getImageSrc = async (win) => {
return win.webContents.executeJavaScript(
'document.querySelector(".ytmusic-player-bar .ytmusic-player-bar img")?.src'
);

}

// Grab the native image using the src
const getImage = async (src) => {
const result = await fetch(src);
const buffer = await result.buffer();
const output = nativeImage.createFromBuffer(buffer);
if (output.isEmpty() && !src.endsWith(".jpg") && src.includes(".jpg")) { // fix hidden webp files (https://github.com/th-ch/youtube-music/issues/315)
return getImage(src.slice(0, src.lastIndexOf(".jpg")+4));
} else {
return output;
}
};
if (src) {
const result = await fetch(src);
const buffer = await result.buffer();
const output = nativeImage.createFromBuffer(buffer);
if (output.isEmpty() && !src.endsWith(".jpg") && src.includes(".jpg")) { // fix hidden webp files (https://github.com/th-ch/youtube-music/issues/315)
return getImage(src.slice(0, src.lastIndexOf(".jpg") + 4));
} else {
return output;
}
};
}

// To find the paused status, we check if the title contains `-`
const getPausedStatus = async (win) => {
Expand All @@ -39,6 +48,25 @@ const getArtist = async (win) => {
`);
}

const getTitle = async (win) => {
return win.webContents.executeJavaScript(`
document.querySelector(".ytmusic-player-bar yt-formatted-string.title")
?.textContent
`);
}

const getSongDuration = async (win) => {
return win.webContents.executeJavaScript(`
document.querySelector("#progress-bar")
?.ariaValueMax
`);
}

const getSongURL = async (win) => {
return win.webContents.getURL().split("&")[0];
}


// Fill songInfo with empty values
const songInfo = {
title: "",
Expand All @@ -53,19 +81,25 @@ const songInfo = {
url: "",
};


const handleData = async (responseText, win) => {
let data = JSON.parse(responseText);
songInfo.title = cleanupName(data?.videoDetails?.title);
songInfo.artist =
(await getArtist(win)) || cleanupName(data?.videoDetails?.author);
songInfo.views = data?.videoDetails?.viewCount;
songInfo.imageSrc = data?.videoDetails?.thumbnail?.thumbnails?.pop()?.url;
songInfo.songDuration = data?.videoDetails?.lengthSeconds;
songInfo.image = await getImage(songInfo.imageSrc);
songInfo.uploadDate = data?.microformat?.microformatDataRenderer?.uploadDate;
songInfo.url = data?.microformat?.microformatDataRenderer?.urlCanonical;

win.webContents.send("update-song-info", JSON.stringify(songInfo));
if (data?.videoDetails) {
songInfo.title =
(await getTitle(win)) || cleanupName(data?.videoDetails?.title);
songInfo.artist =
(await getArtist(win)) || cleanupName(data?.videoDetails?.author);
songInfo.views = data?.videoDetails?.viewCount;
songInfo.imageSrc =
(await getImageSrc(win)) || data?.videoDetails?.thumbnail?.thumbnails?.pop()?.url;
songInfo.songDuration =
(await getSongDuration(win)) || data?.videoDetails?.lengthSeconds;
songInfo.image = await getImage(songInfo.imageSrc);
songInfo.uploadDate = data?.microformat?.microformatDataRenderer?.uploadDate;
songInfo.url =
(await getSongURL(win)) || data?.microformat?.microformatDataRenderer?.urlCanonical;
win.webContents.send("update-song-info", JSON.stringify(songInfo));
}
};

// This variable will be filled with the callbacks once they register
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Install the `youtube-music-bin` package from the AUR. For AUR installation instr
- [**SponsorBlock**](https://github.com/ajayyy/SponsorBlock): skips non-music parts
- **Taskbar media control**: control app from your [Windows taskbar](https://user-images.githubusercontent.com/78568641/111916130-24a35e80-8a82-11eb-80c8-5021c1aa27f4.png)
- **Touchbar**: custom TouchBar layout for macOS
- **OBS tunna**: send informations about the song to tuna in port 1608 currently its not tracking song update (progress bar)

## Dev

Expand Down