From da9cb8e2f5c82d5bb6c4761db8a6d315b6f83a80 Mon Sep 17 00:00:00 2001 From: TC Date: Fri, 6 Oct 2023 20:58:18 +0200 Subject: [PATCH] Add lumiastream beta plugin --- config/defaults.ts | 1 + menu.ts | 14 ++++--- plugins/lumiastream/back.ts | 82 +++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 plugins/lumiastream/back.ts diff --git a/config/defaults.ts b/config/defaults.ts index 11a36ccf..3075a55b 100644 --- a/config/defaults.ts +++ b/config/defaults.ts @@ -81,6 +81,7 @@ const defaultConfig = { }, 'album-color-theme': {}, 'ambient-mode': {}, + 'lumiastream': {}, // Disabled plugins 'shortcuts': { enabled: false, diff --git a/menu.ts b/menu.ts index fca99e6d..adb92065 100644 --- a/menu.ts +++ b/menu.ts @@ -16,6 +16,8 @@ export type MenuTemplate = (Electron.MenuItemConstructorOptions | Electron.MenuI // True only if in-app-menu was loaded on launch const inAppMenuActive = config.plugins.isEnabled('in-app-menu'); +const betaPlugins = ['crossfade', 'lumiastream']; + const pluginEnabledMenu = (plugin: string, label = '', hasSubmenu = false, refreshMenu: (() => void ) | undefined = undefined): Electron.MenuItemConstructorOptions => ({ label: label || plugin, type: 'checkbox', @@ -46,13 +48,13 @@ export const mainMenuTemplate = (win: BrowserWindow): MenuTemplate => { label: 'Plugins', submenu: getAllPlugins().map((plugin) => { + let pluginLabel = plugin; + if (betaPlugins.includes(plugin)) { + pluginLabel += ' [beta]'; + } + const pluginPath = path.join(__dirname, 'plugins', plugin, 'menu.js'); if (existsSync(pluginPath)) { - let pluginLabel = plugin; - if (pluginLabel === 'crossfade') { - pluginLabel = 'crossfade [beta]'; - } - if (!config.plugins.isEnabled(plugin)) { return pluginEnabledMenu(plugin, pluginLabel, true, refreshMenu); } @@ -71,7 +73,7 @@ export const mainMenuTemplate = (win: BrowserWindow): MenuTemplate => { } satisfies Electron.MenuItemConstructorOptions; } - return pluginEnabledMenu(plugin); + return pluginEnabledMenu(plugin, pluginLabel); }), }, { diff --git a/plugins/lumiastream/back.ts b/plugins/lumiastream/back.ts new file mode 100644 index 00000000..a026dbac --- /dev/null +++ b/plugins/lumiastream/back.ts @@ -0,0 +1,82 @@ +import { BrowserWindow , net } from 'electron'; + +import registerCallback from '../../providers/song-info'; + +const secToMilisec = (t?: number) => t ? Math.round(Number(t) * 1e3) : undefined; +const previousStatePaused = null; + +type LumiaData = { + origin: string; + eventType: string; + url?: string; + videoId?: string; + playlistId?: string; + cover?: string|null; + cover_url?: string|null; + title?: string; + artists?: string[]; + status?: string; + progress?: number; + duration?: number; + album_url?: string|null; + album?: string|null; + views?: number; + isPaused?: boolean; +} +const data: LumiaData = { + origin: 'youtubemusic', + eventType: 'switchSong', +}; + + +const post = (data: LumiaData) => { + const port = 39231; + const headers = { + 'Content-Type': 'application/json', + 'Accept': 'application/json', + 'Access-Control-Allow-Headers': '*', + 'Access-Control-Allow-Origin': '*', + }; + const url = `http://localhost:${port}/api/media`; + + net.fetch(url, { method: 'POST', body: JSON.stringify({ token: 'lsmedia_ytmsI7812', data }), headers }) + .catch((error: { code: number, errno: number }) => { + console.log( + `Error: '${ + error.code || error.errno + }' - when trying to access lumiastream webserver at port ${port}` + ); + }); +}; + +export default (_: BrowserWindow) => { + registerCallback((songInfo) => { + if (!songInfo.title && !songInfo.artist) { + return; + } + + if (previousStatePaused === null) { + data.eventType = 'switchSong'; + } else if (previousStatePaused !== songInfo.isPaused) { + data.eventType = 'playPause'; + } + + data.duration = secToMilisec(songInfo.songDuration); + data.progress = secToMilisec(songInfo.elapsedSeconds); + data.url = songInfo.url; + data.videoId = songInfo.videoId; + data.playlistId = songInfo.playlistId; + data.cover = songInfo.imageSrc; + data.cover_url = songInfo.imageSrc; + data.album_url = songInfo.imageSrc; + data.title = songInfo.title; + data.artists = [songInfo.artist]; + data.status = songInfo.isPaused ? 'stopped' : 'playing'; + data.isPaused = songInfo.isPaused; + data.album = songInfo.album; + data.views = songInfo.views; + post(data); + }); +}; + +