From b43e3657efea4e4175c0359d6b398984ca77272d Mon Sep 17 00:00:00 2001 From: Adler Luiz Date: Fri, 29 May 2020 15:35:45 -0300 Subject: [PATCH] :sparkles: Fix #126 #112 --- main.js | 7 +- package-lock.json | 80 ++++----- package.json | 2 +- pages/lyrics/lyrics.js | 39 ++++- providers/infoPlayerProvider.js | 6 + providers/trayProvider.js | 301 ++++++++++++++++++-------------- 6 files changed, 256 insertions(+), 179 deletions(-) diff --git a/main.js b/main.js index 8b4652e46..cee2a27cc 100644 --- a/main.js +++ b/main.js @@ -366,8 +366,11 @@ function createWindow() { scrobblerProvider.updateTrackInfo(title, author, album) } - if (!mainWindow.isFocused()) { - tray.balloon(title, author, cover) + if ( + !mainWindow.isFocused() && + settingsProvider.get('settings-show-notifications') + ) { + tray.balloon(title, author, cover, icon) } } diff --git a/package-lock.json b/package-lock.json index 34261184b..8cbff8d80 100644 --- a/package-lock.json +++ b/package-lock.json @@ -281,15 +281,6 @@ "resolved": "https://registry.npmjs.org/after/-/after-0.8.2.tgz", "integrity": "sha1-/ts5T58OAqqXaOcCvaI7UF+ufh8=" }, - "ajax-request": { - "version": "1.2.3", - "resolved": "https://registry.npmjs.org/ajax-request/-/ajax-request-1.2.3.tgz", - "integrity": "sha1-mfy+wdbSeS+F+pSVNTMr0U9fN5A=", - "requires": { - "file-system": "^2.1.1", - "utils-extend": "^1.0.7" - } - }, "ajv": { "version": "6.11.0", "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.11.0.tgz", @@ -495,15 +486,6 @@ "resolved": "https://registry.npmjs.org/base64-arraybuffer/-/base64-arraybuffer-0.1.5.tgz", "integrity": "sha1-c5JncZI7Whl0etZmqlzUv5xunOg=" }, - "base64-img": { - "version": "1.0.4", - "resolved": "https://registry.npmjs.org/base64-img/-/base64-img-1.0.4.tgz", - "integrity": "sha1-PiLVXWx0okVT2EDSsbwSp9sHjTU=", - "requires": { - "ajax-request": "^1.2.0", - "file-system": "^2.1.0" - } - }, "base64-js": { "version": "1.3.1", "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.3.1.tgz", @@ -1433,6 +1415,24 @@ "dev": true, "optional": true }, + "encoding": { + "version": "0.1.12", + "resolved": "https://registry.npmjs.org/encoding/-/encoding-0.1.12.tgz", + "integrity": "sha1-U4tm8+5izRq1HsMjgp0flIDHS+s=", + "requires": { + "iconv-lite": "~0.4.13" + }, + "dependencies": { + "iconv-lite": { + "version": "0.4.24", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", + "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==", + "requires": { + "safer-buffer": ">= 2.1.2 < 3" + } + } + } + }, "end-of-stream": { "version": "1.4.4", "resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.4.tgz", @@ -1681,23 +1681,6 @@ "pend": "~1.2.0" } }, - "file-match": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/file-match/-/file-match-1.0.2.tgz", - "integrity": "sha1-ycrSZdLIrfOoFHWw30dYWQafrvc=", - "requires": { - "utils-extend": "^1.0.6" - } - }, - "file-system": { - "version": "2.2.2", - "resolved": "https://registry.npmjs.org/file-system/-/file-system-2.2.2.tgz", - "integrity": "sha1-fWWDPjojR9zZVqgTxncVPtPt2Yc=", - "requires": { - "file-match": "^1.0.1", - "utils-extend": "^1.0.4" - } - }, "file-type": { "version": "9.0.0", "resolved": "https://registry.npmjs.org/file-type/-/file-type-9.0.0.tgz", @@ -2046,6 +2029,25 @@ "integrity": "sha512-Pgs951kaMm5GXP7MOvxERINe3gsaVjUWFm+UZPSq9xYriQAksyhg0csnS0KXSNRD5NmNdapXEpjxG49+AKh/ug==", "dev": true }, + "image-to-base64": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/image-to-base64/-/image-to-base64-2.1.0.tgz", + "integrity": "sha512-Qlm5F5EJUkxMEY0uPVwbfEkirv4WowHGRj9LBPYU3bAuFucwhvIJiH9g3aJZ91A89jVq9GzmayiNvE2sEjQ9YQ==", + "requires": { + "node-fetch": "^1.7.3" + }, + "dependencies": { + "node-fetch": { + "version": "1.7.3", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-1.7.3.tgz", + "integrity": "sha512-NhZ4CsKx7cYm2vSrBAr2PvFOe6sWDf0UYLRqA6svUYg7+/TSfVAu49jYC4BvQ4Sms9SZgdqGBgroqfDhJdTyKQ==", + "requires": { + "encoding": "^0.1.11", + "is-stream": "^1.0.1" + } + } + } + }, "import-fresh": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/import-fresh/-/import-fresh-2.0.0.tgz", @@ -2173,8 +2175,7 @@ "is-stream": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/is-stream/-/is-stream-1.1.0.tgz", - "integrity": "sha1-EtSj3U5o4Lec6428hBc66A2RykQ=", - "dev": true + "integrity": "sha1-EtSj3U5o4Lec6428hBc66A2RykQ=" }, "is-symbol": { "version": "1.0.3", @@ -7375,11 +7376,6 @@ "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=", "dev": true }, - "utils-extend": { - "version": "1.0.8", - "resolved": "https://registry.npmjs.org/utils-extend/-/utils-extend-1.0.8.tgz", - "integrity": "sha1-zP17ZFQPjpDuIe7Fd2nQZRyril8=" - }, "uuid": { "version": "3.4.0", "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz", diff --git a/package.json b/package.json index 89ce1e485..f486110ea 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,6 @@ }, "dependencies": { "ace-builds": "^1.4.8", - "base64-img": "^1.0.4", "bufferutil": "^4.0.1", "discord-rpc": "^3.1.0", "electron-canvas-to-buffer": "^2.0.0", @@ -46,6 +45,7 @@ "electron-store": "^5.1.0", "electron-updater": "^4.2.0", "i18n": "^0.8.5", + "image-to-base64": "^2.1.0", "js-base64": "^2.5.1", "materialize-css": "^1.0.0", "node-vibrant": "^3.1.5", diff --git a/pages/lyrics/lyrics.js b/pages/lyrics/lyrics.js index cd91d5d45..ff7bff095 100644 --- a/pages/lyrics/lyrics.js +++ b/pages/lyrics/lyrics.js @@ -18,9 +18,7 @@ const lyricProviders = [ const elementLyric = document.getElementById('lyric') -let lastId -let target -let toggled +let lastId, target, toggled loadi18n() @@ -67,6 +65,17 @@ function getLyric(artist, song, id) { ) providerSelected = parseInt(providerSelected) - 1 + retrieveOVHData(artist, song) + .then( + success => { + console.log(success) + }, + error => { + console.log(error) + } + ) + .catch(rejected => console.log(rejected)) + request( urlReplace( lyricProviders[providerSelected].url, @@ -211,3 +220,27 @@ function urlReplace(url, artist, music) { return urlReturn } + +function retrieveOVHData(artist, track) { + return new Promise((resolve, reject) => { + request( + `https://api.lyrics.ovh/v1/${removeAccents(artist)}/${removeAccents( + track + )}`, + { json: true }, + function(err, res, body) { + if (err) { + reject(__.trans('LABEL_LYRICS_NOT_FOUND')) + } + if (body && body.lyrics) { + resolve(body.lyrics) + } else { + reject(__.trans('LABEL_LYRICS_NOT_FOUND')) + } + // document.getElementById('content').scrollTop = 0 + } + ) + }) +} + +function retrieveVagalumeData(artist, track) {} diff --git a/providers/infoPlayerProvider.js b/providers/infoPlayerProvider.js index dcbbffdb9..c13be4d84 100644 --- a/providers/infoPlayerProvider.js +++ b/providers/infoPlayerProvider.js @@ -21,6 +21,7 @@ var track = { id: '', isVideo: false, isAdvertisement: false, + lyrics: '', } function init(view) { @@ -299,6 +300,10 @@ function setSeekbar(webContents, time) { .catch(_ => console.log('error changeSeekbar')) } +function setLyrics(lyrics) { + track.lyrics = lyrics +} + function convertToHuman(time) { var _aux = time var _minutes = 0 @@ -352,4 +357,5 @@ module.exports = { firstPlay: firstPlay, setVolume: setVolume, setSeekbar: setSeekbar, + setLyrics: setLyrics, } diff --git a/providers/trayProvider.js b/providers/trayProvider.js index a762413cf..93e8e0eff 100644 --- a/providers/trayProvider.js +++ b/providers/trayProvider.js @@ -1,164 +1,203 @@ -const { app, Menu, Tray, BrowserWindow } = require("electron"); -const path = require("path"); -const mediaControl = require("./mediaProvider"); -const nativeImage = require("electron").nativeImage; -const settingsProvider = require("./settingsProvider"); -const __ = require("./translateProvider"); -const Notification = require("electron-native-notification"); -const { doBehavior } = require("../utils/window"); -const base64Img = require("base64-img"); -const systemInfo = require("../utils/systemInfo"); - -let tray = null; -let saved_icon = null; -let saved_mainWindow = null; +const { app, Menu, Tray, BrowserWindow } = require('electron') +const path = require('path') +const mediaControl = require('./mediaProvider') +const nativeImage = require('electron').nativeImage +const settingsProvider = require('./settingsProvider') +const __ = require('./translateProvider') +const Notification = require('electron-native-notification') +const { doBehavior } = require('../utils/window') +// const base64Img = require("base64-img"); +const systemInfo = require('../utils/systemInfo') +const imageToBase64 = require('image-to-base64') + +let tray = null +let saved_icon = null +let saved_mainWindow = null function setTooltip(tooltip) { - tray.setToolTip(tooltip); + tray.setToolTip(tooltip) } let init_tray = () => { - const { popUpMenu } = require("./templateProvider"); - const contextMenu = Menu.buildFromTemplate( - popUpMenu(__, saved_mainWindow, mediaControl, BrowserWindow, path, app) - ); + const { popUpMenu } = require('./templateProvider') + const contextMenu = Menu.buildFromTemplate( + popUpMenu(__, saved_mainWindow, mediaControl, BrowserWindow, path, app) + ) - setTooltip("YouTube Music Desktop App"); - tray.setContextMenu(contextMenu); + setTooltip('YouTube Music Desktop App') + tray.setContextMenu(contextMenu) - tray.addListener("click", () => { - doBehavior(saved_mainWindow); - }); + tray.addListener('click', () => { + doBehavior(saved_mainWindow) + }) - tray.addListener("balloon-click", function() { - doBehavior(saved_mainWindow); - }); -}; + tray.addListener('balloon-click', function() { + doBehavior(saved_mainWindow) + }) +} -let popUpMenu = null; +let popUpMenu = null function createTray(mainWindow, icon) { - saved_icon = path.join(__dirname, `../${icon}`); - let nativeImageIcon = nativeImage.createFromPath(saved_icon); - tray = new Tray(nativeImageIcon); - - saved_mainWindow = mainWindow; - if (!systemInfo.isMac()) { - init_tray(); - } else { - // on Mac OS X - setShinyTray(); - } + saved_icon = path.join(__dirname, `../${icon}`) + let nativeImageIcon = nativeImage.createFromPath(saved_icon) + tray = new Tray(nativeImageIcon) + + saved_mainWindow = mainWindow + if (!systemInfo.isMac()) { + init_tray() + } else { + // on Mac OS X + setShinyTray() + } } function updateTrayIcon(icon) { - let nativeImageIcon = nativeImage.createFromPath(icon); - if (systemInfo.isMac()) { - nativeImageIcon = nativeImageIcon.resize({ - height: 18, - width: 18, - quality: "best" - }); - } - tray.setImage(nativeImageIcon); + let nativeImageIcon = nativeImage.createFromPath(icon) + if (systemInfo.isMac()) { + nativeImageIcon = nativeImageIcon.resize({ + height: 18, + width: 18, + quality: 'best', + }) + } + tray.setImage(nativeImageIcon) } -function balloon(title, content, cover) { +/*function balloon(title, content, cover, icon) { if (title && content && cover) { - base64Img.requestBase64(cover, function(err, res, body) { - let image = nativeImage.createFromDataURL(body); - if (settingsProvider.get("settings-show-notifications")) { - if (title && content) { - if (process.platform == "win32") { - tray.displayBalloon({ - icon: image, - title: title, - content: content - }); - } else { - new Notification(title, { - body: content, - icon: image - }); - } - } + base64Img.requestBase64(cover, function (err, res, body) { + let image = nativeImage.createFromPath(icon) + + if (!err && body !== undefined) { + image = nativeImage.createFromDataURL(body); } + + _doNotification(title, content, image) }); } +}*/ + +function balloon(title, content, cover, icon) { + if (title && content && cover) { + imageToBase64(cover) + .then( + response => { + _doNotification( + title, + content, + nativeImage.createFromDataURL( + `data:image/jpeg;base64,${response}` + ) + ) + }, + _ => { + _doNotification( + title, + content, + nativeImage.createFromPath(icon) + ) + } + ) + .catch(_ => { + _doNotification( + title, + content, + nativeImage.createFromPath(icon) + ) + }) + } +} + +function _doNotification(title, content, image) { + if (title && content) { + if (process.platform == 'win32') { + tray.displayBalloon({ + icon: image, + title: title, + content: content, + }) + } else { + new Notification(title, { + body: content, + icon: image, + }) + } + } } function quit() { - tray.destroy(); + tray.destroy() } function setShinyTray() { - if ( - settingsProvider.get("settings-shiny-tray") && - process.platform === "darwin" - ) { - // Shiny tray enabled - tray.setContextMenu(null); - tray.removeAllListeners(); - tray.on("right-click", (event, bound, position) => { - // console.log('right_clicked', bound); - if (!popUpMenu.isVisible()) { - popUpMenu.setPosition(bound.x, bound.y + bound.height + 1); - popUpMenu.show(); - } else { - popUpMenu.hide(); - } - }); - tray.on("click", (event, bound, position) => { - // console.log(position); - if (position.x < 32) { - doBehavior(saved_mainWindow); - } else if (position.x > 130) { - mediaControl.playPauseTrack(saved_mainWindow.getBrowserView()); - } - }); - popUpMenu = new BrowserWindow({ - frame: false, - center: true, - alwaysOnTop: true, - autoHideMenuBar: true, - resizable: false, - backgroundColor: "#232323", - width: 160, - height: 277, - webPreferences: { nodeIntegration: true } - }); - popUpMenu.loadFile( - path.join(__dirname, "../pages/settings/mac_shiny.html") - ); - popUpMenu.setVisibleOnAllWorkspaces(true); - popUpMenu.hide(); - popUpMenu.on("blur", () => { - popUpMenu.hide(); - }); - } else { - // Shiny tray disabled ||| on onther platform - tray.setImage(saved_icon); - tray.removeAllListeners(); - init_tray(); - } + if ( + settingsProvider.get('settings-shiny-tray') && + process.platform === 'darwin' + ) { + // Shiny tray enabled + tray.setContextMenu(null) + tray.removeAllListeners() + tray.on('right-click', (event, bound, position) => { + // console.log('right_clicked', bound); + if (!popUpMenu.isVisible()) { + popUpMenu.setPosition(bound.x, bound.y + bound.height + 1) + popUpMenu.show() + } else { + popUpMenu.hide() + } + }) + tray.on('click', (event, bound, position) => { + // console.log(position); + if (position.x < 32) { + doBehavior(saved_mainWindow) + } else if (position.x > 130) { + mediaControl.playPauseTrack(saved_mainWindow.getBrowserView()) + } + }) + popUpMenu = new BrowserWindow({ + frame: false, + center: true, + alwaysOnTop: true, + autoHideMenuBar: true, + resizable: false, + backgroundColor: '#232323', + width: 160, + height: 277, + webPreferences: { nodeIntegration: true }, + }) + popUpMenu.loadFile( + path.join(__dirname, '../pages/settings/mac_shiny.html') + ) + popUpMenu.setVisibleOnAllWorkspaces(true) + popUpMenu.hide() + popUpMenu.on('blur', () => { + popUpMenu.hide() + }) + } else { + // Shiny tray disabled ||| on onther platform + tray.setImage(saved_icon) + tray.removeAllListeners() + init_tray() + } } function updateImage(payload) { - if (!settingsProvider.get("settings-shiny-tray")) return; - var img = - typeof nativeImage.createFromDataURL === "function" - ? nativeImage.createFromDataURL(payload) // electron v0.36+ - : nativeImage.createFromDataUrl(payload); // electron v0.30 - tray.setImage(img); + if (!settingsProvider.get('settings-shiny-tray')) return + var img = + typeof nativeImage.createFromDataURL === 'function' + ? nativeImage.createFromDataURL(payload) // electron v0.36+ + : nativeImage.createFromDataUrl(payload) // electron v0.30 + tray.setImage(img) } module.exports = { - setTooltip: setTooltip, - createTray: createTray, - balloon: balloon, - quit: quit, - setShinyTray: setShinyTray, - updateImage: updateImage, - updateTrayIcon: updateTrayIcon -}; + setTooltip: setTooltip, + createTray: createTray, + balloon: balloon, + quit: quit, + setShinyTray: setShinyTray, + updateImage: updateImage, + updateTrayIcon: updateTrayIcon, +}