Skip to content

Commit

Permalink
fix image to TG and refresh photo
Browse files Browse the repository at this point in the history
  • Loading branch information
lluisd committed Feb 7, 2024
1 parent 12e5190 commit ff7bf45
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
5 changes: 4 additions & 1 deletion handlers/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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})`
Expand Down
19 changes: 15 additions & 4 deletions helpers/browserApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
Expand All @@ -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()
}


Expand Down
4 changes: 4 additions & 0 deletions lib/inputParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class InputParser {
isAskingForTakeScreenshot (text) {
return text.toLowerCase().startsWith('!foto')
}

isAskingForF5 (text) {
return text.toLowerCase().startsWith('!restartfoto')
}
}

module.exports = InputParser
3 changes: 3 additions & 0 deletions lib/messenger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 6 additions & 1 deletion services/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ async function startAndWarmUpBrowserIfNeeded() {
}
}

async function refreshPage() {
await browserApi.resfreshPage()
}



async function closeBrowserIfNeeded() {
Expand All @@ -52,5 +56,6 @@ module.exports = {
getScreenshot,
closeBrowser,
closeBrowserIfNeeded,
startAndWarmUpBrowserIfNeeded
startAndWarmUpBrowserIfNeeded,
refreshPage
}

0 comments on commit ff7bf45

Please sign in to comment.