-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
595 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
const Generic = require('./generic') | ||
const Weather = require('./Weather') | ||
const Train = require('./Train') | ||
const Stream = require('./Stream') | ||
|
||
module.exports = { | ||
generic: new Generic(), | ||
weather: new Weather(), | ||
train: new Train() | ||
train: new Train(), | ||
stream: new Stream() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const TwitchService = require('../services/twitch') | ||
const config = require("../config"); | ||
|
||
const twitchUrl = 'https://www.twitch.tv/' | ||
|
||
class Stream { | ||
async catchStream (bot) { | ||
const result = await TwitchService.getStream() | ||
|
||
if (result) { | ||
const image = `[\u200c](${result.thumbnail_url.replace('{width}', '400').replace('{height}', '300')})` | ||
const link = `[${twitchUrl}${result.user_name}](${twitchUrl}${result.user_name})` | ||
const directo = `*¡EN DIRECTO!*` | ||
const text = `${image} ${directo} ${link}` | ||
|
||
const options = { | ||
parse_mode: 'markdown' | ||
} | ||
|
||
bot.sendMessage(config.telegram.chatId, text, options) | ||
} | ||
|
||
// { | ||
// "id": "40400588869", | ||
// "user_id": "779563374", | ||
// "user_login": "manzana_oscura", | ||
// "user_name": "manzana_oscura", | ||
// "game_id": "516575", | ||
// "game_name": "VALORANT", | ||
// "type": "live", | ||
// "title": "aver si veo por donde me matan", | ||
// "viewer_count": 8, | ||
// "started_at": "2024-02-02T12:05:37Z", | ||
// "language": "es", | ||
// "thumbnail_url": "https://static-cdn.jtvnw.net/previews-ttv/live_user_manzana_oscura-{width}x{height}.jpg", | ||
// "tag_ids": [], | ||
// "tags": [ | ||
// "PequeñaGranComunidad", | ||
// "PreguntaYRespondo", | ||
// "lectura", | ||
// "ASMR", | ||
// "Relajacion", | ||
// "Meditacion", | ||
// "Español", | ||
// "English" | ||
// ], | ||
// "is_mature": false | ||
// } | ||
} | ||
} | ||
|
||
module.exports = Stream |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const TelegramBot = require('node-telegram-bot-api') | ||
const config = require('../config') | ||
const cron = require('node-cron') | ||
const handlers = require('../handlers') | ||
|
||
class Notifier { | ||
constructor () { | ||
this.bot = new TelegramBot(config.telegram.apiKey) | ||
} | ||
|
||
notify () { | ||
cron.schedule('*/1 * * * *', () => { | ||
handlers.stream.catchStream(this.bot) | ||
}) | ||
|
||
return Promise.resolve() | ||
} | ||
} | ||
|
||
module.exports = Notifier |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const mongoose = require('mongoose') | ||
const Schema = mongoose.Schema | ||
|
||
/* Channel Schema */ | ||
const ChannelSchema = new Schema({ | ||
name: { | ||
type: String, | ||
required: true | ||
}, | ||
live: { | ||
type: Boolean, | ||
required: true | ||
} | ||
}) | ||
|
||
module.exports = mongoose.model('channel', ChannelSchema, 'channels') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const config = require('../config') | ||
const dbManager = require('../helpers/dbmanager') | ||
|
||
const endpointPrefix = 'https://api.twitch.tv/helix/streams' | ||
|
||
async function getStream() { | ||
let result = null | ||
|
||
const token = await dbManager.getToken(parseInt(config.twitch.userId)).lean() | ||
const endpoint = endpointPrefix + '?user_login=' + config.twitch.channels | ||
const options = { | ||
headers: { | ||
'Client-Id': config.twitch.clientId, | ||
'Authorization': 'Bearer ' + token.accessToken | ||
} | ||
} | ||
const response = await fetch(endpoint, options) | ||
const data = await response.json() | ||
const liveData = data?.data?.[0] ?? null | ||
|
||
const channel = await dbManager.getChannel(config.twitch.channels).lean() | ||
if (liveData && !channel.live) { | ||
await dbManager.updateChannel(config.twitch.channels, true) | ||
result = liveData | ||
} else if (!liveData && channel.live) { | ||
await dbManager.updateChannel(config.twitch.channels, false) | ||
} | ||
|
||
return result | ||
} | ||
|
||
module.exports = { | ||
getStream | ||
} |
Oops, something went wrong.