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-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`)
|
2020-12-20 18:53:35 +01:00
|
|
|
// rssLoop(db.config.timeLoop*60*1000)
|
|
|
|
module.exports = {
|
|
|
|
rssLoop
|
|
|
|
};
|