// @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'); 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; console.log('⌛ interroger la BDD mobilizon postgresql'); async function init() { 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); // console.log('res', res); res.rows.forEach((elem: any) => { // console.log('elem', elem.title); utilsTools.localMobilizonEventsByTitle.push(elem.title); }) 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:any) => { 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; let counter = 0; let bearerTokenIsOK = true; utilsTools.agendadulibre.queryToAdd.forEach((graphQLquery: any) => { if (limiter && counter <= parserConfig.max_new_events && bearerTokenIsOK) { counter++; console.log(counter, ' * ', graphQLquery.variables.title); 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é'); } } 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'); utilsTools.client.end(); console.log('✅ ça c\'est fait '); console.log(' '); } (init)(); console.log('✅ hey ho', this);