SkeptiCOM/services/rss-skeptikon.js

88 lines
2.3 KiB
JavaScript
Raw Permalink Normal View History

2020-12-12 18:08:49 +01:00
/*
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)