Skip to content

Commit

Permalink
Merge pull request #9 from lluisd/logging_mongodb
Browse files Browse the repository at this point in the history
stream title log
  • Loading branch information
lluisd authored Sep 24, 2024
2 parents 0b035ec + 0c683d1 commit 64ce9e1
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 13 deletions.
11 changes: 11 additions & 0 deletions handlers/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const TwitchService = require('../services/twitch')
const BrowserService = require('../services/browser')
const BirthdayService = require('../services/birthday')
const ScreenshotService = require('../services/screenshot')
const LoggerService = require('../services/logger')
const config = require("../config")
const moment = require('moment')
require('moment-precise-range-plugin')
Expand Down Expand Up @@ -39,6 +40,7 @@ class Stream {

async catchStream (telegramBot, twitchBot, target) {
const result = await TwitchService.getStream()
await this._logStreamTitle(result)
if (result && result.type === 'live' ) {
await this.sendTodayBirthday(twitchBot, target)
const text = this._getText(result)
Expand Down Expand Up @@ -137,6 +139,15 @@ class Stream {
_getTodayBdayText(nick) {
return ${nick} cumple años hoy!`
}

async _logStreamTitle(result) {
if ((result.type === 'live' || result.type === 'stillLive') && result.lastTitle !== result.title ) {
await LoggerService.logStreamTitle(result.user_id, result.title)
}

}


}

module.exports = Stream
17 changes: 11 additions & 6 deletions helpers/dbmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ const Channel = require('../models/channel')
const Birthday = require('../models/birthday')
const Screenshot = require('../models/screenshot')
const tempsDeFlors = require('../models/tempsDeFlors')
const Log = require('../models/log')
const moment = require('moment')
const {InsertOneModel} = require("mongoose");
const ChatLog = require('../models/chatLog')
const TitleLog = require('../models/titleLog')

function getToken (userId) {
return Token.findOne({userId: userId})
Expand Down Expand Up @@ -80,10 +79,15 @@ async function getTFSpots(roomId){
return tempsDeFlors.find({roomId: parseInt(roomId)}).sort('number').lean()
}

async function addLogLine (roomId, nick, text, date) {
return Log.insertMany({roomId: roomId, nick: nick, text: text, date: date})
async function addChatLogLine (roomId, nick, text, date) {
return ChatLog.insertMany({roomId: roomId, nick: nick, text: text, date: date})
}

async function addTitleLogLine (roomId, title, date) {
return TitleLog.insertMany({roomId: roomId, title: title, date: date})
}


module.exports = {
getToken,
updateToken,
Expand All @@ -101,5 +105,6 @@ module.exports = {
getTFSpot,
getTFSpots,
setTFSpot,
addLogLine
addTitleLogLine,
addChatLogLine
}
2 changes: 1 addition & 1 deletion lib/messenger.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Messenger {
async handleText (target, context, msg, self) {
if (self) { return; } // Ignore messages from the bot

await Logger.logMessage(context['room-id'], context['username'], msg)
await Logger.logChatMessage(context['room-id'], context['username'], msg)

const text = msg.trim();
const textSplit = text.split(' ')
Expand Down
4 changes: 2 additions & 2 deletions models/log.js → models/chatLog.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const Schema = mongoose.Schema
const db = require("../db.openai")();

/* Log Schema */
const LogSchema = new Schema({
const ChatLogSchema = new Schema({
roomId: {
type: Number,
required: true
Expand All @@ -23,4 +23,4 @@ const LogSchema = new Schema({
})

const twitchDB = db.useDb('twitch');
module.exports = twitchDB.model('log', LogSchema, 'logs')
module.exports = twitchDB.model('chatLog', ChatLogSchema, 'chatLogs')
18 changes: 18 additions & 0 deletions models/titleLog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const mongoose = require('mongoose')
const Schema = mongoose.Schema
const db = require("../db.openai")();

/* Log Schema */
const TitleLogSchema = new Schema({
roomId: {
type: Number,
required: true
},
title: {
type: String,
required: true
},
})

const twitchDB = db.useDb('twitch');
module.exports = twitchDB.model('titleLog', TitleLogSchema, 'titleLog')
13 changes: 10 additions & 3 deletions services/logger.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
const dbManager = require('../helpers/dbmanager')
const moment = require('moment')

async function logMessage(roomId, username, text) {
async function logChatMessage(roomId, username, text) {
let result = null
result = await dbManager.addLogLine(roomId, username, text, moment())
result = await dbManager.addChatLogLine(roomId, username, text, moment())
return result
}

async function logStreamTitle(roomId, title) {
let result = null
result = await dbManager.addTitleLogLine(roomId, title, moment())
return result
}


module.exports = {
logMessage
logChatMessage,
logStreamTitle
}
2 changes: 1 addition & 1 deletion services/twitch.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async function getStream() {
const channel = await dbManager.getChannel(config.twitch.channels).lean()
if (liveData && !channel.live) {
await dbManager.updateChannel(config.twitch.channels, { live: true, streamId: liveData.id, title: liveData.title, lastUpdate: new Date() })
result = liveData
result = { ...liveData, lastTitle: channel.title}
} else if (!liveData && channel.live) {
await dbManager.updateChannel(config.twitch.channels, { live: false, streamId: null, lastMessageId: null })
result = { type: 'finished', messageId: channel.lastMessageId, streamId: channel.streamId}
Expand Down

0 comments on commit 64ce9e1

Please sign in to comment.