You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
6.3 KiB

2 years ago
// @author tykayn contact@cipherbliss.com www.cipherbliss.com
// imports
import utils from './utils';
import parserConfig from './config';
// @ts-ignore
import Parser from 'rss-parser';
const fetch = require('node-fetch');
2 years ago
let parser = new Parser();
// @ts-ignore
console.log(' ==============================================='.blue);
2 years ago
// @ts-ignore
console.log(' ==== Rss Feeder for mobilizon - by tykayn ===='.blue);
2 years ago
// @ts-ignore
console.log(' ==============================================='.blue);
2 years ago
if (parserConfig.debug) {
1 year ago
// @ts-ignore
console.log('configMobilizon'.blue, config);
2 years ago
}
const utilsTools = new utils();
2 years ago
// trouver si un évènement existe déjà par son titre
let createEventQueries = '';
2 years ago
let counterOfEventsToAdd = 0;
2 years ago
const addEventQuery = utilsTools.agendadulibre.addQuery;
const runCreationQuery = utilsTools.runCreationQuery;
2 years ago
1 year ago
console.log('⌛ interroger la BDD mobilizon postgresql');
2 years ago
1 year ago
async function init() {
2 years ago
1 year ago
utilsTools.setupClientPostgresql();
2 years ago
1 year ago
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);
1 year ago
// console.log('res', res);
res.rows.forEach((elem: any) => {
// console.log('elem', elem.title);
utilsTools.localMobilizonEventsByTitle.push(elem.title);
})
1 year ago
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);
1 year ago
console.log('⌛ lecture du flux rss : ' + parserConfig.rss_feed_url);
fetch(parserConfig.rss_feed_url, {method: 'GET'})
1 year ago
.then((arrayOfEvents: any) => {
console.log('xml rss_feed_url count', Object.keys(arrayOfEvents).length);
1 year ago
});
1 year ago
1 year ago
let feed = await parser.parseURL(parserConfig.rss_feed_url);
console.log('✅ flux rss obtenu');
console.log(feed.title);
1 year ago
1 year ago
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) => {
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
);
let limiter = parserConfig.limit_persistence_of_new_events;
1 year ago
let counter = 0;
let bearerTokenIsOK = true;
1 year ago
utilsTools.agendadulibre.queryToAdd.forEach((graphQLquery: any) => {
1 year ago
if (limiter && counter <= parserConfig.max_new_events && bearerTokenIsOK) {
1 year ago
counter++;
console.log(counter, ' * ', graphQLquery.variables.title);
1 year ago
const body = {
operationName: "createEvent",
query: graphQLquery.query,
variables: graphQLquery.variables
}
if (parserConfig.enableFetch) {
fetch(parserConfig.dev_mode ? parserConfig.dev_url : parserConfig.mobilizon_public_url, {
method: "post",
body: JSON.stringify(body),
headers: {
"Content-Type": "application/json",
"authorization": "Bearer " + parserConfig.bearer_token,
}
})
.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');
}
res.json()
})
.then((json: any) => console.log(json))
.catch((err: any) => console.log(err))
} else {
console.log('---- le fetch est désactivé');
}
1 year ago
}
if (parserConfig.runAddQueriesToMobilizonBDD) {
// @ts-ignore
console.log(
' rajouter les évènements manquants à la base mobilizon',
);
utilsTools.makeQuery();
utilsTools.runCreationQuery();
}
1 year ago
1 year ago
});
2 years ago
1 year ago
}
1 year ago
// @ts-ignore
console.log(' ---------- '.green);
2 years ago
// @ts-ignore
console.log(counterOfEventsToAdd, ' évènements ajoutés '.green);
2 years ago
// @ts-ignore
console.log(' ---------- '.green);
2 years ago
// @ts-ignore
console.log('✅ fermer la connec postgresql');
1 year ago
utilsTools.client.end();
console.log('✅ ça c\'est fait ');
1 year ago
console.log(' ');
}
2 years ago
1 year ago
(init)();
console.log('✅ hey ho', this);