Skip to content

Commit

Permalink
add !foto command to take screenshot
Browse files Browse the repository at this point in the history
  • Loading branch information
lluisd committed Feb 4, 2024
1 parent 1b29226 commit dbb3b6d
Show file tree
Hide file tree
Showing 12 changed files with 752 additions and 10 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main_twitch-mz-bot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
envkey_PORT: 3000
envkey_TELEGRAM_TOKEN: ${{ secrets.TELEGRAM_TOKEN }}
envKey_TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }}
envKey_EXTERNAL_URL: ${{ secrets.EXTERNAL_URL }}

- name: Set up Node.js version
uses: actions/setup-node@v4
Expand Down
8 changes: 5 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ mongoose.connect(config.database).then(() => {
.then(() => {
console.log('Connected')

const app = express();
const app = express()

app.use(express.static('public'))
app.use('/images', express.static('images'))

const listener = app.listen(process.env.PORT, ()=> {
console.log('Listening on port ', + listener.address().port)
app.get('/', (req, res) => res.send('Hello World!'))
})
})
const notifier = new Notifier()
notifier.notify()
})


Expand Down
1 change: 1 addition & 0 deletions config/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
externalUrl: process.env.EXTERNAL_URL,
database: process.env.MONGODB_URI,
twitch: {
channels: process.env.TWITCH_CHANNELS,
Expand Down
17 changes: 15 additions & 2 deletions handlers/stream.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
const TwitchService = require('../services/twitch')
const config = require("../config");
const BrowserService = require('../services/browser')
const config = require("../config")
const moment = require('moment')
require('moment-precise-range-plugin')
var math = require('mathjs');
require('mathjs')


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

class Stream {
async captureScreenshot(target, bot, notifierBot, user) {
const image = await BrowserService.getScreenshot()
await bot.say(target, `Captura de ${user}: ${config.externalUrl}/images/${image.fileName}`)
const channel = await TwitchService.getChannel()
await notifierBot.sendPhoto(config.telegram.chatId, image.buffer, {
caption: `Captura del directo _${channel.title}_ \n por *${user}*`,
parse_mode: 'Markdown'
})

}

async catchStream (bot) {
const result = await TwitchService.getStream()

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

isAskingForTakeScreenshot (text) {
return text.toLowerCase().startsWith('!foto')
}
}

module.exports = InputParser
7 changes: 7 additions & 0 deletions lib/messenger.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const inputParser = new InputParser()
const tmi = require('@twurple/auth-tmi')
const { RefreshingAuthProvider } = require('@twurple/auth')
const TokenService = require("../services/token");
const Notifier = require("./notifier");

class Messenger {
constructor () {}
Expand All @@ -20,6 +21,9 @@ class Messenger {
}
this.bot = new tmi.client(opts)

this.notifier = new Notifier()
await this.notifier.notify()

return this.listen()
}

Expand Down Expand Up @@ -72,6 +76,9 @@ class Messenger {
if (textSplit.length > 2 && inputParser.isAskingForNextAveTrain(textSplit[0])) {
return handlers.train.getNextAVE(target, textSplit.slice(1).join(' '), this.bot)
}

if (textSplit.length > 0 && inputParser.isAskingForTakeScreenshot(textSplit[0]))
return handlers.stream.captureScreenshot(target, this.bot, this.notifier.bot, context['display-name'])
}

handleHosting (channel, target, viewers) {
Expand Down
4 changes: 4 additions & 0 deletions lib/notifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ class Notifier {

return Promise.resolve()
}

getBot () {
return this.bot
}
}

module.exports = Notifier
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"mssql": "^10.0.2",
"node-cron": "^3.0.3",
"node-telegram-bot-api": "^0.64.0",
"puppeteer": "^21.11.0",
"tmi.js": "^1.8.5"
},
"scripts": {
Expand Down
Binary file added public/images/6dglgc.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions services/browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const config = require('../config')
const puppeteer = require("puppeteer");
require('mathjs')

async function getScreenshot() {
const browser = await puppeteer.launch({headless: true})
const page = await browser.newPage()
const session = await page.target().createCDPSession()
await session.send("Page.enable")

await page.setViewport({ width: 1920, height: 1080 })

await page.goto("https://www.twitch.tv/" + config.twitch.channels, { waitUntil: 'networkidle0' })
await page.waitForSelector('div.persistent-player')
await page.$eval('.video-player__default-player', el => el.remove())
const svgImage = await page.$('div.persistent-player')
const name = Math.random().toString(36).substring(2,8)
const bufferImage = await svgImage.screenshot({
path: `public/images/${name}.png`,
omitBackground: true
})
await browser.close()

return {buffer: bufferImage, fileName: `${name}.png` }
}



module.exports = {
getScreenshot
}
7 changes: 6 additions & 1 deletion services/twitch.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ async function getStream() {
return { ...result, lastUpdate: channel.lastUpdate}
}

async function getChannel () {
return dbManager.getChannel(config.twitch.channels).lean()
}

async function saveLastMessage (msg) {
await dbManager.updateChannel(config.twitch.channels, { lastMessageId: msg.message_id })
}
Expand All @@ -49,5 +53,6 @@ module.exports = {
getStream,
saveLastMessage,
deleteLastMessage,
saveTitle
saveTitle,
getChannel
}
Loading

0 comments on commit dbb3b6d

Please sign in to comment.