rss-feeder-mobilizon/index.ts

188 lines
6.3 KiB
TypeScript
Raw Normal View History

2021-01-03 20:32:27 +01:00
// @author tykayn contact@cipherbliss.com www.cipherbliss.com
// imports
2022-01-10 10:29:30 +01:00
import utils from './utils';
import parserConfig from './config';
// @ts-ignore
2021-01-05 13:26:42 +01:00
import Parser from 'rss-parser';
const fetch = require('node-fetch');
2021-01-03 20:32:27 +01:00
let parser = new Parser();
// @ts-ignore
2021-01-05 13:26:42 +01:00
console.log(' ==============================================='.blue);
2021-01-03 20:32:27 +01:00
// @ts-ignore
2021-01-05 13:26:42 +01:00
console.log(' ==== Rss Feeder for mobilizon - by tykayn ===='.blue);
2021-01-03 20:32:27 +01:00
// @ts-ignore
2021-01-05 13:26:42 +01:00
console.log(' ==============================================='.blue);
2021-01-03 20:32:27 +01:00
if (parserConfig.debug) {
2022-01-10 11:48:28 +01:00
// @ts-ignore
console.log('configMobilizon'.blue, config);
2021-01-03 20:32:27 +01:00
}
const utilsTools = new utils();
2021-01-03 20:32:27 +01:00
// trouver si un évènement existe déjà par son titre
2021-01-05 13:26:42 +01:00
let createEventQueries = '';
2021-01-03 20:32:27 +01:00
let counterOfEventsToAdd = 0;
2021-01-05 11:48:41 +01:00
const addEventQuery = utilsTools.agendadulibre.addQuery;
const runCreationQuery = utilsTools.runCreationQuery;
2021-01-03 20:32:27 +01:00
2022-01-10 11:48:28 +01:00
console.log('⌛ interroger la BDD mobilizon postgresql');
2021-01-03 20:32:27 +01:00
2022-01-10 11:48:28 +01:00
async function init() {
2021-01-05 11:48:41 +01:00
2022-01-10 11:48:28 +01:00
utilsTools.setupClientPostgresql();
2021-01-03 20:32:27 +01:00
2022-01-10 11:48:28 +01:00
await utilsTools.client.connect();
console.log('✅ OK connecté à postgresql');
console.log(' ');
const res = await utilsTools.client.query('SELECT * from events');
2022-01-10 12:48:37 +01:00
console.log('💾 évènements enregistrés dans mobilizon : ', res.rows.length);
2022-01-10 11:48:28 +01:00
// console.log('res', res);
2022-01-10 12:48:37 +01:00
res.rows.forEach((elem: any) => {
// console.log('elem', elem.title);
utilsTools.localMobilizonEventsByTitle.push(elem.title);
})
2021-01-05 11:17:54 +01:00
2022-01-10 11:48:28 +01:00
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);
2021-01-05 11:17:54 +01:00
2022-01-10 11:48:28 +01:00
console.log('⌛ lecture du flux rss : ' + parserConfig.rss_feed_url);
fetch(parserConfig.rss_feed_url, {method: 'GET'})
2021-01-05 11:17:54 +01:00
2022-01-10 11:48:28 +01:00
.then((arrayOfEvents: any) => {
console.log('xml rss_feed_url count', Object.keys(arrayOfEvents).length);
2022-01-10 11:48:28 +01:00
});
2022-01-10 11:06:31 +01:00
2022-01-10 11:48:28 +01:00
let feed = await parser.parseURL(parserConfig.rss_feed_url);
console.log('✅ flux rss obtenu');
console.log(feed.title);
2022-01-10 11:06:31 +01:00
2022-01-10 11:48:28 +01:00
console.log('⚙️ interprétation des évènements');
console.log(
'⌛ trouver les évènements qui ne font pas partie de la BDD mobilizon postgresql',
);
2022-01-13 09:35:57 +01:00
feed.items.forEach((eventFound:any) => {
2022-01-10 11:48:28 +01:00
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');
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',
utilsTools.agendadulibre.queryToAdd.length
);
2022-01-10 12:48:37 +01:00
let limiter = parserConfig.limit_persistence_of_new_events;
2022-01-10 11:48:28 +01:00
let counter = 0;
2022-01-10 12:48:37 +01:00
let bearerTokenIsOK = true;
2022-01-10 11:48:28 +01:00
2022-01-10 12:48:37 +01:00
utilsTools.agendadulibre.queryToAdd.forEach((graphQLquery: any) => {
2022-01-10 11:48:28 +01:00
2022-01-10 12:48:37 +01:00
if (limiter && counter <= parserConfig.max_new_events && bearerTokenIsOK) {
2022-01-10 11:48:28 +01:00
counter++;
2022-01-10 12:48:37 +01:00
console.log(counter, ' * ', graphQLquery.variables.title);
2022-01-10 11:48:28 +01:00
const body = {
operationName: "createEvent",
query: graphQLquery.query,
variables: graphQLquery.variables
}
2022-01-10 12:17:34 +01:00
2022-01-10 12:48:37 +01:00
if (parserConfig.enableFetch) {
fetch(parserConfig.dev_mode ? parserConfig.dev_url : parserConfig.mobilizon_public_url, {
2022-01-10 12:17:34 +01:00
method: "post",
body: JSON.stringify(body),
headers: {
"Content-Type": "application/json",
2022-01-10 12:48:37 +01:00
"authorization": "Bearer " + parserConfig.bearer_token,
2022-01-10 12:17:34 +01:00
}
})
2022-01-10 12:48:37 +01:00
.then((res: any) => {
let status = res.status;
console.log('status', status);
if (status === 401) {
console.error(' /!\\ ------------------ ERROR: Bearer token invalid ------------------')
bearerTokenIsOK = false;
} else if (status === 200) {
console.log('succès');
}
2022-01-10 12:17:34 +01:00
res.json()
})
2022-01-10 12:48:37 +01:00
.then((json: any) => console.log(json))
.catch((err: any) => console.log(err))
} else {
2022-01-10 12:17:34 +01:00
console.log('---- le fetch est désactivé');
}
2022-01-10 11:48:28 +01:00
}
if (parserConfig.runAddQueriesToMobilizonBDD) {
// @ts-ignore
console.log(
' rajouter les évènements manquants à la base mobilizon',
);
utilsTools.makeQuery();
utilsTools.runCreationQuery();
}
2022-01-10 11:06:31 +01:00
2022-01-10 11:48:28 +01:00
});
2021-01-03 20:32:27 +01:00
2022-01-10 11:48:28 +01:00
}
2022-01-10 12:48:37 +01:00
2022-01-10 11:48:28 +01:00
// @ts-ignore
2021-01-05 13:26:42 +01:00
console.log(' ---------- '.green);
2021-01-03 20:32:27 +01:00
// @ts-ignore
2021-01-05 13:26:42 +01:00
console.log(counterOfEventsToAdd, ' évènements ajoutés '.green);
2021-01-03 20:32:27 +01:00
// @ts-ignore
2021-01-05 13:26:42 +01:00
console.log(' ---------- '.green);
2021-01-03 20:32:27 +01:00
// @ts-ignore
2021-01-05 13:26:42 +01:00
console.log('✅ fermer la connec postgresql');
2022-01-10 11:48:28 +01:00
utilsTools.client.end();
2022-01-10 10:29:30 +01:00
console.log('✅ ça c\'est fait ');
2022-01-10 11:48:28 +01:00
console.log(' ');
}
2021-01-03 20:32:27 +01:00
2022-01-10 11:48:28 +01:00
(init)();
console.log('✅ hey ho', this);