multi-account-post-schedule.../helpers/libs/rss_management.mjs
2023-08-17 10:55:27 +02:00

85 lines
2.1 KiB
JavaScript

import Parser from 'rss-parser';
import {diffDaysBetweenTwoDates} from "./utils.mjs";
const parser = new Parser()
/**
* fetch and return a parsed rss feed
* @param url
* @returns {Promise<*>}
*/
export async function parseRssFeed(url){
let parsedFeed = await parser.parseURL(url);
console.log(parsedFeed.title);
parsedFeed.items.forEach((item) => {
console.log(item.title);
});
return parsedFeed
}
/**
* get a subset of rss parsed items
* @param parsedRssFeed
* @param days
* @returns {T[]}
*/
export function getArticlesFromDaysInRssFeed(parsedRssFeed , days=7) {
// return parsedRssFeed.items.splice(0,days);
return parsedRssFeed.items.splice(0,6);
}
export function makeTitleContentFromRss(parsedFeed) {
let content = ''
let counter = 0;
if (!parsedFeed.length) {
return '';
}
parsedFeed.forEach((item) => {
// if (!counter) {
// console.log('first item', item)
// }
let cleanedTitle= item.title.replace('[','').trim()
cleanedTitle= cleanedTitle.replace(']','').trim()
content += `\n* [${cleanedTitle}](${item.link}) `;
// content += `\n ${item.isoDate.substring(0,10)} : ${item.contentSnippet.split('\n')[0]} \n`;
counter++;
});
return content;
}
/**
* get a subset of articles in a specified range number of days in the past
* @param json_content
* @param nbOfDays
* @returns {[]}
*/
export function getArticlesFromDaysInJson(json_content,nbOfDays){
let today = new Date()
let selectedEvents = [];
console.log('getArticlesFromDaysInJson: filtre nbOfDays', nbOfDays)
console.log('events ', json_content.length)
json_content.forEach((item) => {
let differenceDays = diffDaysBetweenTwoDates(new Date(item.start_time) ,today);
if( differenceDays < nbOfDays){
console.log('on garde', differenceDays,item.city, item.title)
selectedEvents.push(item)
}
// else{
// console.log('on ne garde pas', differenceDays,item.city, item.title)
// }
})
return selectedEvents
}