diff --git a/plugins/tuna-obs/back.js b/plugins/tuna-obs/back.js index d9882e4d00..beea699f31 100644 --- a/plugins/tuna-obs/back.js +++ b/plugins/tuna-obs/back.js @@ -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}); + }) } diff --git a/providers/song-info.js b/providers/song-info.js index 0ef0f86897..ec490c62e0 100644 --- a/providers/song-info.js +++ b/providers/song-info.js @@ -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) => { @@ -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: "", @@ -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 diff --git a/readme.md b/readme.md index c47170c11a..e5b03a48d6 100644 --- a/readme.md +++ b/readme.md @@ -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