diff --git a/handlers/stream.js b/handlers/stream.js index 4c8f0bf..f98d035 100644 --- a/handlers/stream.js +++ b/handlers/stream.js @@ -9,6 +9,9 @@ const twitchUrl = 'https://www.twitch.tv/' class Stream { + async refreshPage() { + await BrowserService.refreshPage().catch(() => { console.error('refreshPage on refreshPage')}) + } async captureScreenshot(target, bot, notifierBot, user) { const image = await BrowserService.getScreenshot().catch(() => { console.error('getScreenshot on captureScreenshot')}) if (image) { @@ -71,7 +74,7 @@ class Stream { const width = Math.floor(Math.random() * (1280 - 1000 + 1) + 1000) const height = Math.trunc(width / (16/9)) - const image = `[\u200c](${stream.thumbnail_url.replace('-{width}x{height}', `-${width}x${height}`)})` + const image = `[\u200c](${stream.thumbnail_url.replace('-{width}x{height}', `-1280x720`)}?a=${Date.now()})` const link = `[${twitchUrl}${stream.user_name}](${twitchUrl}${stream.user_name})` const title = `🔴 *¡EN DIRECTO!*` return `${image} ${title} ${link} \n _${stream.title}_ (${duration})` diff --git a/helpers/browserApi.js b/helpers/browserApi.js index 2b4a13a..1bbe095 100644 --- a/helpers/browserApi.js +++ b/helpers/browserApi.js @@ -55,12 +55,16 @@ class PuppeteerApi { async handleStart() { await this.page.setViewport({ width: 1920, height: 1080 }) - await this.page.goto("https://www.twitch.tv/" + config.twitch.channels) //{ waitUntil: 'networkidle0' } + await this.page.goto("https://www.twitch.tv/" + config.twitch.channels, { waitUntil: ['networkidle0', 'domcontentloaded'] }) - await this.page.waitForSelector('div.persistent-player') + await this.removeElementsAndGetDiv() + } + + async removeElementsAndGetDiv() { await this.page.$eval('button[data-a-target="consent-banner-accept"]', el => el.click()).catch(() => {}) await this.page.$eval('#twilight-sticky-footer-root', el => el.remove()).catch(() => {}) await this.page.$eval('button[data-a-target="content-classification-gate-overlay-start-watching-button"]', el => el.click()).catch(() => {}) + await this.page.waitForSelector('div.persistent-player') await this.page.$eval('.video-player__default-player', el => el.remove()) this.svgImage = await this.page.$('div.persistent-player') } @@ -71,11 +75,18 @@ class PuppeteerApi { } async checkIfBrowserIsOpen() { - return await this.browser && this.browser.isConnected() + const isConnected = await this.browser.isConnected() + return this.browser && isConnected } async checkIfPageIsOpen() { - return await this.page && !this.page.isClosed() + const isClosed = await this.page.isClosed() + return this.page && !isClosed + } + + async resfreshPage() { + await this.page.reload({ waitUntil: ["networkidle0", "domcontentloaded"] }) + await this.removeElementsAndGetDiv() } diff --git a/lib/inputParser.js b/lib/inputParser.js index 2f84950..1566a18 100644 --- a/lib/inputParser.js +++ b/lib/inputParser.js @@ -22,6 +22,10 @@ class InputParser { isAskingForTakeScreenshot (text) { return text.toLowerCase().startsWith('!foto') } + + isAskingForF5 (text) { + return text.toLowerCase().startsWith('!restartfoto') + } } module.exports = InputParser diff --git a/lib/messenger.js b/lib/messenger.js index 83a4e48..f26f6ff 100644 --- a/lib/messenger.js +++ b/lib/messenger.js @@ -79,6 +79,9 @@ class Messenger { if (textSplit.length > 0 && inputParser.isAskingForTakeScreenshot(textSplit[0])) return handlers.stream.captureScreenshot(target, this.bot, this.notifier.bot, context['display-name']) + + if (textSplit.length > 0 && inputParser.isAskingForF5(textSplit[0])) + return handlers.stream.refreshPage(target, this.bot, this.notifier.bot) } handleHosting (channel, target, viewers) { diff --git a/services/browser.js b/services/browser.js index 717799e..e24a8b6 100644 --- a/services/browser.js +++ b/services/browser.js @@ -39,6 +39,10 @@ async function startAndWarmUpBrowserIfNeeded() { } } +async function refreshPage() { + await browserApi.resfreshPage() +} + async function closeBrowserIfNeeded() { @@ -52,5 +56,6 @@ module.exports = { getScreenshot, closeBrowser, closeBrowserIfNeeded, - startAndWarmUpBrowserIfNeeded + startAndWarmUpBrowserIfNeeded, + refreshPage }