rss-feeder-mobilizon/index.ts

136 lines
4.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// @author tykayn contact@cipherbliss.com www.cipherbliss.com
// imports
import utils from './utils';
import parserConfig from './config';
import { htmlEscape } from 'escape-goat';
// @ts-ignore
import Parser from 'rss-parser';
const fetch = require('node-fetch');
let parser = new Parser();
// @ts-ignore
console.log(' ==============================================='.blue);
// @ts-ignore
console.log(' ==== Rss Feeder for mobilizon - by tykayn ===='.blue);
// @ts-ignore
console.log(' ==============================================='.blue);
if (parserConfig.debug) {
// @ts-ignore
console.log('configMobilizon'.blue, config);
}
const utilsTools = new utils();
// trouver si un évènement existe déjà par son titre
let createEventQueries = '';
let counterOfEventsToAdd = 0;
const addEventQuery = utilsTools.agendadulibre.addQuery;
const runCreationQuery = utilsTools.runCreationQuery;
(async () => {
console.log('⌛ interroger la BDD mobilizon postgresql');
utilsTools.setupClientPostgresql();
await utilsTools.client.connect();
console.log('✅ OK connecté à postgresql');
console.log(' ');
const res = await utilsTools.client.query('SELECT * from events');
console.log('💾 évènements enregistrés dans mobilizon : ', res.rows.length); // Hello world!
console.log('res', res);
utilsTools.localMobilizonEventsByTitle = res.rows;
if (parserConfig.askAgendaDuLibre) {
// @ts-ignore
console.log(' ===================================================='.blue);
// @ts-ignore
console.log(' ==== demander à l\'agenda du libre son flux RSS ===='.blue);
// @ts-ignore
console.log(' ===================================================='.blue);
console.log('⌛ lecture du flux rss : ' + parserConfig.rss_feed_url);
fetch(parserConfig.rss_feed_url, { method: 'GET' })
.then((arrayOfEvents:any) => {
console.log('xml rss_feed_url count', Object.keys(arrayOfEvents).length);
});
let feed = await parser.parseURL(parserConfig.rss_feed_url);
console.log('✅ flux rss obtenu');
console.log(feed.title);
console.log('⚙️ interprétation des évènements');
console.log(
'⌛ trouver les évènements qui ne font pas partie de la BDD mobilizon postgresql',
);
feed.items.forEach((eventFound) => {
console.log(eventFound.title);
// console.log('item', item)
utilsTools.agendadulibre.doesEventExists(eventFound);
});
}
if (parserConfig.askOsmCal) {
// @ts-ignore
console.log(' ===================================================='.blue);
// @ts-ignore
console.log(' ==== demander à OSM cal ===='.blue);
// @ts-ignore
console.log(' ===================================================='.blue);
fetch(parserConfig.osmcal_url, { method: 'GET' })
.then((res:any) => res.json())
.then((arrayOfEvents:any) => {
console.log('json osmcal count', arrayOfEvents.length);
utilsTools.writeFile('osmcal.json', arrayOfEvents, 'json');
console.log('arrayOfEvents[0]', arrayOfEvents[1])
utilsTools.osmcal.doesEventExists(arrayOfEvents[1])
let EventsToCreate:any = [];
arrayOfEvents.forEach((eventFound:any) => {
if(!utilsTools.osmcal.doesEventExists(eventFound)){
EventsToCreate.push(eventFound);
};
});
utilsTools.createEventQueriesForApi(EventsToCreate)
});
}
if (parserConfig.runAddQueriesToMobilizonAPI) {
console.log(
' rajouter les évènements manquants par l\'API GraphQL',
);
}
if (parserConfig.runAddQueriesToMobilizonBDD) {
// @ts-ignore
console.log(
' rajouter les évènements manquants à la base mobilizon',
);
utilsTools.makeQuery();
utilsTools.runCreationQuery();
// @ts-ignore
console.log(' ---------- '.green);
// @ts-ignore
console.log(counterOfEventsToAdd, ' évènements ajoutés '.green);
// @ts-ignore
console.log(' ---------- '.green);
// @ts-ignore
console.log('✅ fermer la connec postgresql');
await utilsTools.client.end();
console.log('✅ ça c\'est fait ');
}
})();
console.log('✅ hey ho', this);