Skip to content

Commit

Permalink
fix browser
Browse files Browse the repository at this point in the history
  • Loading branch information
lluisd committed Feb 6, 2024
1 parent 3e44b96 commit fe34986
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 190 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ node.log
/tmp/
.env
.vscode/
public/images/*
.cache/
13 changes: 8 additions & 5 deletions handlers/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ const moment = require('moment')
require('moment-precise-range-plugin')
require('mathjs')


const twitchUrl = 'https://www.twitch.tv/'

class Stream {

async captureScreenshot(target, bot, notifierBot, user) {
const image = await BrowserService.getScreenshot()
const image = await BrowserService.getScreenshot().catch(() => { console.error('getScreenshot on captureScreenshot')})
if (image) {
const channel = await TwitchService.getChannel()
await bot.say(target, `Captura de ${user}: ${config.externalUrl}/images/${image.fileName}`)
Expand All @@ -34,16 +34,16 @@ class Stream {
}

const msg = await bot.sendMessage(config.telegram.chatId, text, options)
try {
await bot.pinChatMessage(config.telegram.chatId, result.messageId)
} catch {}
await bot.pinChatMessage(config.telegram.chatId, result.messageId).catch(() => {})
await TwitchService.saveLastMessage(msg)
await TwitchService.saveTitle(result.title)
await BrowserService.startAndWarmUpBrowserIfNeeded().catch(() => { console.error('startAndWarmUpBrowserIfNeeded on live')})

} else if (result && result.type === 'finished' && result.messageId) {
await bot.deleteMessage(config.telegram.chatId, result.messageId)
await TwitchService.deleteLastMessage()
await TwitchService.saveTitle(result.title)
await BrowserService.closeBrowser().catch(() => { console.error('closeBrowser on finished')})
} else if (result && result.type === 'stillLive' && result.messageId && (result.lastTitle !== result.title || (result.lastUpdate && moment().diff(moment(result.lastUpdate)) > 300000))) {
const options = {
chat_id: config.telegram.chatId,
Expand All @@ -56,6 +56,9 @@ class Stream {
console.log('error')
}
await TwitchService.saveTitle(result.title)
await BrowserService.startAndWarmUpBrowserIfNeeded().catch(() => { console.error('startAndWarmUpBrowserIfNeeded on stillLive')})
} else if (result && result.type === 'notLive') {
await BrowserService.closeBrowserIfNeeded().catch(() => { console.error('closeBrowserIfNeeded on notLive')})
}
}

Expand Down
36 changes: 22 additions & 14 deletions services/browser-api.js → helpers/browserApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ const puppeteer = require('puppeteer-core')
const config = require('../config')

class PuppeteerApi {

browser = null
page = null
svgImage = null

constructor() {
}

Expand All @@ -14,33 +14,33 @@ class PuppeteerApi {
}

async newBrowser() {
return await puppeteer.connect({ browserWSEndpoint: `wss://${config.browserlessUrl}` })
return await puppeteer.connect({ browserWSEndpoint: config.browserlessUrl })
}

async getBrowser() {
async createNewBrowser() {
this.browser = await this.newBrowser()
}

async getBrowser() {
if (!this.browser) {
this.browser = await this.newBrowser()
await this.createNewBrowser()
}

return this.browser
}

async forceNewPage() {
this.page = await this.newPage()
async createNewPage() {
this.page = await this._newPage()
await this.handleStart()
}

async getPage() {
if (!this.page) {
this.page = await this.newPage()
await this.handleStart()
await this.createNewPage()
}
return this.page

}

async newPage() {
async _newPage() {
const browser = await this.getBrowser()
return await browser.newPage()
}
Expand All @@ -55,20 +55,28 @@ 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' }

await this.page.waitForSelector('div.persistent-player')
await this.page.$eval('button[data-a-target="consent-banner-accept"]', el => el.click()).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')
}


async shutdown() {
async closeBrowser() {
await this.browser.close()
}

async checkIfBrowserIsOpen() {
return await this.browser && this.browser.isConnected()
}

async checkIfPageIsOpen() {
return await this.page && !this.page.isClosed()
}


}

Expand Down
37 changes: 32 additions & 5 deletions services/browser.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
const browserApi = require('./browser-api.js')
const browserApi = require('../helpers/browserApi.js')
require('mathjs')

async function getScreenshot() {
let page = null
try {
page = await browserApi.getPage()
await browserApi.getPage()
} catch (error) {
console.log(error)
page = await browserApi.forceNewPage()
await browserApi.createNewPage()
return null
}
const name = Math.random().toString(36).substring(2,8)
Expand All @@ -18,6 +17,34 @@ async function getScreenshot() {
return {buffer: bufferImage, fileName: `${name}.png` }
}

async function closeBrowser() {
return await browserApi.closeBrowser()
}

async function startAndWarmUpBrowserIfNeeded() {
const browserIsOpen = await browserApi.checkIfBrowserIsOpen()

if (!browserIsOpen) {
await browserApi.createNewBrowser()
}
const pageIsOpen = await browserApi.checkIfPageIsOpen()
if (!pageIsOpen) {
await browserApi.createNewPage()
}
}



async function closeBrowserIfNeeded() {
const isOpened = await browserApi.checkIfBrowserIsOpen()
if (isOpened) {
await browserApi.closeBrowser()
}
}

module.exports = {
getScreenshot
getScreenshot,
closeBrowser,
closeBrowserIfNeeded,
startAndWarmUpBrowserIfNeeded
}
Loading

0 comments on commit fe34986

Please sign in to comment.