From 9a7baeac231d7c5e4747eab779facee4b224a569 Mon Sep 17 00:00:00 2001 From: Araxeus <78568641+Araxeus@users.noreply.github.com> Date: Wed, 10 Nov 2021 18:37:16 +0200 Subject: [PATCH] fix tuna time update --- plugins/tuna-obs/back.js | 51 ++++++++++++++++++++++++++-------------- providers/song-info.js | 2 -- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/plugins/tuna-obs/back.js b/plugins/tuna-obs/back.js index d9882e4d..b7a54719 100644 --- a/plugins/tuna-obs/back.js +++ b/plugins/tuna-obs/back.js @@ -1,33 +1,50 @@ -const { ipcRenderer } = require("electron"); +const { ipcMain } = require("electron"); const fetch = require('node-fetch'); const registerCallback = require("../../providers/song-info"); -const post = (data) => { +const secToMilisec = t => Math.round(Number(t) * 1000); +const data = { + cover_url: '', + title: '', + artists: [], + status: '', + progress: 0, + duration: 0, + album_url: '' +}; + +const post = async (data) => { const port = 1608; - headers = {'Content-Type': 'application/json', + headers = { + 'Content-Type': 'application/json', 'Accept': 'application/json', 'Access-Control-Allow-Headers': '*', - 'Access-Control-Allow-Origin': '*'} + 'Access-Control-Allow-Origin': '*' + } const url = `http://localhost:${port}/`; - fetch(url, {method: 'POST', headers, body:JSON.stringify({data})}); + fetch(url, { method: 'POST', headers, body: JSON.stringify({ data }) }); } -module.exports = async (win) => { - registerCallback((songInfo) => { +module.exports = async () => { + ipcMain.on('timeChanged', async (_, t) => { + if (!data.title) return; + data.progress = secToMilisec(t); + post(data); + }); - // Register the callback - if (songInfo.title.length === 0 && songInfo.artist.length === 0) { + registerCallback((songInfo) => { + if (!songInfo.title && !songInfo.artist) { 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}); + data.duration = secToMilisec(songInfo.songDuration) + data.progress = secToMilisec(songInfo.elapsedSeconds) + data.cover_url = songInfo.imageSrc; + data.album_url = songInfo.imageSrc; + data.title = songInfo.title; + data.artists = [songInfo.artist]; + data.status = songInfo.isPaused ? 'Paused' : 'Playing'; + post(data); }) } diff --git a/providers/song-info.js b/providers/song-info.js index ebd1d6c4..37cd55fa 100644 --- a/providers/song-info.js +++ b/providers/song-info.js @@ -45,7 +45,6 @@ const songInfo = { songDuration: 0, elapsedSeconds: 0, url: "", - videoId: "", }; const handleData = async (responseText, win) => { @@ -58,7 +57,6 @@ const handleData = async (responseText, win) => { songInfo.image = await getImage(songInfo.imageSrc); songInfo.uploadDate = data?.microformat?.microformatDataRenderer?.uploadDate; songInfo.url = data?.microformat?.microformatDataRenderer?.urlCanonical?.split("&")[0]; - songInfo.videoId = data?.videoDetails?.videoId; // used for options.resumeOnStart config.set("url", data?.microformat?.microformatDataRenderer?.urlCanonical);