diff --git a/conf/discord.json b/conf/discord.json index d40dd04..b41a651 100644 --- a/conf/discord.json +++ b/conf/discord.json @@ -1,5 +1,5 @@ -{ - "clientID": "701077243652341762", - "token": "NzAxMDc3MjQzNjUyMzQxNzYy.XpsPmw.luwV8qmRlYwibpA3hzT9SnN5mvk", - "secret": "oxiy0FoaI2e4ni0lkK6B9yqaS5IOJ2e1" -} +{ + "clientID": "701077243652341762", + "token": "NzAxMDc3MjQzNjUyMzQxNzYy.XpsPmw.luwV8qmRlYwibpA3hzT9SnN5mvk", + "secret": "oxiy0FoaI2e4ni0lkK6B9yqaS5IOJ2e1" +} diff --git a/conf/routes.json b/conf/routes.json index 9687a6e..9d0c28a 100644 --- a/conf/routes.json +++ b/conf/routes.json @@ -1,25 +1,25 @@ -{ - "discordBot": {}, - "rss-youtube": { - "discord": { - "channel": "701093105864147024", - "color": "#FF0000", - "style": "tinyEmbed", - "footer": { - "text": "vidéo Youtube", - "icon_url": "https://data.kataku.in/cdn/youtube_social.png" - } - } - }, - "rss-skeptikon": { - "discord": { - "channel": "701093136713252925", - "color": "#F1680D", - "style": "embed", - "footer": { - "text": "vidéo Peertube", - "icon_url": "https://data.kataku.in/cdn/skeptikon_social.png" - } - } - } -} +{ + "discordBot": {}, + "rss-youtube": { + "discord": { + "channel": "701093105864147024", + "color": "#FF0000", + "style": "tinyEmbed", + "footer": { + "text": "vidéo Youtube", + "icon_url": "https://data.kataku.in/cdn/youtube_social.png" + } + } + }, + "rss-skeptikon": { + "discord": { + "channel": "701093136713252925", + "color": "#F1680D", + "style": "embed", + "footer": { + "text": "vidéo Peertube", + "icon_url": "https://data.kataku.in/cdn/skeptikon_social.png" + } + } + } +} diff --git a/conf/skepticom.json b/conf/skepticom.json index f92c12a..b6b5379 100644 --- a/conf/skepticom.json +++ b/conf/skepticom.json @@ -1,15 +1,15 @@ -{ - "services" : [ - "logs", - "discord", - "rss-skeptikon", - "rss-youtube" - ], - "pm2": { - "prefix": "skp_", - "mode": "cluster" - }, - "logs": { - "path": "./logs" - } -} +{ + "services" : [ + "logs", + "discord", + "rss-skeptikon", + "rss-youtube" + ], + "pm2": { + "prefix": "skp_", + "mode": "cluster" + }, + "logs": { + "path": "./logs" + } +} diff --git a/modules/discordBot.js b/modules/discordBot.js index 90adbe4..4c38a52 100644 --- a/modules/discordBot.js +++ b/modules/discordBot.js @@ -1,41 +1,41 @@ -/* - SkeptCOM [Discord] | Pour envoyer des notifications sur un serveur Discord -*/ - // LOAD configFiles -const discordConf = require('../conf/discord.json') -const routage = require('../modules/routage')('discordBot') - - // REQUIRE, CONST, VARIABLES -const Discord = require('discord.js') -const client = new Discord.Client() - - - // FUNCTIONS -const myDiscord = { - send: function() {} -} - - // INIT -client.on('ready', () => { - console.log(` --- [Discord] Logged in as ${client.user.tag}!`) - routage.send(` --- [Discord] Logged in as ${client.user.tag}!`) - - myDiscord.send = (channel, msg) => { - client.channels.fetch(channel) - .then(channel => channel.send(msg)) - .catch(console.error) - } - -}) - -// client.on('message', message => { -// if (message.content.startsWith('!ping')) { -// console.log(`on a un ping`) -// routage.send(message.content) -// } -// }) - - -client.login(discordConf.token) - -module.exports = myDiscord +/* + SkeptCOM [Discord] | Pour envoyer des notifications sur un serveur Discord +*/ + // LOAD configFiles +const discordConf = require('../conf/discord.json') +const routage = require('../modules/routage')('discordBot') + + // REQUIRE, CONST, VARIABLES +const Discord = require('discord.js') +const client = new Discord.Client() + + + // FUNCTIONS +const myDiscord = { + send: function() {} +} + + // INIT +client.on('ready', () => { + console.log(` --- [Discord] Logged in as ${client.user.tag}!`) + routage.send(` --- [Discord] Logged in as ${client.user.tag}!`) + + myDiscord.send = (channel, msg) => { + client.channels.fetch(channel) + .then(channel => channel.send(msg)) + .catch(console.error) + } + +}) + +// client.on('message', message => { +// if (message.content.startsWith('!ping')) { +// console.log(`on a un ping`) +// routage.send(message.content) +// } +// }) + + +client.login(discordConf.token) + +module.exports = myDiscord diff --git a/modules/routage.js b/modules/routage.js index 52fd597..6f1918b 100644 --- a/modules/routage.js +++ b/modules/routage.js @@ -1,30 +1,30 @@ -/* - SkeptCOM [IO] | Via un json, on va rediriger qui envoye a quoi -*/ - -const routes = require('../conf/routes.json') -const skepticonf = require('../conf/skepticom.json') -const services = {} - -module.exports = (who) => { - - if (!who || !routes[who]) return console.error(`ERROR routage`) - - services[who] = require('./skeptIO')(who) - - function send(data) { - services[who].send('logs', '', data) - for (let i in routes[who]) { - services[who].send(i, routes[who][i], data) - } - } - - function log(data) { - services[who].send('logs', '', data) - } - - return { - send, - log - } -} +/* + SkeptCOM [IO] | Via un json, on va rediriger qui envoye a quoi +*/ + +const routes = require('../conf/routes.json') +const skepticonf = require('../conf/skepticom.json') +const services = {} + +module.exports = (who) => { + + if (!who || !routes[who]) return console.error(`ERROR routage`) + + services[who] = require('./skeptIO')(who) + + function send(data) { + services[who].send('logs', '', data) + for (let i in routes[who]) { + services[who].send(i, routes[who][i], data) + } + } + + function log(data) { + services[who].send('logs', '', data) + } + + return { + send, + log + } +} diff --git a/modules/skeptIO.js b/modules/skeptIO.js index bd903c5..0cdad33 100644 --- a/modules/skeptIO.js +++ b/modules/skeptIO.js @@ -1,40 +1,40 @@ -/* - SkeptIO | Gestionnaire des services -*/ - -const pm2 = require('pm2') - -module.exports = (who) => { - - function sendMSG(to, conf, data) { - if (!who) return console.error(`ERROR skeptIO ${to}`) - - let dataMSG = { - topic: to, - from: 'skp_' + who, - conf, - data - } - - pm2.list( (err, list) => { // on check la liste des process pm2 - for (let i in list) { - if (list[i].name === 'skp_' + to) { // on verif que le process exite bien - pm2.sendDataToProcessId(list[i].pm_id, dataMSG, (err, res) => { // et on send - if (err) return console.error(` <-- [${list[i].pm_id}:${list[i].name}] error`, err) - }) - } - } - }) - } - - function receiveMSG(res) { - process.on('message', data => { - res(data) - }) - } - - return { - send: sendMSG, - receive: receiveMSG - } -} +/* + SkeptIO | Gestionnaire des services +*/ + +const pm2 = require('pm2') + +module.exports = (who) => { + + function sendMSG(to, conf, data) { + if (!who) return console.error(`ERROR skeptIO ${to}`) + + let dataMSG = { + topic: to, + from: 'skp_' + who, + conf, + data + } + + pm2.list( (err, list) => { // on check la liste des process pm2 + for (let i in list) { + if (list[i].name === 'skp_' + to) { // on verif que le process exite bien + pm2.sendDataToProcessId(list[i].pm_id, dataMSG, (err, res) => { // et on send + if (err) return console.error(` <-- [${list[i].pm_id}:${list[i].name}] error`, err) + }) + } + } + }) + } + + function receiveMSG(res) { + process.on('message', data => { + res(data) + }) + } + + return { + send: sendMSG, + receive: receiveMSG + } +} diff --git a/services/discord.js b/services/discord.js index 023b45d..548fc2a 100644 --- a/services/discord.js +++ b/services/discord.js @@ -1,54 +1,54 @@ -/* - SkeptCOM [Discord] | Pour envoyer des notifications sur un serveur Discord - - exemple pour injecter depuis un routage : - routage.send({ - author: "", - title: "", - url : "", - image: "", - desc: "" - }) -*/ - - // REQUIRE, CONST, VARIABLES -const discordBot = require('../modules/discordBot') -const skeptIO = require('../modules/skeptIO')('discord') - -function embed(conf, data) { - return { - embed: { - color: conf.color, - author: {name: data.author}, - title: data.title, - url: data.url, - image: {url: data.image }, - description: data.desc, - footer: conf.footer - } - } -} - -function tinyEmbed(conf, data) { - return { - embed: { - color: conf.color, - author: {name: data.author}, - title: data.title, - url: data.url, - thumbnail: {url: data.image}, - description: data.desc, - footer: conf.footer - } - } -} - - // INIT -skeptIO.receive( obj => { - console.log(` --> [Discord] message from [${obj.from}]`) - - if (obj.conf.style === "msg") discordBot.send(obj.conf.channel, obj.data.desc) - if (obj.conf.style === "embed") discordBot.send(obj.conf.channel, embed(obj.conf, obj.data)) - if (obj.conf.style === "tinyEmbed") discordBot.send(obj.conf.channel, tinyEmbed(obj.conf, obj.data)) - -}) +/* + SkeptCOM [Discord] | Pour envoyer des notifications sur un serveur Discord + + exemple pour injecter depuis un routage : + routage.send({ + author: "", + title: "", + url : "", + image: "", + desc: "" + }) +*/ + + // REQUIRE, CONST, VARIABLES +const discordBot = require('../modules/discordBot') +const skeptIO = require('../modules/skeptIO')('discord') + +function embed(conf, data) { + return { + embed: { + color: conf.color, + author: {name: data.author}, + title: data.title, + url: data.url, + image: {url: data.image }, + description: data.desc, + footer: conf.footer + } + } +} + +function tinyEmbed(conf, data) { + return { + embed: { + color: conf.color, + author: {name: data.author}, + title: data.title, + url: data.url, + thumbnail: {url: data.image}, + description: data.desc, + footer: conf.footer + } + } +} + + // INIT +skeptIO.receive( obj => { + console.log(` --> [Discord] message from [${obj.from}]`) + + if (obj.conf.style === "msg") discordBot.send(obj.conf.channel, obj.data.desc) + if (obj.conf.style === "embed") discordBot.send(obj.conf.channel, embed(obj.conf, obj.data)) + if (obj.conf.style === "tinyEmbed") discordBot.send(obj.conf.channel, tinyEmbed(obj.conf, obj.data)) + +}) diff --git a/services/logs.js b/services/logs.js index e4d2b57..48a1201 100644 --- a/services/logs.js +++ b/services/logs.js @@ -1,35 +1,35 @@ -/* - SkeptCOM [logs] | on va log les choses, c'est mieux quand on a un problème ... 'fin, les gens bons le fond souvent ♥ -*/ - - // REQUIRE, CONST, VARIABLES -const dateFormat = require('dateformat') -const fs = require('fs') -const fse = require('fs-extra') -const colors = require('colors') -const path = require('path') - -const skepticonf = require('../conf/skepticom.json') -const skeptIO = require('../modules/skeptIO')('logs') -const logConf = {} - - // INIT -if (!skepticonf.logs) return console.log(` [ERROR || LOGS] no configuration for logs.js in ./conf/skepticom.json`) -if (skepticonf.logs.path) { - logConf.path = path.resolve(skepticonf.logs.path) -} else { - logConf.path = path.resolve('./_logs') -} -fse.ensureDirSync(logConf.path) - -skeptIO.receive( obj => { - let rtn = `[${dateFormat("yyyy-mm-dd HH:MM")}] ${obj.from.replace(skepticonf.pm2.prefix, '')}\n ${JSON.stringify(obj.data)}` - - console.log(rtn.magenta) - fs.appendFile(logConf.path + '/' + dateFormat("yyyy-W") + '.log', rtn + '\n', 'utf8', function(err) { - if (err) return console.error(err) - }) - -}) - -console.log(` --- [LOGS] Load`) +/* + SkeptCOM [logs] | on va log les choses, c'est mieux quand on a un problème ... 'fin, les gens bons le fond souvent ♥ +*/ + + // REQUIRE, CONST, VARIABLES +const dateFormat = require('dateformat') +const fs = require('fs') +const fse = require('fs-extra') +const colors = require('colors') +const path = require('path') + +const skepticonf = require('../conf/skepticom.json') +const skeptIO = require('../modules/skeptIO')('logs') +const logConf = {} + + // INIT +if (!skepticonf.logs) return console.log(` [ERROR || LOGS] no configuration for logs.js in ./conf/skepticom.json`) +if (skepticonf.logs.path) { + logConf.path = path.resolve(skepticonf.logs.path) +} else { + logConf.path = path.resolve('./_logs') +} +fse.ensureDirSync(logConf.path) + +skeptIO.receive( obj => { + let rtn = `[${dateFormat("yyyy-mm-dd HH:MM")}] ${obj.from.replace(skepticonf.pm2.prefix, '')}\n ${JSON.stringify(obj.data)}` + + console.log(rtn.magenta) + fs.appendFile(logConf.path + '/' + dateFormat("yyyy-W") + '.log', rtn + '\n', 'utf8', function(err) { + if (err) return console.error(err) + }) + +}) + +console.log(` --- [LOGS] Load`) diff --git a/services/rss-skeptikon.js b/services/rss-skeptikon.js index 0c141f5..6eec016 100644 --- a/services/rss-skeptikon.js +++ b/services/rss-skeptikon.js @@ -1,87 +1,87 @@ -/* - SkeptCOM [RSS-Youtube] | Détecte les nouvelles vidéos YT via RSS -*/ - - - // REQUIRE, CONST, VARIABLES -const jsonfile = require('jsonfile') -const Parser = require('rss-parser') -const path = require('path') - -const routage = require('../modules/routage')('rss-skeptikon') -let parser = new Parser({ - headers: { - Accept: 'application/rss+xml, application/xml', - } -}) -const db = require('../db/rss-skeptikon.json') - - // FUNCTIONS - // et on output ! YOLO ! -function output(data) { - console.log(` --> [RSS-Skeptikon | NV] ${data.author} | ${data.title}`) - if (routage && routage.send) routage.send(data) -} - - // bon, on a besoin de recup le RSS, normol -async function getFeed(url) { - return new Promise((resolve, reject) => { - parser.parseURL(url, (err, res) => { - if (err) return reject(err) - resolve(res) - }) - }) -} - - // on filtre un peu, puis en fonction on output -async function parseRSS(feed) { - - for (let n in feed.items) { - let i = feed.items.length-n-1 // on check en décroissant - if (feed.items[i].isoDate > db.lastCheck || feed.items[i].isoDate > db.latest) { - db.latest = feed.items[i].isoDate - let id = feed.items[i].link.split('/')[feed.items[i].link.split('/').length-1] - let c = { - author: feed.items[i].creator, - title: feed.items[i].title, - url: feed.items[i].link, - id: id, - image: "https://skeptikon.fr/static/previews/" + id + ".jpg", - desc: feed.items[i].content.slice(0, db.config.sliceDescription) + " [...]", - isoDate: feed.items[i].isoDate - } - output(c) - } - } -} - - // on check l'ensemble de feeds -async function checkFeeds(feeds) { - console.log(` --- [RSS-Skeptikon] check @${db.lastCheck}`) - routage.log(`check @${db.lastCheck}`) - - let feed = await getFeed(feeds) - await parseRSS(feed) - - let dateObj = new Date() - db.lastCheck = dateObj.toISOString() - - // et on externalise la nouvelle date. Youpi ! - jsonfile.writeFile(path.resolve('./db/rss-skeptikon.json'), db, {spaces: 2}, (err) => { - if (err) console.error(err) - }) - -} - - -function rssLoop(time) { - checkFeeds(db.config.urlFeeds) - setTimeout(() => { - rssLoop(time) - }, time) -} - - // INIT -console.log(` --- [RSS-Skeptikon] Load`) -routage.log(`Load`) -rssLoop(db.config.timeLoop*60*1000) +/* + SkeptCOM [RSS-Youtube] | Détecte les nouvelles vidéos YT via RSS +*/ + + + // REQUIRE, CONST, VARIABLES +const jsonfile = require('jsonfile') +const Parser = require('rss-parser') +const path = require('path') + +const routage = require('../modules/routage')('rss-skeptikon') +let parser = new Parser({ + headers: { + Accept: 'application/rss+xml, application/xml', + } +}) +const db = require('../db/rss-skeptikon.json') + + // FUNCTIONS + // et on output ! YOLO ! +function output(data) { + console.log(` --> [RSS-Skeptikon | NV] ${data.author} | ${data.title}`) + if (routage && routage.send) routage.send(data) +} + + // bon, on a besoin de recup le RSS, normol +async function getFeed(url) { + return new Promise((resolve, reject) => { + parser.parseURL(url, (err, res) => { + if (err) return reject(err) + resolve(res) + }) + }) +} + + // on filtre un peu, puis en fonction on output +async function parseRSS(feed) { + + for (let n in feed.items) { + let i = feed.items.length-n-1 // on check en décroissant + if (feed.items[i].isoDate > db.lastCheck || feed.items[i].isoDate > db.latest) { + db.latest = feed.items[i].isoDate + let id = feed.items[i].link.split('/')[feed.items[i].link.split('/').length-1] + let c = { + author: feed.items[i].creator, + title: feed.items[i].title, + url: feed.items[i].link, + id: id, + image: "https://skeptikon.fr/static/previews/" + id + ".jpg", + desc: feed.items[i].content.slice(0, db.config.sliceDescription) + " [...]", + isoDate: feed.items[i].isoDate + } + output(c) + } + } +} + + // on check l'ensemble de feeds +async function checkFeeds(feeds) { + console.log(` --- [RSS-Skeptikon] check @${db.lastCheck}`) + routage.log(`check @${db.lastCheck}`) + + let feed = await getFeed(feeds) + await parseRSS(feed) + + let dateObj = new Date() + db.lastCheck = dateObj.toISOString() + + // et on externalise la nouvelle date. Youpi ! + jsonfile.writeFile(path.resolve('./db/rss-skeptikon.json'), db, {spaces: 2}, (err) => { + if (err) console.error(err) + }) + +} + + +function rssLoop(time) { + checkFeeds(db.config.urlFeeds) + setTimeout(() => { + rssLoop(time) + }, time) +} + + // INIT +console.log(` --- [RSS-Skeptikon] Load`) +routage.log(`Load`) +rssLoop(db.config.timeLoop*60*1000) diff --git a/services/rss-youtube.js b/services/rss-youtube.js index 01a7ec5..5a2a482 100644 --- a/services/rss-youtube.js +++ b/services/rss-youtube.js @@ -1,91 +1,91 @@ -/* - SkeptCOM [RSS-Youtube] | Détecte les nouvelles vidéos YT via RSS -*/ - - // REQUIRE, CONST, VARIABLES -const jsonfile = require('jsonfile') -const Parser = require('rss-parser') -const path = require('path') - -const routage = require('../modules/routage')('rss-youtube') -let parser = new Parser({ - customFields: { - item: [ 'author', 'media:group' ] - } -}) -const db = require('../db/rss-youtube.json') - - - // FUNCTIONS - // et on output ! YOLO ! -function output(data) { - console.log(` --> [RSS-Youtube | NV] ${data.author} | ${data.title}`) - if (routage && routage.send) routage.send(data) -} - - // bon, on a besoin de recup le RSS, normol -async function getFeed(url) { - return new Promise((resolve, reject) => { - parser.parseURL(url, (err, res) => { - if (err) return reject(err) - resolve(res) - }) - }) -} - - // on filtre un peu, puis en fonction on output -async function parseRSS(feed, owner) { - if (!db.timecode[owner]) db.timecode[owner] = db.timecode.lastCheck - let cLastCheck = db.timecode[owner] - for (let i in feed.items) { - if (feed.items[i].isoDate > db.timecode.lastCheck || feed.items[i].isoDate > cLastCheck) { - if (feed.items[i].isoDate > cLastCheck) cLastCheck = feed.items[i].isoDate - let c = feed.items[i] - c.desc = feed.items[i]['media:group']['media:description'][0].slice(0, db.config.sliceDescription) + " [...]" - c.image = "http://i3.ytimg.com/vi/" + c.id.split(':')[2] + "/maxresdefault.jpg" - c.url = c.link - delete c.link - delete c['media:group'] - if (c.isoDate > cLastCheck) cLastCheck = feed.items[i].isoDate - output(c) - } - } - db.timecode[owner] = cLastCheck -} - - // on check l'ensemble de feeds -async function checkFeeds(feeds) { - console.log(` --- [RSS-Youtube] check @${db.timecode.lastCheck}`) - routage.log(`check @${db.timecode.lastCheck}`) - for (let i in feeds) { - let feed = await getFeed(db.config.urlFeeds + feeds[i]) - try { - await parseRSS(feed, i) - } catch (e) { - console.error("error while parsing feed %s", feed, e); - } - } - - let dateObj = new Date() - db.timecode.lastCheck = dateObj.toISOString() - - // et on externalise la nouvelle date. Youpi ! - jsonfile.writeFile(path.resolve('./db/rss-youtube.json'), db, {spaces: 2}, (err) => { - console.log() - if (err) console.error(err) - }) - -} - - -function rssLoop(time) { - checkFeeds(db.feeds) - setTimeout(() => { - rssLoop(time) - }, time) -} - - // INIT -console.log(` --- [RSS-Youtube] Load`) -routage.log(`Load`) -rssLoop(db.config.timeLoop*60*1000) +/* + SkeptCOM [RSS-Youtube] | Détecte les nouvelles vidéos YT via RSS +*/ + + // REQUIRE, CONST, VARIABLES +const jsonfile = require('jsonfile') +const Parser = require('rss-parser') +const path = require('path') + +const routage = require('../modules/routage')('rss-youtube') +let parser = new Parser({ + customFields: { + item: [ 'author', 'media:group' ] + } +}) +const db = require('../db/rss-youtube.json') + + + // FUNCTIONS + // et on output ! YOLO ! +function output(data) { + console.log(` --> [RSS-Youtube | NV] ${data.author} | ${data.title}`) + if (routage && routage.send) routage.send(data) +} + + // bon, on a besoin de recup le RSS, normol +async function getFeed(url) { + return new Promise((resolve, reject) => { + parser.parseURL(url, (err, res) => { + if (err) return reject(err) + resolve(res) + }) + }) +} + + // on filtre un peu, puis en fonction on output +async function parseRSS(feed, owner) { + if (!db.timecode[owner]) db.timecode[owner] = db.timecode.lastCheck + let cLastCheck = db.timecode[owner] + for (let i in feed.items) { + if (feed.items[i].isoDate > db.timecode.lastCheck || feed.items[i].isoDate > cLastCheck) { + if (feed.items[i].isoDate > cLastCheck) cLastCheck = feed.items[i].isoDate + let c = feed.items[i] + c.desc = feed.items[i]['media:group']['media:description'][0].slice(0, db.config.sliceDescription) + " [...]" + c.image = "http://i3.ytimg.com/vi/" + c.id.split(':')[2] + "/maxresdefault.jpg" + c.url = c.link + delete c.link + delete c['media:group'] + if (c.isoDate > cLastCheck) cLastCheck = feed.items[i].isoDate + output(c) + } + } + db.timecode[owner] = cLastCheck +} + + // on check l'ensemble de feeds +async function checkFeeds(feeds) { + console.log(` --- [RSS-Youtube] check @${db.timecode.lastCheck}`) + routage.log(`check @${db.timecode.lastCheck}`) + for (let i in feeds) { + let feed = await getFeed(db.config.urlFeeds + feeds[i]) + try { + await parseRSS(feed, i) + } catch (e) { + console.error("error while parsing feed %s", feed, e); + } + } + + let dateObj = new Date() + db.timecode.lastCheck = dateObj.toISOString() + + // et on externalise la nouvelle date. Youpi ! + jsonfile.writeFile(path.resolve('./db/rss-youtube.json'), db, {spaces: 2}, (err) => { + console.log() + if (err) console.error(err) + }) + +} + + +function rssLoop(time) { + checkFeeds(db.feeds) + setTimeout(() => { + rssLoop(time) + }, time) +} + + // INIT +console.log(` --- [RSS-Youtube] Load`) +routage.log(`Load`) +rssLoop(db.config.timeLoop*60*1000)