Skip to content

Commit

Permalink
clean up files and update readme file
Browse files Browse the repository at this point in the history
  • Loading branch information
lluisd committed Feb 13, 2024
1 parent bfcdedb commit 86536fe
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 66 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ MONGODB_URI //mongoodb uri for the access/refresh tokens.
AEMET_API_KEY //aemet api key to get spanish weather (https://opendata.aemet.es/centrodedescargas/inicio)
SQL_CONNECTION //sql connection string for the train information
PORT //port of the running app
TELEGRAM_TOKEN //Telegram bot API token to send notifications
TELEGRAM_CHAT_ID //Telegram chat id to send the notifications
EXTERNAL_URL //external url of the running app to create the public image urls
BROWSERLESS_URL //browserless websocket url to take screenshots
```

## Weather
Expand Down Expand Up @@ -41,5 +45,15 @@ Find next departure time of the train from a given origin and destination. It us

```javascript
!ave girona, sants
//result:
//result: Próximo tren AVE de Girona a Barcelona-Sants sale a las 16:30:00
```

## Photo
Take a screenshot of the current stream and send it to the chat.

### commands

```javascript
!foto
//result: https://external.url/images/cclj35.png
```
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mongoose.connect(config.database).then(() => {

const listener = app.listen(process.env.PORT, ()=> {
console.log('Listening on port ', + listener.address().port)
app.get('/', (req, res) => res.send('Hello World!'))
app.get('/', (req, res) => res.send('Live!'))
})
})
})
Expand Down
7 changes: 1 addition & 6 deletions handlers/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ require('mathjs')
const twitchUrl = 'https://www.twitch.tv/'

class Stream {

async refreshPage() {
await BrowserService.refreshPage().catch(() => { console.error('refreshPage on refreshPage')})
}
Expand Down Expand Up @@ -53,11 +52,7 @@ class Stream {
message_id: result.messageId,
parse_mode: 'Markdown'
}
try {
await bot.editMessageText(this._getText(result), options)
} catch {
console.log('error')
}
await bot.editMessageText(this._getText(result), options).catch(() => {})
await TwitchService.saveTitle(result.title)
await BrowserService.startAndWarmUpBrowserIfNeeded().catch(() => { console.error('startAndWarmUpBrowserIfNeeded on stillLive')})
} else if (result && result.type === 'notLive') {
Expand Down
58 changes: 15 additions & 43 deletions helpers/browserApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ class PuppeteerApi {
page = null
svgImage = null

constructor() {
}

getSvgImage() {
return this.svgImage
}


async createNewBrowser() {
this.browser = await puppeteer.connect({ browserWSEndpoint: config.browserlessUrl })
this.browser.on('disconnected', async () => {
Expand All @@ -24,38 +16,11 @@ class PuppeteerApi {
})
}

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

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

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

async _newPage() {
const browser = await this.getBrowser()
return await browser.newPage()
}

async handBack(page) {
// close the page or even reuse it?.
await page.close()

// you could add logic for closing the whole browser instance depending what
// you want.
}

async handleStart() {
await this.page.setViewport({ width: 1920, height: 1080 })
await this.page.goto("https://www.twitch.tv/" + config.twitch.channels, { waitUntil: ['networkidle0', 'domcontentloaded'] })
Expand All @@ -72,9 +37,19 @@ class PuppeteerApi {
this.svgImage = await this.page.$('div.persistent-player')
}

async refreshPage() {
await this.page.reload({ waitUntil: ["networkidle0", "domcontentloaded"] })
await this.removeElementsAndGetDiv()
}

async closeBrowser() {
await this.browser.close()
async takeScreenshot(path) {
if (this.svgImage) {
return await this.svgImage.screenshot({
path: path,
omitBackground: true
})
}
return null
}

async checkIfBrowserIsOpen() {
Expand All @@ -95,12 +70,9 @@ class PuppeteerApi {
return !isClosed
}

async resfreshPage() {
await this.page.reload({ waitUntil: ["networkidle0", "domcontentloaded"] })
await this.removeElementsAndGetDiv()
async closeBrowser() {
await this.browser.close()
}


}

const browserApi = new PuppeteerApi()
Expand Down
2 changes: 1 addition & 1 deletion lib/inputParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class InputParser {
}

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

Expand Down
2 changes: 1 addition & 1 deletion lib/messenger.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ 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]))
if (textSplit.length > 0 && inputParser.isAskingForF5(textSplit[0]) && (context['mod'] || context['vip']))
return handlers.stream.refreshPage(target, this.bot, this.notifier.bot)
}

Expand Down
15 changes: 5 additions & 10 deletions services/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,14 @@ async function getScreenshot() {
}
const name = Math.random().toString(36).substring(2,8)
try {
bufferImage = await browserApi.getSvgImage().screenshot({
path: `public/images/${name}.png`,
omitBackground: true
})
bufferImage = await browserApi.takeScreenshot(`public/images/${name}.png`)
} catch (error) {
console.log(error)
return null
}
return {buffer: bufferImage, fileName: `${name}.png` }
}

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

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

Expand All @@ -44,10 +37,12 @@ async function startAndWarmUpBrowserIfNeeded() {
}

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


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

async function closeBrowserIfNeeded() {
const isOpened = await browserApi.checkIfBrowserIsOpen()
Expand Down
13 changes: 10 additions & 3 deletions services/twitch.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const endpointPrefix = 'https://api.twitch.tv/helix/streams'

async function getStream() {
let result = { type: 'notLive'}
let liveData = null

const token = await dbManager.getToken(parseInt(config.twitch.userId)).lean()
const endpoint = endpointPrefix + '?user_login=' + config.twitch.channels
Expand All @@ -14,9 +15,15 @@ async function getStream() {
'Authorization': 'Bearer ' + token.accessToken
}
}
const response = await fetch(endpoint, options)
const data = await response.json()
const liveData = data?.data?.[0] ?? null

try {
const response = await fetch(endpoint, options)
const data = await response.json()
liveData = data?.data?.[0] ?? null

} catch {
result = { type: 'error'}
}

const channel = await dbManager.getChannel(config.twitch.channels).lean()
if (liveData && !channel.live) {
Expand Down

0 comments on commit 86536fe

Please sign in to comment.